Spring implements operations prior to initializing and destroying beans in three different ways

Source: Internet
Author: User

There are three ways to define operations before the spring container initializes the bean and destroys it:

First: Operations performed prior to initializing and destroying beans through @postconstruct and @PreDestroy methods

The second is: by defining the Init-method and Destory-method methods in XML

The third is the implementation of the Initializingbean and Disposablebean interfaces via beans

The following shows the @PostConstruct and @PreDestory

1: Define the relevant implementation class:

[Java]View PlainCopy
  1. Package com.myapp.core.annotation.init;
  2. Import javax.annotation.PostConstruct;
  3. Import Javax.annotation.PreDestroy;
  4. Public class Personservice {
  5. private String message;
  6. Public String GetMessage () {
  7. return message;
  8. }
  9. public void Setmessage (String message) {
  10. this.message = message;
  11. }
  12. @PostConstruct
  13. public void init () {
  14. System.out.println ("I ' m init method using @PostConstrut ...." +message);
  15. }
  16. @PreDestroy
  17. public void Dostory () {
  18. System.out.println ("I ' m destory method using @PreDestroy ..." +message);
  19. }
  20. }


2: Define the relevant configuration file:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. Xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-3.1.xsd ">
  9. <!--<context:component-scan base-package= "com.myapp.core.jsr330"/>--
  10. <context:annotation-config />
  11. <Bean id= "personservice" class="Com.myapp.core.annotation.init.PersonService">
  12. < name="message" value="123"> </Property>
  13. </Bean>
  14. </Beans>


Where <context:annotation-config/> tells the spring container to use annotation configuration: Scan annotation configuration;

Test class:

[Java]View PlainCopy
  1. Package com.myapp.core.annotation.init;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. Public class Maintest {
  5. public static void Main (string[] args) {
  6. ApplicationContext context = new Classpathxmlapplicationcontext ("Resource/annotation.xml");
  7. Personservice Personservice = (personservice) context.getbean ("Personservice");
  8. Personservice.dostory ();
  9. }
  10. }


Test results:

I ' m init method using @PostConstrut .... 123
I ' m destory method using @PreDestroy ..... 123

The org.springframework.context.annotation.CommonAnnotationBeanPostProcessor can also be loaded by stating

class to tell the spring container how to use the Common annotation configuration:

You only need to modify the configuration file to:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. Xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-3.1.xsd ">
  9. <!--<context:component-scan base-package= "com.myapp.core.jsr330"/>--
  10. <!--<context:annotation-config/>--
  11. <Bean class="Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  12. <Bean id= "personservice" class="Com.myapp.core.annotation.init.PersonService">
  13. < name="message" value="123"> </Property>
  14. </Bean>
  15. </Beans>


The output of the above test can also be obtained.

Spring implements operations prior to initializing and destroying beans in three different ways

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.