BeanFactoryPostProcessor
@FunctionalInterface
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
查看一下子类
案例1 修改容器中bean的属性
@Component
public class User {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
@Component
public class Config implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// BeanDefinition beanDefinition = beanFactory.getBeanDefinition("user");
// beanDefinition.getPropertyValues().add("name", "this is change userName");
}
}
@ComponentScan
public class Client {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Client.class);
context.refresh();
System.out.println(context.getBean(User.class));
}
}
User{name='this is change userName'}
BeanFactoryPostProcessor重要的实现类
BeanDefinitionRegistryPostProcessor
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
查看一下子类
案例1 使用BeanDefinitionRegistryPostProcessor注册BeanDefinition
@Component
public class Config implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
AbstractBeanDefinition userNameBdf = BeanDefinitionBuilder.
genericBeanDefinition(String.class).
addConstructorArgValue("this is name").
getBeanDefinition();
registry.registerBeanDefinition("userName", userNameBdf);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
@ComponentScan
public class Client {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Client.class);
context.refresh();
System.out.println(context.getBean("userName"));
}
}
案例2 定义2个BeanDefinitionRegistryPostProcessor实现Ordered接口
@Component
public class Config1 implements BeanDefinitionRegistryPostProcessor, Ordered {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("this is Config1 registry");
AbstractBeanDefinition nameBdf = BeanDefinitionBuilder.
genericBeanDefinition(String.class).
addConstructorArgValue("this is Config1").
getBeanDefinition();
registry.registerBeanDefinition("Config1", nameBdf);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public int getOrder() {
return 1;
}
}
@Component
public class Config2 implements BeanDefinitionRegistryPostProcessor, Ordered {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("this is Config2 registry");
AbstractBeanDefinition nameBdf = BeanDefinitionBuilder.
genericBeanDefinition(String.class).
addConstructorArgValue("this is Config2").
getBeanDefinition();
registry.registerBeanDefinition("Config2", nameBdf);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public int getOrder() {
return 2;
}
}
@ComponentScan
public class Client {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(Client.class);
context.refresh();
context.getBeansOfType(String.class).forEach((beanName, bean) -> {
System.out.println(String.format("%s->%s", beanName, bean));
});
}
}
this is Config1 registry
this is Config2 registry
多个注册优先级越小越先执行。
ConfigurationClassPostProcessor
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
PriorityOrdered, ResourceLoaderAware, ApplicationStartupAware, BeanClassLoaderAware,
EnvironmentAware {}