下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#addBeanFactoryPostProcessor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
// 创建 BeanFactory 容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 注册 Configuration Class(配置类) -> Spring Bean
applicationContext.register(ThreadLocalScopeDemo.class);
applicationContext.addBeanFactoryPostProcessor(beanFactory -> {
// 注册自定义 scope
beanFactory.registerScope(ThreadLocalScope.SCOPE_NAME, new ThreadLocalScope());
});
// 启动 Spring 应用上下文
applicationContext.refresh();
scopedBeansByLookup(applicationContext);
// 关闭 Spring 应用上下文
applicationContext.close();
}
public static void main(String[] args) {
// 创建 BeanFactory 容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 注册 Configuration Class(配置类) -> Spring Bean
applicationContext.register(ResolvableDependencySourceDemo.class);
applicationContext.addBeanFactoryPostProcessor(beanFactory -> {
// 注册 Resolvable Dependency
beanFactory.registerResolvableDependency(String.class, "Hello,World");
});
// 启动 Spring 应用上下文
applicationContext.refresh();
// 显示地关闭 Spring 应用上下文
applicationContext.close();
}
AbstractApplicationContext createApplicationContext(Domain domain) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(gatewayApplicationContext);
context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
context.addBeanFactoryPostProcessor(configurer);
context.getBeanFactory().registerSingleton("domain", domain);
context.register(HandlerConfiguration.class);
context.setId("context-domain-" + domain.getId());
context.refresh();
return context;
}
AbstractApplicationContext createApplicationContext(Api api) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(gatewayApplicationContext);
context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
context.addBeanFactoryPostProcessor(configurer);
context.getBeanFactory().registerSingleton("api", api);
context.register(ApiHandlerConfiguration.class);
context.setId("context-api-" + api.getId());
context.refresh();
return context;
}
public static void main(String[] args) {
// 创建 BeanFactory 容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 注册 Configuration Class(配置类) -> Spring Bean
applicationContext.register(BeanScopeDemo.class);
applicationContext.addBeanFactoryPostProcessor(beanFactory -> {
beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.printf("%s Bean 名称:%s 在初始化后回调...%n", bean.getClass().getName(), beanName);
return bean;
}
});
});
// 启动 Spring 应用上下文
applicationContext.refresh();
// 结论一:
// Singleton Bean 无论依赖查找还是依赖注入,均为同一个对象
// Prototype Bean 无论依赖查找还是依赖注入,均为新生成的对象
// 结论二:
// 如果依赖注入集合类型的对象,Singleton Bean 和 Prototype Bean 均会存在一个
// Prototype Bean 有别于其他地方的依赖注入 Prototype Bean
// 结论三:
// 无论是 Singleton 还是 Prototype Bean 均会执行初始化方法回调
// 不过仅 Singleton Bean 会执行销毁方法回调
scopedBeansByLookup(applicationContext);
scopedBeansByInjection(applicationContext);
// 显示地关闭 Spring 应用上下文
applicationContext.close();
}
@Override
public void init(String... packages) {
applicationContext = new AnnotationConfigApplicationContext();
Config config= DefaultConfig.INSTANCE;
ConfigBeanFactoryPostProcessor configBeanFactoryPostProcessor = new ConfigBeanFactoryPostProcessor(config);
applicationContext.addBeanFactoryPostProcessor(configBeanFactoryPostProcessor);
applicationContext.scan(packages);
}
@Override
public ProtocolProvider create(String type, ApplicationContext parentContext) {
logger.debug("Looking for an protocol provider for [{}]", type);
Protocol protocol = protocols.get(type);
if (protocol != null) {
try {
ProtocolProvider protocolProvider = createInstance(protocol.protocolProvider());
Plugin plugin = protocolPlugins.get(protocol);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(parentContext);
context.setClassLoader(pluginClassLoaderFactory.getOrCreateClassLoader(plugin));
context.setEnvironment((ConfigurableEnvironment) parentContext.getEnvironment());
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setEnvironment(parentContext.getEnvironment());
context.addBeanFactoryPostProcessor(configurer);
context.register(protocol.configuration());
context.registerBeanDefinition(plugin.clazz(), BeanDefinitionBuilder.rootBeanDefinition(plugin.clazz()).getBeanDefinition());
context.refresh();
context.getAutowireCapableBeanFactory().autowireBean(protocolProvider);
if (protocolProvider instanceof InitializingBean) {
((InitializingBean) protocolProvider).afterPropertiesSet();
}
return protocolProvider;
} catch (Exception ex) {
logger.error("An unexpected error occurs while loading protocol", ex);
return null;
}
} else {
logger.error("No protocol provider is registered for type {}", type);
throw new IllegalStateException("No protocol provider is registered for type " + type);
}
}
private static void addPostprocessorAndUpdateContext(Class<?> currentVerticleClass,
AnnotationConfigApplicationContext annotationConfigApplicationContext) {
annotationConfigApplicationContext.addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
annotationConfigApplicationContext.refresh();
annotationConfigApplicationContext.start();
annotationConfigApplicationContext.registerShutdownHook();
}
public SpringContextSupport build(String[] packages) {
applicationContext = new AnnotationConfigApplicationContext();
applicationContext.scan(packages);
applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurerWith(providedProperties));
applicationContext.refresh();
return this;
}