Spring Bean init-method and destroy-method with Annotations

In This Tutorial We will Learn Spring bean int-method ad destroy-method with annotations. We can do this by usind @PostConstruct and @PreDestroy

Let's See an Example

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml
  • spring-core
  • spring-beans
  • spring-context

1.pom.xml

<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>com.practice</groupId>
<artifactId>postconstructpredestroy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>


</dependencies>
</project>

2.Writing Spring Beans

In This Example Im Writing one Spring Bean Named With Demo

Demo.java


package com.practice;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Demo {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
@PostConstruct
public void init()throws Exception{
System.out.println("Init method :"+message);
}

@Override
public String toString() {
return "Demo [message=" + message + "]";
}

@PreDestroy
public void cleanUp()throws Exception{

System.out.println("destroy method ");

}

}



2.Setting Up Spring Configuration File

applicationContext.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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config/>

<bean id = "demoBean" class = "com.practice.Demo" >
<property name = "message" value = "welcome to javalschool"></property>
</bean>
</beans>

3.Now Create Test class ,RunThe Code

AppTest.java


package com.practice.test;

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

import com.practice.Demo;

public class AppTest {


public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
Demo account = (Demo)context.getBean("demoBean");

System.out.println(account.toString());
context.close();



}

}



output:


INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Init method :welcome to javalschool
Demo [message=welcome to javalschool]
Sep 03, 2016 12:00:50 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1be87a0: startup date [Sat Sep 03 00:00:49 IST 2016]; root of context hierarchy
destroy method

No comments:

Post a Comment