Spring supports @postconstruct and @predestroy annotations from 2.5 onwards. They are functionally equivalent to Init-method and Destroy-method, but in one bean, multiple @postconstruct and @predestroy can be defined.
in my personal opinion, I do not want my team to use these methods extensively in the code . These methods always make people feel that code is operating in a jumping way, rather than based on conventional logic.
Vulnerabilities and unexpected methods always appear in such configurations ....
- Use multiple postconstruct and Predestroy source code
Driver PackageCom.spring.bean;ImportOrg.springframework.stereotype.Component;/** * A simple bean configured with annotations, driver * @author Fan Fangming * * */@Component Public class people { PrivateString name; PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; }} Car PackageCom.spring.bean;ImportJavax.annotation.PostConstruct;ImportJavax.annotation.PreDestroy;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Component;/** * A simple bean configured with annotations * @author Fan Fangming * * * /@Component Public class Car { PrivatePeople people; Public Car() {System.out.println ("Car: default constructor"); } PublicPeopleGetPeople() {returnPeople; }@Autowired Public void Setpeople(People people) {System.out.println ("--@Autowired parameter injection"); This. People = people; }@PostConstruct Private void Ini1() {System.out.println ("--run ini1."); }@PostConstruct Private void Ini2() {System.out.println ("--run ini2."); }@PreDestroy Private void Destory1() {System.out.println ("--run destory1."); }@PreDestroy Private void Destory2() {System.out.println ("--run Destory2."); }@Override PublicStringtoString(){return "Role class is called:"+ This. People.getname (); }}
- Simulate container startup and shutdown
package com.spring.bean;< Span class= "Hljs-keyword" >import org.springframework.context.ApplicationContext; import org.springframework.context.support.classpathxmlapplicationcontext;< Span class= "Hljs-keyword" >public class easyspringcontext { public static void main (string[] args) {//simulation boot container applicationcontext ctx = new Classpathxmlapplicationcontext ( "bean.xml" ); //simulation container off ((Classpathxmlapplicationcontext) CTX). Destroy (); }}
Configure Bean.xml
<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns="Http://www.springframework.org/schema/beans" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" Xmlns:context="Http://www.springframework.org/schema/context" Xmlns:tx="Http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:task="Http://www.springframework.org/schema/task" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3 .0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-co Ntext-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3. 0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "> <context:component-scan base-package="Com.spring.bean"/> </Beans>
Car: Default constructor
– @Autowired parameter Injection
– Run Ini1.
– Run Ini2.
– Run Destory1.
– Run Destory2.
Good memory is better than bad writing 91-spring3 study (-bean) postconstruct and Predestroy