Spring – Event Handling | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

Spring provides the following standard events –

1. ContextRefreshedEvent

  • This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.

2. ContextStartedEvent

  • This event is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can poll your database or you can restart any stopped application after receiving this event.

3. ContextStoppedEvent

  • This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.

4. ContextClosedEvent

  • This event 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.

. . . . .

  • To do event handling BeanFactory is not sutable, it is only for instantiation POJOs and DI. ApplicationContext is also not sutable to manage lifecycle. We need to use ConfigurableApplicationContext.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create EventHandlers project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 5.X jars)
    • commons-logging-X.X.jar
    • spring-aop-X.X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean class="com.codeFactory.beans.IOCStartEventListener" />
	<bean class="com.codeFactory.beans.IOCRefreshEventListener" />
	<bean class="com.codeFactory.beans.IOCCloseEventListener" />
	<bean class="com.codeFactory.beans.IOCStopEventListener" />
</beans>

IOCRefreshEventListener.java

package com.codeFactory.beans;

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

/**
 * @author code.factory
 *
 */
public class IOCRefreshEventListener implements ApplicationListener<ContextRefreshedEvent> {

	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		System.out.println("On IOC Refresh method and while container startup");
	}
}

IOCStartEventListener.java

package com.codeFactory.beans;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;

/**
 * @author code.factory
 *
 */
public class IOCStartEventListener implements ApplicationListener<ContextStartedEvent> {

	@Override
	public void onApplicationEvent(ContextStartedEvent event) {
		System.out.println("On IOC start method");
	}
}

IOCStopEventListener.java

package com.codeFactory.beans;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

/**
 * @author code.factory
 *
 */
public class IOCStopEventListener implements ApplicationListener<ContextStoppedEvent> {

	@Override
	public void onApplicationEvent(ContextStoppedEvent arg0) {
		System.out.println("On IOC Stop method");
	}
}

IOCCloseEventListener.java

package com.codeFactory.beans;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;

/**
 * @author code.factory
 *
 */
public class IOCCloseEventListener implements ApplicationListener<ContextClosedEvent> {

	@Override
	public void onApplicationEvent(ContextClosedEvent arg0) {
		System.out.println("On IOC Close method");
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		context.start(); // on ioc start
		context.stop(); // on stop
		context.close(); // for shutdown
		context.refresh(); // once again container startup
	}
}

Output:

On IOC Refresh method and while container startup
On IOC start method
On IOC Stop method
On IOC Close method
On IOC Refresh method and while container startup

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s