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