spring容器中bean的初始化方式大体有三种:
@PostConstruct ,@PreDestroy: 从Java EE5规范开始,Servlet中增加了两个影响Servlet生命周期的注解,@PostConstruct和@PreDestroy。@PostConstruct会在Servlet构造函数之后,初始化之前执行
package com.edu.bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/@Componentpublic classColor {
@PostConstructpublic voidinit(){
System.out.println("init......");
}
@PreDestroypublic voiddestroy(){
System.out.println("destroy.........");
}
}
package com.edu.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/
public classTest1 {public static voidmain(String[] args) {
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext("com.edu.bean");
context.close();
}
}
输出:
init......
destroy.........
@Bean中指定initMethod,destroyMethod方法
package com.edu.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/
public classColor {
@PostConstructpublic voidinit(){
System.out.println("init......");
}public voidinit1(){
System.out.println("init1......");
}
@PreDestroypublic voiddestroy(){
System.out.println("destroy.........");
}public voiddestroy1(){
System.out.println("destroy1.........");
}
}
package com.edu.bean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/@ConfigurationclassConfig {
@Bean(name= "color",initMethod = "init1",destroyMethod = "destroy1")publicColor getColor(){return newColor();
}
}
package com.edu.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/
public classTest1 {public static voidmain(String[] args) {
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(Config.class);
context.close();
}
}
输出:
init......
init1......
destroy.........
destroy1.........
证明@PostConstruct 修饰的方法是在initMethod标注的方法之前执行,@PreDestroy修饰的方法是在destroyMethod 标注的方法之前执行
interface InitializingBean,DisposableBean:重写afterPropertiesSet,destroy方法
package com.edu.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/@Componentpublic classColor implements InitializingBean, DisposableBean {
@PostConstructpublic voidinit(){
System.out.println("init......");
}
@PreDestroypublic voiddestroy(){
System.out.println("destroy.........");
}
@Overridepublic voidafterPropertiesSet() throws Exception {
System.out.println("init1.........");
}
@Overridepublic voiddestroy() throws Exception {
System.out.println("destroy1......");
}
}
package com.edu.bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/**
* Created by IntelliJ IDEA.
* User: chenzhubing
* Date: 2019/6/17*/
public classTest1 {public static voidmain(String[] args) {
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext("com.edu.bean");
context.close();
}
}
输出:
init......
init1.........
destroy.........
destroy1......