8--deep use of spring--2...4 customizing lifecycle Behavior with @postconstruct and @predestroy

Source: Internet
Author: User
Tags call back

8.2.4 Customizing life cycle Behavior with @postconstruct and @predestroy

The @PostConstruct and @PreDestroy are also located under the Javax.annotation package, and are the two annotation,spring from the Java EE specification that draw on them directly, Used to customize the life cycle behavior of beans in a spring container.

@PostConstruct: Features similar to the Init-method attribute in the <bean.../> element. Used to modify the method to make it a bean initialization method;

@PreDestroy: Features similar to the Destroy-method attribute in the <bean.../> element. Used to modify the method to make it a method before the bean is destroyed.

 PackageEdu.pri.lime._8_2_4.bean.impl;Importorg.springframework.stereotype.Component;ImportEdu.pri.lime._8_2_4.bean. Axe;ImportEdu.pri.lime._8_2_4.bean. person; @Component Public classChineseImplementsPerson {@Resource (name= "Steelaxe")    PrivateAxe Axe; /*you can have spring call back the method after the bean's dependency injection is complete*/@PostConstruct Public voidinit () {System.out.println ("Initializing init method is executing ..."); }    /*you can have spring call back the method before destroying the bean .*/@PerDestroy Public voidClose () {System.out.println ("Close method before destroying is executed ..."); }     Public voidUseaxe () {System.out.println (Axe.chop ()); }}

La La la

La La la

8--deep use of spring--2...4 customizing lifecycle Behavior with @postconstruct and @predestroy

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.