org.springframework
spring-context
5.2.22.RELEASE
public class Color {
public void init(){
System.out.println("Color init");
}
public void destroy(){
System.out.println("Color destroy");
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeansConfig {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Color color(){
return new Color();
}
}
public static void main(String[] args) {
testXml();
testAnnotation();
}
public static void testXml(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
context.close();
}
public static void testAnnotation(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
context.close();
}
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Color implements InitializingBean , DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Color init");
}
@Override
public void destroy() throws Exception {
System.out.println("Color destroy");
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeansConfig {
@Bean
public Color color(){
return new Color();
}
}
public static void main(String[] args) {
testAnnotation();
}
public static void testAnnotation(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
context.close();
}
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Color {
@PostConstruct
public void init(){
System.out.println("Color init");
}
@PreDestroy
public void destroy(){
System.out.println("Color destroy");
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeansConfig {
@Bean
public Color color(){
return new Color();
}
}
public static void main(String[] args) {
testAnnotation();
}
public static void testAnnotation(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
context.close();
}
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Color {
@PostConstruct
public void init(){
System.out.println("Color init");
}
@PreDestroy
public void destroy(){
System.out.println("Color destroy");
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// bean初始化之前调用,initMehtod之前执行
System.out.println("postProcessBeforeInitialization : " + beanName + " :" + bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// bean初始化之后调用,initMehtod之后执行
System.out.println("postProcessAfterInitialization : " + beanName + " :" + bean);
return bean;
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeansConfig {
@Bean
public Color color(){
return new Color();
}
@Bean
public MyBeanPostProcessor myBeanPostProcessor(){
return new MyBeanPostProcessor();
}
}
public static void main(String[] args) {
testAnnotation();
}
public static void testAnnotation(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
context.close();
}
本章主要学习了,Spring Bean设置初始化方法和销毁方法。《参考资料》