Thursday, May 18, 2017

Spring Bean Life Cycle Example



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>SpringBeanLifeCycle</groupId>
<artifactId>SpringBeanLifeCycle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<spring-framework.version>4.3.8.RELEASE</spring-framework.version>
</properties>

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

</dependencies>
</project>

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 id="user" class="com.masterjee.User" />
<bean id="userService" class="com.masterjee.UserService">
<property name="user" ref="user" />
</bean>
<bean id="myService" class="com.masterjee.MyService" />
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
</beans>

User.java

package com.masterjee;

public class User {
    private int id;
    private String name;
    private String email;
    private String contact;
    private String password;
    private String passwordConfirm;

    public User(){
        System.out.println("Creating User instance");
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

}

UserService.java

package com.masterjee;

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

public class UserService {
    private User user;

    @PostConstruct
    public void init() {
        System.out.println("init method of UserService using @PostConstruct");
        this.user.setName("Anil");
    }

    public UserService() {
        System.out.println("creating instance of UserService");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("destroy method of UserService using @PreDestroy");
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

MyService.java

package com.masterjee;

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

public class MyService {
    @PostConstruct
    public void init() {
        System.out.println("init method of UserService using @PostConstruct");
    }

    public MyService() {
        System.out.println("creating instance of UserService");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("destroy method of UserService using @PreDestroy");
    }

}

MainClass.java

package com.masterjee;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainClass {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("Spring Context started");
        UserService userService = (UserService) applicationContext.getBean("userService");
        System.out.println("Bean received from application context");
        System.out.println("name: "+userService.getUser().getName());
        applicationContext.close();
        System.out.println("Spring Context closed");
    }

}

Console

May 18, 2017 5:26:18 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@433c675d: startup date [Thu May 18 17:26:18 IST 2017]; root of context hierarchy
May 18, 2017 5:26:18 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Creating User instance
creating instance of UserService
init method of UserService using @PostConstruct
creating instance of UserService
init method of UserService using @PostConstruct
Spring Context started
Bean received from application context
name: Anil
May 18, 2017 5:26:19 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@433c675d: startup date [Thu May 18 17:26:18 IST 2017]; root of context hierarchy
destroy method of UserService using @PreDestroy
destroy method of UserService using @PreDestroy
Spring Context closed

No comments:

Post a Comment