java - ServletContextListener 中的@Autowired

标签 java spring spring-mvc

我有一个 InitApp 类

@Component
public class InitApp implements ServletContextListener {

@Autowired
ConfigrationService weatherConfService;

/** Creates a new instance of InitApp */
public InitApp() {
   }

public void contextInitialized(ServletContextEvent servletContextEvent) {
    System.out.println(weatherConfService);
   }
public void contextDestroyed(ServletContextEvent servletContextEvent) {
   }
}

和 web.xml 中的监听器:

    <listener>
        <listener-class>com.web.Utils.InitApp</listener-class>
    </listener>

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

confService 打印 --> null 什么问题?

最佳答案

当我遇到同样的问题时,我想到了一些想法。

第一个是使用 Spring 实用程序从监听器中的 Spring 上下文检索 bean:

例如:

@WebListener
public class CacheInitializationListener implements ServletContextListener {

    /**
     * Initialize the Cache Manager once the application has started
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        CacheManager cacheManager = WebApplicationContextUtils.getRequiredWebApplicationContext(
                sce.getServletContext()).getBean(CacheManager.class);
        try {
            cacheManager.init();
        } catch (Exception e) {
            // rethrow as a runtime exception
            throw new IllegalStateException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub

    }
}

如果您只有一个或两个 bean,这会很好用。否则它会变得乏味。另一种选择是显式调用 Spring 的 Autowire 实用程序:

@WebListener
public class CacheInitializationListener implements ServletContextListener {

    @Autowired
    private CacheManager cacheManager;

    /**
     * Initialize the Cache once the application has started
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        try {
            cacheManager.init();
        } catch (Exception e) {
            // rethrow as a runtime exception
            throw new IllegalStateException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub

    }
}

这两种解决方案的警告是,必须先加载 Spring 上下文,然后才能工作。鉴于无法使用 @WebListener 定义 Listener 顺序,请确保在 web.xml 中定义 Spring ContextLoaderListener 以强制执行它首先加载(网络描述符中定义的监听器在注释定义的监听器之前加载)。

关于java - ServletContextListener 中的@Autowired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656046/

相关文章:

java - 如何读取 BufferedInputStream 中的一行?

java - Netty 自适应 UDP 组播支持

java - android标签布局问题

java - Spring Boot 安全性 + Thymeleaf : IProcessorDialect class missing

java - Intellij 类路径错误 由 : java. io.FileNotFoundException 引起

java - 如何拦截Spring MVC序列化响应?

java - 将模拟对象提供给另一个模拟对象构造函数?

java - Spring+Hibernate+MySQL CRUD 不工作

java - Spring 绑定(bind) : how to work with something like map?

java - Spring MVC 和 Apache Hadoop 启动 MapReduce Job