Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#addBeanFactoryPostProcessor()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#addBeanFactoryPostProcessor() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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();
    }
 
Example 2
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();
    }
 
Example 3
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;
}
 
Example 4
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;
}
 
Example 5
Source Project: geekbang-lessons   File: BeanScopeDemo.java    License: Apache License 2.0 5 votes vote down vote up
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();
    }
 
Example 6
Source Project: ace   File: SpringContainer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void init(String... packages) {
    applicationContext = new AnnotationConfigApplicationContext();
    Config config= DefaultConfig.INSTANCE;
    ConfigBeanFactoryPostProcessor configBeanFactoryPostProcessor = new ConfigBeanFactoryPostProcessor(config);
    applicationContext.addBeanFactoryPostProcessor(configBeanFactoryPostProcessor);
    applicationContext.scan(packages);

}
 
Example 7
@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);
    }
}
 
Example 8
private static void addPostprocessorAndUpdateContext(Class<?> currentVerticleClass,
    AnnotationConfigApplicationContext annotationConfigApplicationContext) {
    annotationConfigApplicationContext.addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();
}
 
Example 9
public SpringContextSupport build(String[] packages) {

		applicationContext = new AnnotationConfigApplicationContext();

		applicationContext.scan(packages);

		applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurerWith(providedProperties));
		applicationContext.refresh();


		return this;
	}