2013-12-23 51 views
0

我有springmvc和angularjs应用程序启动并运行。 在Springmvc我有一个名为userSessionBean的bean。 现在我正在添加一个拦截器,以弹簧mvc和它的前handel方法,我试图访问userSessionBean。Spring拦截器依赖注入

我的问题是“我可以注入userSessionBean内拦截”

/** 
* 
*/ 
package com.loginLite.remote.authentication.interceptors; 

import java.util.Random; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 




import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.servlet.HandlerInterceptor; 
import org.springframework.web.servlet.ModelAndView; 

import com.remote.authentication.model.UserSessionBean; 

/** 
* @author jamju02 
* 
*/ 
public class AuthenticationInteceptor implements HandlerInterceptor { 
@Autowired 
private UserSessionBean userSessionBean = null; 

/** 
* @return the userSessionBean 
*/ 
public UserSessionBean getUserSessionBean() { 
    return userSessionBean; 
} 

/** 
* @param userSessionBean the userSessionBean to set 
*/ 
public void setUserSessionBean(UserSessionBean userSessionBean) { 
    userSessionBean = userSessionBean; 
} 

@Override 
public boolean preHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler) throws Exception { 

    System.out.println("Pre-handle"); 

    return true; 
} 

@Override 
public void postHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler, 
     ModelAndView modelAndView) throws Exception { 
    Random rnd = new Random(); 
    int tokenNumber = 100000 + rnd.nextInt(900000); 
    userSessionBean.setAuthTokenNumber(String.valueOf(tokenNumber)); 
    response.addHeader("AUTH_TOKEN",userSessionBean.getAuthTokenNumber()); 
    System.out.println("Post-handle"); 
} 

@Override 
public void afterCompletion(HttpServletRequest request, 
     HttpServletResponse response, Object handler, Exception ex) 
     throws Exception { 
    System.out.println("After completion handle"); 
} 

} 

我的分发程序Servlet

<mvc:interceptors> 
    <bean class="com.paychex.loginLite.remote.authentication.interceptors.AuthenticationInteceptor"> 
    <property name="userSessionBean" ref="userSessionBean"></property> 
    </bean> 
</mvc:interceptors> 


<bean id="userSessionBean" 
    class="com.paychex.loginLite.remote.authentication.model.UserSessionBean" 
    scope="session"> 
    <aop:scoped-proxy /> 
</bean> 
+1

你试过了吗? – zeroflagL

+0

是的,我尝试了上面提到的代码,userSessionBean对象为null, – user1834664

+0

你的配置是什么样的? – zeroflagL

回答

-1

我终于能够解决这点我是困扰我的问题。 错误是,我为我的拦截器类实现接口“HandlerInterceptor”,只要我删除接口和扩展类“HandlerInterceptorAdapter”依赖注入开始工作正常。

+0

This does not'解决问题。 –