Followers

springIOC: Bean Life Cycle(Java Based)

HelloWorld.java
================package myspringIOC;public class HelloWorld {    private String message;        public void init(){            System.out.println("I am going through Init");    }    public String getMessage() {        return message;    }    public void setMessage(String message) {       // System.out.println("I am setter....");        this.message = message;    }
        public void destroy(){            System.out.println("I am going through destroy");    }        }HelloWorldConfig.java=======================package myspringIOC;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class HelloWorldConfig {        @Bean(initMethod = "init",destroyMethod = "destroy")    public HelloWorld hello(){    return new HelloWorld();    }    }AnnotationTest.java====================package myspringIOC;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.AbstractApplicationContext;public class AnnotationTest {    public static void main(String[] args) {        AbstractApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);    HelloWorld helloWorld = context.getBean(HelloWorld.class);    helloWorld.setMessage("HI Bean how are you?");        System.out.println(helloWorld.getMessage());        context.registerShutdownHook();                    }        }


No comments:

Post a Comment