2013-12-23 135 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'解決問題。 –