Lifecycle Callback Methods – Java and Spring Examples http://localhost/wordpress Sat, 01 Jun 2019 14:53:50 +0000 fr-FR hourly 1 https://wordpress.org/?v=5.0.4 Example Spring 4 – Lifecycle Callback Methods http://localhost/wordpress/2019/02/19/example-spring-4-dependency-injection-lifecycle-callback-methods/ Tue, 19 Feb 2019 16:55:16 +0000 http://localhost/wordpress/?p=497 This example presents the basic concept of using setter injection in Spring.

The technologies used are :
– Spring 4.3.4
– JDK 1.8
– Maven 3.3.9

You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :

mvn eclipse:eclipse
package javaspringexamples.spring.ioc.LifecycleCallbackMethods;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class A {
	@PostConstruct
	public void init() throws Exception {
		System.out.println("A @PostConstruct-init() method invoked");
	}

	@PreDestroy
	public void destroy() throws RuntimeException {
		System.out.println("A @PreDestroy-destroy() method invoked");
	}
}

package javaspringexamples.spring.ioc.LifecycleCallbackMethods;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class B implements InitializingBean, DisposableBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("B InitializingBean-afterPropertiesSet() method invoked");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("B DisposableBean-destroy() method invoked");
	}

}

package javaspringexamples.spring.ioc.LifecycleCallbackMethods;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class C {
	public void init() throws Exception {
		System.out.println("C init-method=\"init\"-init() method invoked");
	}

	public void destroy() throws RuntimeException {
		System.out.println("C destroy-method=\"destroy\"-destroy() method invoked");
	}
}

package javaspringexamples.spring.ioc.LifecycleCallbackMethods;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class Main {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"classpath:/applicationContext.xml");
		applicationContext.registerShutdownHook();
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:annotation-config />

	<bean
		class="javaspringexamples.spring.ioc.LifecycleCallbackMethods.A" />
	<bean
		class="javaspringexamples.spring.ioc.LifecycleCallbackMethods.B" />
	<bean id="c"
		class="javaspringexamples.spring.ioc.LifecycleCallbackMethods.C"
		init-method="init" destroy-method="destroy" />

</beans>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>javaspringexamples.spring</groupId>
	<artifactId>Lifecycle-Callback-Methods</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>example Lifecycle Callback Methods</name>

	<properties>
		<springframework.version>4.3.4.RELEASE</springframework.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

The output is :
A @PostConstruct-init() method invoked
B InitializingBean-afterPropertiesSet() method invoked
C init-method= »init »-init() method invoked
C destroy-method= »destroy »-destroy() method invoked
B DisposableBean-destroy() method invoked
A @PreDestroy-destroy() method invoked

Get the sources of the example from the following GitHub url

Or Download a .zip file

]]>