Spring @Autowired提供了新实例而不是初始化的会话范围bean

时间:2017-03-13 18:50:43

标签: java spring autowired

我正在使用Spring开发应用程序,我需要在会话中初始化Ticket对象,然后从另一个控制器获取它。

我有两个控制器,因此有两个xml上下文。

  

的web.xml

Traceback (most recent call last):
  File "MeanShift.py", line 4, in <module>
    from sklearn.cluster import MeanShift, estimate_bandwidth
ImportError: No module named sklearn.cluster
  

其余-context.xml中

    <web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring Web MVC Application</display-name>

   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
   </welcome-file-list>

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-context.xml</param-value>
   </context-param>

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <servlet>
      <servlet-name>rest</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/rest-context.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>rest</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>

   <servlet>
      <servlet-name>index</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/index-context.xml</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>index</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>
</web-app>
  

索引context.xml中

<beans ...>
    <mvc:annotation-driven/>    
    <context:annotation-config/>    
    <context:component-scan base-package="menu.MenuController"/>    
</beans>

在spring-context中我有我的Ticket bean:

<beans ...>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/**" location="/"/>

    <context:annotation-config/>    
    <bean class="auth.IndexWebController" />    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>

与其他豆类一起。

这就是我的控制器的样子:

  

IndexWebController.java

<bean id="tickerProvider" class="TicketProviderImpl" scope="session">
    <aop:scoped-proxy/>
  

MenuController.java

@Controller
@SessionAttributes("user")
public class IndexWebController {

    private static Logger logger = LoggerFactory.getLogger(IndexWebController.class);

    @Autowired
    private TicketProviderImpl ticketProvider;

    @ModelAttribute("user")
    public User populateUser() {
        return new User();
    }

    @RequestMapping(value = "/", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView printIndex(@ModelAttribute("user") User user,
                                   @RequestParam(value = "iv-user", required = false) String login,
                                   @RequestParam(value = "iv-groups", required = false) String groups) {
        ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("index.html");

<...>            
        ticketProvider.setLogin(login);
                logger.info("Пользователь установлен в " + login);
    <...>

问题是当我在MenuController中自动装配ticketProvider bean时,我得到了一个新的实例,而不是那些已经在IndexWebController中初始化的实例。实际上我得到null,但等待登录。

使用会话范围的bean的错误或误解在哪里?

1 个答案:

答案 0 :(得分:-1)

根据this,您需要另一个侦听器来将您的会话公开给应用程序。 因此,添加<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>应该可以解决问题。