如何访问java中spring aop logger类中会话中的任何数据?

如何访问java中spring aop logger类中会话中的任何数据?,java,spring-aop,Java,Spring Aop,我是spring AOP新手,我正在尝试为我的Action类实现日志记录。现在我还想在日志记录时将某些信息保存在数据库中。为了实现这一点,我在会话中有一些数据需要在日志记录类中访问,但试图访问这些数据时,“出现空指针异常”。任何帮助都将不胜感激…谢谢 LoggingInterceptor.java package com.mcmc.utility; import java.io.BufferedWriter; import java.io.File; import java.io.FileWr

我是spring AOP新手,我正在尝试为我的Action类实现日志记录。现在我还想在日志记录时将某些信息保存在数据库中。为了实现这一点,我在会话中有一些数据需要在日志记录类中访问,但试图访问这些数据时,“出现空指针异常”。任何帮助都将不胜感激…谢谢

LoggingInterceptor.java

package com.mcmc.utility;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import com.mcmc.hn.bean.UserInfo;
import com.mcmc.hn.dao.interfaces.UserManagementDao;

public class LoggingInterceptor implements MethodBeforeAdvice,SessionAware{ //, AfterReturningAdvice, ThrowsAdvice
    private static Log log = null;
    Map<String, Object> sesionMap=null;
    HttpSession session = null;
    UserManagementDao userManagementDao;


public LoggingInterceptor(){
}

public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    log = LogFactory.getLog(arg2.getClass());
    log.info("Beginning method: "+arg0.getName());
    System.out.println("BEFORE>>>>>>>>>>>>>>>Beginning method: "+arg0.getName());

    HashMap loggingDescription = new HashMap(); 
    loggingDescription.put(new Integer(1),"This is a method to display List of Users");
    loggingDescription.put(new Integer(2),"This is a method to display account Information of the logged-in User");

     UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT); <-- THIS IS WHERE NULL POINTER EXCEPTION IS GENERATED.
    String usrName = user.getFname() + " " + user.getLname();
    String usrId = user.getUser_id();
    String method="";
    if(arg0.getName().equals("displayUser")){
        method = (String) loggingDescription.get(1);
    }else{
        method = (String) loggingDescription.get(2);
    }
    userManagementDao.logInfo(method,usrName,usrId);
}

public void setSession(Map<String, Object> map) {
    this.sesionMap = map;
}
}        
package com.mcmc.utility;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.Writer;
导入java.lang.reflect.Method;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
导入java.util.HashMap;
导入java.util.Map;
导入javax.servlet.http.HttpSession;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.apache.struts2.interceptor.SessionAware;
返回建议后导入org.springframework.aop.after;
导入org.springframework.aop.MethodBeforeAdvice;
导入org.springframework.aop.ThrowsAdvice;
导入com.mcmc.hn.bean.UserInfo;
导入com.mcmc.hn.dao.interfaces.UserManagementDao;
公共类LoggingInterceptor实现MethodBeforeAdvice、SessionAware{//、ReturningAdvice、ThrowsAdvice之后的方法
私有静态日志=null;
Map sesionMap=null;
HttpSession=null;
UserManagementDao UserManagementDao;
公共日志拦截器(){
}
在(方法arg0,对象[]arg1,对象arg2)抛出Throwable之前的public void{
log=LogFactory.getLog(arg2.getClass());
log.info(“开始方法:+arg0.getName());
System.out.println(“BEFORE>>>>>>>>>开始方法:“+arg0.getName());
HashMap loggingDescription=新建HashMap();
loggingDescription.put(新的整数(1),“这是一个显示用户列表的方法”);
loggingDescription.put(新整数(2),“这是显示登录用户帐户信息的方法”);
UserInfo user=(UserInfo)sesionMap.get(MCMCConstants.user\u INFO\u对象);属性的代码

<!-- Bean configuration -->
<bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean" >
    <property name="proxyInterfaces" value="com.mcmc.hn.dao.interfaces.UserManagementDao">
    </property>
    <property name="target">
        <ref local="userManagementDao" />
    </property>
    <property name="interceptorNames">
        <list>
            <value>theTracingBeforeAdvisor</value>
        </list>
    </property>
</bean> 

    <!-- Bean Classes -->

<!-- <bean id="userManagementDao" class="com.mcmc.hn.dao.UserManagementDaoImpl" /> -->
<!-- Advisor pointcut definition for before advice -->
<bean id="theTracingBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
        <ref local="theTracingBeforeAdvice" />
    </property>
    <property name="pattern">
        <value>.*displayUser.*</value>
    </property>
</bean>

<!-- Advisor pointcut definition for after advice -->

<!-- Advice classes -->
<bean id="theTracingBeforeAdvice" class="com.mcmc.utility.LoggingInterceptor" />

顾问面前的演讲
*displayUser*
UserManagementAction.java-->Action类

public class UserManagementAction extends ActionSupport implements ModelDriven<UserInfo>, RequestAware,SessionAware, ServletResponseAware, ServletRequestAware {


private UserInfo userInfo = new UserInfo();
private UserAddress userAddress = new UserAddress();

UserManagementDao userManagementDao;
KeywordCategoryDao keywordCategoryDao;

HttpServletRequest request = null;  
HttpServletResponse response = null;
HttpSession session = null;
/*ProxyFactoryBean proxyBean = null;*/

Map<Long, Object> userSessionMap=new HashMap<Long, Object>();  // Map is used to hold the user session reference in Servlet Context 

String userName="";
String password="";
String remStatus="";

Map<String, Object> reqMap=null;
Map<String, Object> sesionMap=null;

String usrName="";              //Variables for logging 
String usrId="";



/**
 * Calls a function to retrieve the values from Database. 
 * @return SUCCESS in oder to load the JSP page.
 */
@SuppressWarnings({ "unchecked" })

public String displayUser(){

    UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT);
    usrName = user.getFname() + " " + user.getLname();
    usrId = user.getUser_id();
    String method="This is a method to display List of Users";
    userManagementDao.logInfo(method,usrName,usrId);

    ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
    UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
    List<UserInfo>  Lst  = userManagementDao.displayUser();
    reqMap.put("userList", Lst);

    return SUCCESS;
}

    public String login(){
    ApplicationContext appContext = new FileSystemXmlApplicationContext("classpath:../../WEB-INF/applicationContext.xml");
    UserManagementDao userManagementDao=(UserManagementDao) appContext.getBean("proxyBean");
    String returnStatus = SUCCESS;
    session = request.getSession();
    long usr_id;
    long roleId;
    int count=0,flag=0;
    Map<Long, Object> userMap=new HashMap<Long, Object>();

    int status=0;
    if(remStatus.equals("on")){
        status=1;
    }else{
        status=0;
    }
    session.setAttribute("status", status);

    UserInfo user=userManagementDao.login(userName, password);
    userMap=(Map<Long, Object>)session.getServletContext().getAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP); //Retrieving userMap from Servlet Context with User Session Info

    if( userMap!= null){
        if(userMap.containsKey(user.getId())){
            flag=1;
        }
    }

    if(user!=null && flag==0 ){
        count=1;
        sesionMap.put(MCMCConstants.USER_INFO_OBJECT, user);<--THIS IS THE DATA THAT IS IN SESSION THAT I NEED TO ACCESS IN LOGGINGINTERCEPTOR.java
        usr_id=user.getId();
        roleId=user.getRoleId();            
        //System.out.println("USERID::::::"+roleId);
        sesionMap.put(MCMCConstants.USER_INFO_ID,usr_id);
        sesionMap.put(MCMCConstants.USER_ROLE_ID,roleId);
        // Loading Constants into Session
        SessionUtility.loadCategoryList(sesionMap, keywordCategoryDao);
        if(status==1){
            Cookie cookie = new Cookie ("loginData",userName + "|" + password+"?" + status);
            response.addCookie(cookie);
        }

        userSessionMap.put(user.getId(), user.getUser_id());
        session.getServletContext().setAttribute(MCMCConstants.USER_SESSION_REFERENCE_MAP, userSessionMap); //Setting userSessionMap to ServletContext

        returnStatus=SUCCESS;
    }else{
        returnStatus= "input";
    }

    return returnStatus;
}

/* Getter And Setter Methods as required */
}
公共类UserManagementAction扩展了ActionSupport实现了ModelDriven、RequestAware、SessionAware、ServletResponseAware、ServletRequestAware{
private UserInfo UserInfo=new UserInfo();
private UserAddress UserAddress=new UserAddress();
UserManagementDao UserManagementDao;
关键词类别dao关键词类别dao;
HttpServletRequest请求=null;
HttpServletResponse=null;
HttpSession=null;
/*ProxyFactoryBean proxyBean=null*/
Map userSessionMap=new HashMap();//Map用于在Servlet上下文中保存用户会话引用
字符串userName=“”;
字符串密码=”;
字符串remStatus=“”;
Map reqMap=null;
Map sesionMap=null;
字符串usrName=“;//用于日志记录的变量
字符串usrId=“”;
/**
*调用函数从数据库中检索值。
*@return成功加载JSP页面。
*/
@SuppressWarnings({“unchecked”})
公共字符串displayUser(){
UserInfo user=(UserInfo)sesionMap.get(MCMCConstants.user\u INFO\u对象);
usrName=user.getFname()+“”+user.getLname();
usrId=user.getUser_id();
String method=“这是一种显示用户列表的方法”;
userManagementDao.logInfo(方法、usrName、usrId);
ApplicationContext appContext=新文件系统XMLApplicationContext(“类路径:../../WEB-INF/ApplicationContext.xml”);
UserManagementDao UserManagementDao=(UserManagementDao)appContext.getBean(“proxyBean”);
List Lst=userManagementDao.displayUser();
请求映射放置(“用户列表”,Lst);
回归成功;
}
公共字符串登录(){
ApplicationContext appContext=新文件系统XMLApplicationContext(“类路径:../../WEB-INF/ApplicationContext.xml”);
UserManagementDao UserManagementDao=(UserManagementDao)appContext.getBean(“proxyBean”);
字符串returnStatus=SUCCESS;
session=request.getSession();
长usr_id;
长罗莱德;
整数计数=0,标志=0;
Map userMap=newhashmap();
int status=0;
if(remStatus.equals(“on”)){
状态=1;
}否则{
状态=0;
}
session.setAttribute(“状态”,status);
UserInfo user=userManagementDao.login(用户名、密码);
userMap=(Map)session.getServletContext().getAttribute(MCMCConstants.USER_session_REFERENCE_Map);//使用用户会话信息从Servlet上下文检索userMap
if(userMap!=null){
if(userMap.containsKey(user.getId())){
flag=1;
}
}
if(user!=null&&flag==0){
计数=1;

sesionMap.put(MCMCConstants.USER_INFO_OBJECT,USER);为什么不为空?UserManagementDao未注入LoggingInterceptor,也未初始化。事实上,它已在配置文件中注释掉

<!--<bean id="userManagementDao" class="com.mcmc.hn.dao.UserManagementDaoImpl"/>-->

除非userManagementDao中有@Repository和组件扫描,否则它甚至不是Springbean。请通读它以供参考

Map<String, Object> sesionMap=null;
在这里使用相同的sessionMap…这就是为什么要获取null指针

UserInfo user = (UserInfo)sesionMap.get(MCMCConstants.USER_INFO_OBJECT);