Home Tutorial Spring Spring3 Ioc @PostConstruct and @PreDestroy example

 
 

Share on Google+Share on Google+
@PostConstruct and @PreDestroy example
Posted on: September 3, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn about the Post Construct and Pre-Destroy construct of spring framework.

@PostConstruct and @PreDestroy example

In this tutorial you will learn how to implement the @PostConstruct and @PreDestroy which work similar to init-method and destroy-method in bean configuration file or implement the InitializingBean and DisposableBean in your bean class. To use @PostConstruct and @PreDestroy you have to register the CommonAnnotationBeanPostProcessor at bean configuration or specifying the <context:annotation-config /> in the bean configuration file.

StudentService.java

package com.roseindia.student.services;

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

public class StudentService {

	String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@PostConstruct
	public void initIt() throws Exception {
		System.out.println("After properties has been set : " + message);
	}

	@PreDestroy
	public void cleanUp() throws Exception {
		System.out.println("Cleaned Everyting");
	}

}

AppMain.java

package com.roseindia.common;

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

import com.roseindia.student.services.StudentService;

public class AppMain {
	public static void main(String[] args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		StudentService stud = (StudentService) context
				.getBean("studentService");
		System.out.println(stud);
		context.close();
	}
}

context.xml

<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
   <context:annotation-config />
 
   <bean id="studentService" class="com.roseindia.student.services.StudentService">
     <property name="message" value="property message" />
   </bean>
 
</beans>

When you run this application it will display message as shown below:


After properties has been set : property message
com.roseindia.student.services.StudentService@ec4a87
....
Cleaned everything!!

Download this example code

Advertisement

Related Tags for @PostConstruct and @PreDestroy example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com