如何在应用程序上下文初始化事件中添加一个钩子?


67

对于常规的Servlet,我想您可以声明一个上下文侦听器,但是对于Spring MVC,Spring可以使它更容易吗?

此外,如果定义了上下文侦听器,然后需要访问在servlet.xml或中定义的bean,我applicationContext.xml将如何访问它们?

Answers:


103

Spring有一些您可以处理的标准事件。

为此,您必须创建并注册一个实现该ApplicationListener接口的bean,如下所示:

package test.pack.age;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class ApplicationListenerBean implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
            // now you can do applicationContext.getBean(...)
            // ...
        }
    }
}

然后,您在servlet.xmlapplicationContext.xml文件中注册此bean :

<bean id="eventListenerBean" class="test.pack.age.ApplicationListenerBean" />

当应用程序上下文初始化时,Spring会通知它。

在Spring 3(如果使用的是此版本)中,ApplicationListener该类是通用的,您可以声明您感兴趣的事件类型,并且事件将被相应地过滤。您可以像下面这样简化您的bean代码:

public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        // now you can do applicationContext.getBean(...)
        // ...
    }
}

好,谢谢 。很高兴知道spring3会过滤事件。我之前确实注意到了applicationlistener类。但它的钩子也会被RequestHandledEvent调用。
teddy teddy 2012年

知道使用注解内容并声明两个类会怎样?非注释(XML)和两个?他们会按照宣布的顺序开火吗?谢谢;)
mmm 2012年

1
只是为了信息,事件开始上下文是ContextStartedEvent文档: - docs.spring.io/spring/docs/3.2.x/javadoc-api/org/...
库马尔Sambhav

3
@Kumar Sambhav:是正确的,但也必须提及差异。参见此处:stackoverflow.com/questions/5728376/…以及此处:forum.spring.io/forum/spring-projects/container/…–
Bogdan

1
来自Spring文档“从Spring 3.0开始,ApplicationListener可以通用地声明其感兴趣的事件类型。在Spring ApplicationContext中注册时,将相应地过滤事件,并且仅针对匹配事件对象调用侦听器。” 因此,您可以instanceof通过实施ApplicationListener<ContextRefreshedEvent>
Ahmad Y. Saleh 2013年

87

从Spring 4.2开始,您可以使用@EventListener文档

@Component
class MyClassWithEventListeners {

    @EventListener({ContextRefreshedEvent.class})
    void contextRefreshedEvent() {
        System.out.println("a context refreshed event happened");
    }
}

如何打印属性等,此方法似乎没有参数?
Kalpesh Soni

只需添加ContextRefreshedEvent作为参数而不是注释参数即可
Gwaptiva

7

创建您的注释

  @Retention(RetentionPolicy.RUNTIME)
    public @interface AfterSpringLoadComplete {
    }

建立课程

    public class PostProxyInvokerContextListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    ConfigurableListableBeanFactory factory;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            try {
                BeanDefinition definition = factory.getBeanDefinition(name);
                String originalClassName = definition.getBeanClassName();
                Class<?> originalClass = Class.forName(originalClassName);
                Method[] methods = originalClass.getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AfterSpringLoadComplete.class)){
                        Object bean = context.getBean(name);
                        Method currentMethod = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
                        currentMethod.invoke(bean);
                    }
                }
            } catch (Exception ignored) {
            }
        }
    }
}

通过@Component注释或在xml中注册此类

<bean class="ua.adeptius.PostProxyInvokerContextListener"/>

并在对上下文初始化后要运行的任何方法上使用批注,例如:

   @AfterSpringLoadComplete
    public void init() {}

1

输入URL时,我只有一个页面应用程序,它正在创建一个HashMap(供我的网页使用),其中包含来自多个数据库的数据。我在服务器启动期间做了以下事情来加载所有内容-

1-创建ContextContextClass

public class MyAppContextListener implements ServletContextListener
    @Autowired

    private  MyDataProviderBean myDataProviderBean; 

    public MyDataProviderBean getMyDataProviderBean() {

        return MyDataProviderBean;

    }

    public void setMyDataProviderBean(MyDataProviderBean MyDataProviderBean) {

        this.myDataProviderBean = MyDataProviderBean;

    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

        System.out.println("ServletContextListener destroyed");

    }


    @Override

    public void contextInitialized(ServletContextEvent context) {

        System.out.println("ServletContextListener started");

        ServletContext sc = context.getServletContext();

        WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(sc);

        MyDataProviderBean MyDataProviderBean = (MyDataProviderBean)springContext.getBean("myDataProviderBean");

        Map<String, Object> myDataMap = MyDataProviderBean.getDataMap();

        sc.setAttribute("myMap", myDataMap);

    }

2-在web.xml中添加以下条目

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

3-在我的控制器类更新的代码中,首先检查ServletContext中的Map

    @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String index(@ModelAttribute("model") ModelMap model) {

            Map<String, Object> myDataMap = new HashMap<String, Object>();
            if (context != null && context.getAttribute("myMap")!=null)
            {

                myDataMap=(Map<String, Object>)context.getAttribute("myMap");
            }

            else
            {

                myDataMap = myDataProviderBean.getDataMap();
            }

            for (String key : myDataMap.keySet())
            {
                model.addAttribute(key, myDataMap.get(key));
            }
            return "myWebPage";

        }

有了这个很大的变化,当我启动tomcat时,它会在startTime期间加载dataMap并将所有内容放入servletContext中,然后Controller类将其用于从已填充的servletContext中获取结果。


0

请在加载应用程序上下文(即应用程序准备好服务)之后按照以下步骤进行一些处理。

  1. 创建以下注释,即

    @Retention(RetentionPolicy.RUNTIME)@Target(value = {ElementType.METHOD,ElementType.TYPE})public @interface AfterApplicationReady {}

2.创建以下类,它是一个侦听器,可在应用就绪状态下进行调用。

    @Component
    public class PostApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {

    public static final Logger LOGGER = LoggerFactory.getLogger(PostApplicationReadyListener.class);
    public static final String MODULE = PostApplicationReadyListener.class.getSimpleName();

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        try {
            ApplicationContext context = event.getApplicationContext();
            String[] beans = context.getBeanNamesForAnnotation(AfterAppStarted.class);

            LOGGER.info("bean found with AfterAppStarted annotation are : {}", Arrays.toString(beans));

            for (String beanName : beans) {
                Object bean = context.getBean(beanName);
                Class<?> targetClass = AopUtils.getTargetClass(bean);
                Method[] methods = targetClass.getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AfterAppStartedComplete.class)) {

                        LOGGER.info("Method:[{} of Bean:{}] found with AfterAppStartedComplete Annotation.", method.getName(), beanName);

                        Method currentMethod = bean.getClass().getMethod(method.getName(), method.getParameterTypes());

                        LOGGER.info("Going to invoke method:{} of bean:{}", method.getName(), beanName);

                        currentMethod.invoke(bean);

                        LOGGER.info("Invocation compeleted method:{} of bean:{}", method.getName(), beanName);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Exception occured : ", e);
        }
    }
}

最后,当您在启动日志声明应用程序之前启动Spring应用程序时,将调用您的侦听器。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.