CommonAnnotationBeanPostProcessor (@Resource @PostConstructor @PreDestroy)
AutowiredAnnotationBeanPostProcessor (@Autowired @Value)
GenericApplicationContext是一个干净的容器,它没有添加任何的PostProcessor处理器。
调用GenericApplicationContext.refresh()方法,会执行beanFactory后处理器,添加bean后处理器,初始化所有单例。
package com.xkj.org.a04;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
public class Bean1 {
private static final Logger log = LoggerFactory.getLogger(Bean1.class);
private Bean2 bean2;
@Autowired
public void setBean2(Bean2 bean2) {
log.debug("@Autowired 生效:{}", bean2);
this.bean2 = bean2;
}
private Bean3 bean3;
@Resource
public void setBean3(Bean3 bean3) {
log.debug("@Resource 生效:{}", bean3);
this.bean3 = bean3;
}
private String home;
@Autowired
private void setHome(@Value("${JAVA_HOME}") String home) {
log.debug("@Value 生效:{}", home);
this.home = home;
}
@PostConstruct
public void init() {
log.debug("@PostConstruct 生效");
}
@PreDestroy
public void destroy() {
log.debug("@PreDestroy 生效");
}
@Override
public String toString() {
return "Bean1{" +
"bean2=" + bean2 +
", bean3=" + bean3 +
", home='" + home + '\'' +
'}';
}
}
package com.xkj.org.a04;
public class Bean2 {
}
package com.xkj.org.a04;
public class Bean3 {
}
package com.xkj.org.a04;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;
public class A04AppllicationContext {
public static void main(String[] args) {
GenericApplicationContext context = new GenericApplicationContext();
//注册三个bean
context.registerBean("bean1", Bean1.class);
context.registerBean("bean2", Bean2.class);
context.registerBean("bean3", Bean3.class);
context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
//@Autowired @Value
context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
//@Resource @PostConstruct @PreDestroy
context.registerBean(CommonAnnotationBeanPostProcessor.class);
//执行beanFactory后置处理器,添加bean后处理器,初始化所有单例
context.refresh();
context.close();
}
}
ConfigurationPropertiesBindingPostProcessor
package com.xkj.org.a04;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "java")
public class Bean4 {
private String home;
private String version;
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "Bean4{" +
"home='" + home + '\'' +
", version='" + version + '\'' +
'}';
}
}
package com.xkj.org.a04;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;
public class A04AppllicationContext {
public static void main(String[] args) {
GenericApplicationContext context = new GenericApplicationContext();
//注册三个bean
context.registerBean("bean1", Bean1.class);
context.registerBean("bean2", Bean2.class);
context.registerBean("bean3", Bean3.class);
context.registerBean("bean4", Bean4.class);
context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
//@Autowired @Value
context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
//@Resource @PostConstruct @PreDestroy
context.registerBean(CommonAnnotationBeanPostProcessor.class);
//ConfigurationProperties
ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());
//执行beanFactory后置处理器,添加bean后处理器,初始化所有单例
context.refresh();
System.out.println("bean4="+context.getBean(Bean4.class));
context.close();
}
}