Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Standard events of ApplicationContext in Spring

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 923
    Comment on it

    Hello Guys
    ApplicationContext is manages complete life cycle of the beans in spring application. The ApplicationContext publishes many types of events when loading the beans.
    For example:

    ContextRefreshedEvent : It is published when the ApplicationContext is initialized or refreshed. This can be raise using the refresh() method on the ConfigurableApplicationContext interface.

    ContextStartedEvent : It is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can re/start any stopped application after receiving this event.

    ContextStoppedEvent : It is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface.

    ContextClosedEvent : It is published when the ApplicationContext is closed using the close() method on the ConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.

    RequestHandledEvent : It is a web-specific event telling all beans that an HTTP request has been serviced.

    In ApplicationContext event handling is provided through the ApplicationEvent class and ApplicationListener interface. Every time an ApplicationEvent gets published to the ApplicationContext, whenever a bean implements the ApplicationListener, that bean is notified.

    I am writing live example of Standard events, you can see below example -

    Create ContextEventListener.java

    package com.findnerd.spring.events.listener;
     
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
     
    /**
     * @author Bhagwan Singh
     */
    public class ContextEventListener implements ApplicationListener<ApplicationEvent> {
     
        /** 
         * This method will get invoked when an event is triggered.
      * @param event
         */
        public void onApplicationEvent(ApplicationEvent event) {
            System.out.println(event.getClass().getName() + " received!");
        }
    }
    

    Create ApplicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
         
        <bean class="com.findnerd.spring.events.listener.ContextEventListener" />
         
    </beans>
    

    Create ContextEventListenerTest.java

    package com.findnerd.spring.events.listener;
     
    import org.junit.Test;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    /**
     * @author Bhagwan Singh
     */
    public class ContextEventListenerTest {
         
        @Test
        public void test() {
            ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            ctx.refresh();
            ctx.start();
            ctx.stop();
            ctx.close();
             
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: