@Bean(initMethod = "afterPropertiesSet", destroyMethod = "destroy")
public Color color(){
return new Color();
}
方法三
实现InitializingBean, DisposableBean接口
public class Color implements InitializingBean, DisposableBean {
public Color() {
System.out.println("构造方法……");
}
public void destroy() throws Exception {
System.out.println("销毁方法……");
}
public void afterPropertiesSet() throws Exception {
System.out.println("属性赋值后……");
}
}
方法四
使用注解@PostConstruct、@PreDestroy
public class Red {
public Red() {
System.out.println("red 构建……");
}
@PostConstruct
public void init () {
System.out.println("postConstruct……");
}
@PreDestroy
public void destroy(){
System.out.println("destroy……");
}
}
方法五
实现BeanPostProcessor接口
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "---------------postProcessBeforeInitialization……");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "---------------postProcessAfterInitialization……");
return bean;
}
}