Spring (3.2.3), spring3.2.3

Source: Internet
Author: User

Spring (3.2.3), spring3.2.3

Spring containers can manage the lifecycle of beans in the singleton scope. containers can track the creation and destruction of Bean instances. There are two main opportunities to manage Bean lifecycle behaviors:
After Bean dependency Injection
Between beans to be destroyed

 

Behavior after dependency Injection

There are three ways to execute specific actions after all Bean attributes are set successfully:
Implement the org. springframework. beans. factory. InitializingBean Interface
Use the init-method attribute
Use the @ PostConstruct Annotation

Example of InitializingBean interface implementation

Bean definition:

public class ExampleBean implements InitializingBean {        private String field1;    private String field2;        public void setField1(String field1) {        this.field1 = field1;        System.out.println("field1 was set.");    }        public void setField2(String field2) {        this.field1 = field2;        System.out.println("field2 was set.");    }        public ExampleBean() {        System.out.println("In ExampleBean Constructor.");    }    public void afterPropertiesSet() throws Exception {        System.out.println("All properties were set.");    }    }

Spring Configuration:

<bean id="eb" class="com.huey.dream.bean.ExampleBean">    <property name="field1" value=""/>    <property name="field2" value=""/></bean>

Test method:

@Testpublic void testLifecycle() throws Exception {    ApplicationContext appCtx =           new ClassPathXmlApplicationContext("applicationContext.xml");}

Result output:

In ExampleBean Constructor.field1 was set.field2 was set.All properties were set.
Example of using the init-method attribute

Bean definition:

public class ExampleBean  {        private String field1;    private String field2;        public void setField1(String field1) {        this.field1 = field1;        System.out.println("field1 was set.");    }        public void setField2(String field2) {        this.field1 = field2;        System.out.println("field2 was set.");    }        public ExampleBean() {        System.out.println("In ExampleBean Constructor.");    }    public void init() throws Exception {        System.out.println("In init method.");    }    }

Spring Configuration:

<bean id="eb" class="com.huey.dream.bean.ExampleBean" init-method="init">    <property name="field1" value=""/>    <property name="field2" value=""/></bean>
Use the @ PostConstruct Annotation

Bean definition:

public class ExampleBean  {        private String field1;    private String field2;        public void setField1(String field1) {        this.field1 = field1;        System.out.println("field1 was set.");    }        public void setField2(String field2) {        this.field1 = field2;        System.out.println("field2 was set.");    }        public ExampleBean() {        System.out.println("In ExampleBean Constructor.");    }        @PostConstruct    public void init() throws Exception {        System.out.println("In init method.");    }    }

Spring Configuration:

<bean id="eb" class="com.huey.dream.bean.ExampleBean">    <property name="field1" value=""/>    <property name="field2" value=""/></bean>

 

Behavior before Bean destruction

Similar to custom initialization, there are also three methods to execute a specific behavior before the Bean instance is destroyed:
Implement the org. springframework. beans. factory. DisposableBean Interface
Use the destroy-method attribute
Use the @ PreDestroy Annotation

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.