目录
- 一、作用
- 二、常见的bean后处理器
- 2.1 AutowiredAnnotationBeanPostProcessor
- 2.1.1 说明
- 2.1.2 代码示例
- 2.1.3 截图示例
- 2.2 CommonAnnotationBeanPostProcessor
- 2.2.1 说明
- 2.2.2 代码示例
- 2.2.3 截图示例
- 2.3 ConfigurationPropertiesBindingPostProcessor
- 2.3.1 说明
- 2.3.2 代码示例
- 2.3.3 截图示例
一、作用
- 1.为bean生命周期的各个阶段提供扩展
- 2.实例化前、实例化后、依赖注入阶段、初始化前、初始化后、销毁之前提供扩展
二、常见的bean后处理器
2.1 AutowiredAnnotationBeanPostProcessor
2.1.1 说明
- 1. 用来解析@Autowired @Value注解
2.1.2 代码示例
package com.learning.bean_processor;
public class Dog {
}
package com.learning.bean_processor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
public class Person {
private Dog dog;
@Autowired
public void setDog(Dog dog) {
System.out.println("@Autowired生效:" + dog);
this.dog = dog;
}
private String home;
@Autowired
public void setHome(@Value("${JAVA_HOME}") String home){
System.out.println("@Value生效:" + home);
this.home = home;
}
}
package com.learning.bean_processor;
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;
/**
* Bean后处理器
*/
public class BeanProcessorTest {
public static void main(String[] args) {
GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
// 用原始方法注册三个Bean
genericApplicationContext.registerBean("person", Person.class);
genericApplicationContext.registerBean("dog", Dog.class);
genericApplicationContext.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
// 解析@Autowired @Value注解
genericApplicationContext.registerBean(AutowiredAnnotationBeanPostProcessor.class);
// 初始化容器
// 执行beanFactory后处理器,添加bean后处理器,初始化所有单例
genericApplicationContext.refresh();
// 销毁容器
genericApplicationContext.close();
}
}
2.1.3 截图示例
2.2 CommonAnnotationBeanPostProcessor
2.2.1 说明
- 1. 解析@Resource @PostConstruct @PreDestroy
2.2.2 代码示例
package com.learning.bean_processor;
public class Cat {
}
package com.learning.bean_processor;
import javax.annotation.Resource;
public class Person {
private Cat cat;
@Resource
public void setCat(Cat cat) {
System.out.println("@Resource:" + cat);
this.cat = cat;
}
}
package com.learning.bean_processor;
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;
/**
* Bean后处理器
*/
public class BeanProcessorTest {
public static void main(String[] args) {
GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
// 用原始方法注册三个Bean
genericApplicationContext.registerBean("person", Person.class);
genericApplicationContext.registerBean("cat", Cat.class);
// 解析@Resource @PostConstruct @PreDestroy
genericApplicationContext.registerBean(CommonAnnotationBeanPostProcessor.class);
// 初始化容器
// 执行beanFactory后处理器,添加bean后处理器,初始化所有单例
genericApplicationContext.refresh();
// 销毁容器
genericApplicationContext.close();
}
}
2.2.3 截图示例
2.3 ConfigurationPropertiesBindingPostProcessor
2.3.1 说明
- 1. springboot中配置文件属性绑定bean的增强
- 2. 需要引用springboot的包
2.3.2 代码示例
package com.learning.bean_processor;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "java")
public class JavaProperties {
private String home;
private String version;
}
package com.learning.bean_processor;
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;
/**
* Bean后处理器
*/
public class BeanProcessorTest {
public static void main(String[] args) {
GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
// 把springboot中的ConfigurationPropertiesBindingPostProcessor后处理器加到容器中
// 解析@ConfigurationProperties
ConfigurationPropertiesBindingPostProcessor.register(genericApplicationContext.getDefaultListableBeanFactory());
genericApplicationContext.registerBean("java_home", JavaProperties.class);
// 初始化容器
// 执行beanFactory后处理器,添加bean后处理器,初始化所有单例
genericApplicationContext.refresh();
System.out.println(genericApplicationContext.getBean(JavaProperties.class));
// 销毁容器
genericApplicationContext.close();
}
}
2.3.3 截图示例