欢迎光临
我们一直在努力

Java监听器中获取service实例

JAVA.jpg

public class WebContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
        ISrInforService srInforService = applicationContext.getBean(ISrInforService.class);
//        ISrInforService srInforService = (ISrInforService) applicationContext.getBean("srInforService");
        srInforService.countAllSrInforsScore();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
ISrInforService srInforService = applicationContext.getBean(ISrInforService.class);

ISrInforService srInforService = (ISrInforService) applicationContext.getBean("srInforService");

都可以。

然后配置web.xml

<!-- Spring监听配置,Web容器启动自动装配ApplicationContext的配置信息-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

参考:

  1. 在监听器中注入spring bean
  2. Java 的监听器中使用 Spring 框架中的某些 service
赞(0)
未经允许不得转载:Ddmit » Java监听器中获取service实例

评论 抢沙发

登录

找回密码

注册