protected void setUp() throws Exception { bundleContext = new MockBundleContext(); applicationContext = new GenericApplicationContext(); applicationContext.setClassLoader(getClass().getClassLoader()); applicationContext.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); applicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); } }); reader = new XmlBeanDefinitionReader(applicationContext); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(getConfig(), getClass())); applicationContext.refresh(); blueprintContainer = new SpringBlueprintContainer(applicationContext); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); } }); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); beanFactory.registerSingleton("blueprintContainer", new SpringBlueprintContainer(context)); } }); reader = new XmlBeanDefinitionReader(context); reader.setDocumentLoader(new PublicBlueprintDocumentLoader()); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); blueprintContainer = new SpringBlueprintContainer(context); }
@Bean public BeanFactoryPostProcessor beanFactoryPostProcessor(ApplicationContext applicationContext) { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (beanFactory instanceof BeanDefinitionRegistry) { try { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry); scanner.setResourceLoader(applicationContext); scanner.scan("com.quancheng.saluki.boot.web"); } catch (Throwable e) { log.error(e.getMessage(), e); } } } }; }
public static ApplicationContext buildAppContext(ApplicationContext parent, Stream<TestBean> beans, Collection<BeanFactoryPostProcessor> postProcessors) { final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); final GenericApplicationContext applicationContext = new GenericApplicationContext(beanFactory, parent); postProcessors.forEach(applicationContext::addBeanFactoryPostProcessor); beans.forEach(entry -> { final String factoryBean = entry.getName() + "_factory"; beanFactory.registerSingleton(factoryBean, (Supplier<Object>) entry::getBean); beanFactory.registerBeanDefinition(entry.getName(), BeanDefinitionBuilder .rootBeanDefinition(entry.getBean() != null ? entry.getBean().getClass() : Object.class) .setFactoryMethodOnBean("get", factoryBean) .getBeanDefinition()); }); applicationContext.refresh(); return applicationContext; }
@Test public void testFactoryBeansWithAutowiring() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT); BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer"); ppc.postProcessBeanFactory(factory); assertNull(factory.getType("betaFactory")); Alpha alpha = (Alpha) factory.getBean("alpha"); Beta beta = (Beta) factory.getBean("beta"); Gamma gamma = (Gamma) factory.getBean("gamma"); Gamma gamma2 = (Gamma) factory.getBean("gammaFactory"); assertSame(beta, alpha.getBeta()); assertSame(gamma, beta.getGamma()); assertSame(gamma2, beta.getGamma()); assertEquals("yourName", beta.getName()); }
protected WebApplicationContext applicationContext() { if (applicationContext == null) { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); Set<Class<?>> annotatedClasses = annotatedClasses(); if (annotatedClasses != null) { annotatedClasses.iterator().forEachRemaining(webApplicationContext::register); } Set<? extends BeanFactoryPostProcessor> beanFactoryPostProcessors = beanFactoryPostProcessors(); if (beanFactoryPostProcessors != null) { beanFactoryPostProcessors.iterator().forEachRemaining(webApplicationContext::addBeanFactoryPostProcessor); } if (this.rootApplicationContext != null) { webApplicationContext.setParent(this.rootApplicationContext); webApplicationContext.setEnvironment((ConfigurableEnvironment) rootApplicationContext.getEnvironment()); } applicationContext = webApplicationContext; } return (WebApplicationContext) applicationContext; }
@Bean @ConditionalOnProperty(value = "jsf.scope-configurer.jsf.enabled", havingValue = "true", matchIfMissing = true) public static BeanFactoryPostProcessor jsfScopeAnnotationsConfigurer(Environment environment) { CustomScopeAnnotationConfigurer scopeAnnotationConfigurer = new CustomScopeAnnotationConfigurer(); scopeAnnotationConfigurer.setOrder(environment.getProperty("jsf.scope-configurer.jsf.order", Integer.class, Ordered.LOWEST_PRECEDENCE)); scopeAnnotationConfigurer.addMapping(NoneScoped.class, ConfigurableBeanFactory.SCOPE_PROTOTYPE); scopeAnnotationConfigurer.addMapping(RequestScoped.class, WebApplicationContext.SCOPE_REQUEST); scopeAnnotationConfigurer.addMapping(javax.faces.bean.ViewScoped.class, ViewScope.SCOPE_VIEW); scopeAnnotationConfigurer.addMapping(javax.faces.view.ViewScoped.class, ViewScope.SCOPE_VIEW); scopeAnnotationConfigurer.addMapping(SessionScoped.class, WebApplicationContext.SCOPE_SESSION); scopeAnnotationConfigurer.addMapping(ApplicationScoped.class, WebApplicationContext.SCOPE_APPLICATION); return scopeAnnotationConfigurer; }
private void testServletContextListener(ServletContext servletContext) throws Exception { ResteasyAutoConfiguration resteasyAutoConfiguration = new ResteasyAutoConfiguration(); BeanFactoryPostProcessor beanFactoryPostProcessor = ResteasyAutoConfiguration.springBeanProcessor(); ServletContextListener servletContextListener = resteasyAutoConfiguration.resteasyBootstrapListener(beanFactoryPostProcessor); Assert.assertNotNull(servletContextListener); ServletContextEvent sce = new ServletContextEvent(servletContext); servletContextListener.contextInitialized(sce); ResteasyProviderFactory servletContextProviderFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName()); Dispatcher servletContextDispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName()); Registry servletContextRegistry = (Registry) servletContext.getAttribute(Registry.class.getName()); Assert.assertNotNull(servletContextProviderFactory); Assert.assertNotNull(servletContextDispatcher); Assert.assertNotNull(servletContextRegistry); // Exercising fully cobertura branch coverage servletContextListener.contextDestroyed(sce); ServletContextListener servletContextListener2 = resteasyAutoConfiguration.resteasyBootstrapListener(beanFactoryPostProcessor); servletContextListener2.contextDestroyed(sce); }
@Test public void testFactoryBeansWithAutowiring() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT); BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer"); ppc.postProcessBeanFactory(factory); Alpha alpha = (Alpha) factory.getBean("alpha"); Beta beta = (Beta) factory.getBean("beta"); Gamma gamma = (Gamma) factory.getBean("gamma"); Gamma gamma2 = (Gamma) factory.getBean("gammaFactory"); assertSame(beta, alpha.getBeta()); assertSame(gamma, beta.getGamma()); assertSame(gamma2, beta.getGamma()); assertEquals("yourName", beta.getName()); }
@Test public void parseInternal_singleElementDefined_beanDefinitionCreated() throws Exception { //Arrange HttpServer httpServer = MetaDataServer.setupHttpServer(); HttpContext instanceIdHttpContext = httpServer.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("testInstanceId")); DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass())); //Assert BeanFactoryPostProcessor postProcessor = beanFactory.getBean("AmazonEc2InstanceDataPropertySourcePostProcessor", BeanFactoryPostProcessor.class); assertNotNull(postProcessor); assertEquals(1, beanFactory.getBeanDefinitionCount()); httpServer.removeContext(instanceIdHttpContext); }
/** * Overrides properties configured on beans. */ @Bean() @Lazy(false) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public static BeanFactoryPostProcessor propertyOverrideConfigurer() { PropertyOverrideConfigurer theOverrideConfigurer = new PropertyOverrideConfigurer(); final Properties thePropertiesHolder = new Properties(); /* Task refresh interval. */ thePropertiesHolder.put("starterService.taskReschedulingCronExpression", "0/20 * * * * ?"); /* Transport service configuration refresh interval. */ thePropertiesHolder.put("starterService.transportServiceConfigurationRefreshCronExpression", "0/30 * * * * ?"); /* Task execution status reports cleanup interval. */ thePropertiesHolder.put("starterService.taskExecutionStatusCleanupCronExpression", "0/30 0/15 * * * ?"); theOverrideConfigurer.setProperties(thePropertiesHolder); theOverrideConfigurer.setIgnoreInvalidKeys(false); theOverrideConfigurer.setIgnoreResourceNotFound(false); theOverrideConfigurer.setOrder(0); return theOverrideConfigurer; }
/** * Overrides properties configured on beans. */ @Bean() @Lazy(false) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public static BeanFactoryPostProcessor propertyOverrideConfigurer() { PropertyOverrideConfigurer theOverrideConfigurer = new PropertyOverrideConfigurer(); final Properties thePropertiesHolder = new Properties(); /* Task refresh interval. */ thePropertiesHolder.put("starterService.taskReschedulingCronExpression", "* 4/30 * * * ?"); /* Transport service configuration refresh interval. */ thePropertiesHolder.put("starterService.transportServiceConfigurationRefreshCronExpression", "* 5/30 * * * ?"); /* Task execution status reports cleanup interval. */ thePropertiesHolder.put("starterService.taskExecutionStatusCleanupCronExpression", "0/5 * * * * ?"); theOverrideConfigurer.setProperties(thePropertiesHolder); theOverrideConfigurer.setIgnoreInvalidKeys(false); theOverrideConfigurer.setIgnoreResourceNotFound(false); theOverrideConfigurer.setOrder(0); return theOverrideConfigurer; }
public BlueprintContainerProcessor(EventAdminDispatcher dispatcher, BlueprintListenerManager listenerManager, Bundle extenderBundle) { this.dispatcher = dispatcher; this.listenerManager = listenerManager; this.extenderBundle = extenderBundle; Class<?> processorClass = ClassUtils.resolveClassName( "org.eclipse.gemini.blueprint.blueprint.container.support.internal.config.CycleOrderingProcessor", BundleContextAware.class.getClassLoader()); cycleBreaker = (BeanFactoryPostProcessor) BeanUtils.instantiate(processorClass); }
/** * Invoke the given BeanFactoryPostProcessor beans. */ private static void invokeBeanFactoryPostProcessors( Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) { for (BeanFactoryPostProcessor postProcessor : postProcessors) { postProcessor.postProcessBeanFactory(beanFactory); } }
protected void setUp() throws Exception { bundleContext = new MockBundleContext(); context = new GenericApplicationContext(); context.setClassLoader(getClass().getClassLoader()); context.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext)); context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar()); } }); reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(new ClassPathResource(CONFIG, getClass())); context.refresh(); BlueprintContainer = new SpringBlueprintContainer(context); }
public BeanFactoryPostProcessor beanFactoryPostProcessor() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { BeanDefinition bd = beanFactory.getBeanDefinition("beanPostProcessor"); bd.getPropertyValues().addPropertyValue("nameSuffix", "-processed-" + myProp); } }; }
@Bean public BeanFactoryPostProcessor bfpp() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // no-op } }; }
@Bean public static final BeanFactoryPostProcessor bfpp() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // no-op } }; }
@Test public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT); BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer"); ppc.postProcessBeanFactory(factory); Beta beta = (Beta) factory.getBean("beta"); Alpha alpha = (Alpha) factory.getBean("alpha"); Gamma gamma = (Gamma) factory.getBean("gamma"); assertSame(beta, alpha.getBeta()); assertSame(gamma, beta.getGamma()); }
@Bean @ConditionalOnProperty(value = "jsf.scope-configurer.cdi.enabled", havingValue = "true", matchIfMissing = true) public static BeanFactoryPostProcessor cdiScopeAnnotationsConfigurer(Environment environment) { CustomScopeAnnotationConfigurer scopeAnnotationConfigurer = new CustomScopeAnnotationConfigurer(); scopeAnnotationConfigurer.setOrder(environment.getProperty("jsf.scope-configurer.cdi.order", Integer.class, Ordered.LOWEST_PRECEDENCE)); scopeAnnotationConfigurer.addMapping(RequestScoped.class, WebApplicationContext.SCOPE_REQUEST); scopeAnnotationConfigurer.addMapping(SessionScoped.class, WebApplicationContext.SCOPE_SESSION); scopeAnnotationConfigurer.addMapping(ConversationScoped.class, WebApplicationContext.SCOPE_SESSION); scopeAnnotationConfigurer.addMapping(ApplicationScoped.class, WebApplicationContext.SCOPE_APPLICATION); return scopeAnnotationConfigurer; }
@Test public void testRegisteredScopeView() { ConfigurableListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanFactoryPostProcessor beanFactoryPostProcessor = ViewScopeAutoConfiguration.viewScopeConfigurer(); beanFactoryPostProcessor.postProcessBeanFactory(beanFactory); assertThat(beanFactory.getRegisteredScope(ViewScope.SCOPE_VIEW)) .isInstanceOf(ViewScope.class); }
@Bean public BeanFactoryPostProcessor beanFactoryPostProcessor() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { doMapping("os.model", "", mockProcessor("Windows"), beanFactory); doMapping("browser.model", "", mockProcessor("Chrome"), beanFactory); doMapping("browser.type", "", mockProcessor("Desktop"), beanFactory); doMapping("browser.version", "", mockProcessor(49), beanFactory); } }; }
@Bean public static BeanFactoryPostProcessor entityScanPackagesPostProcessor() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (beanFactory instanceof BeanDefinitionRegistry) { EntityScanPackages.register((BeanDefinitionRegistry) beanFactory, Collections.singletonList(Schema.class.getPackage().getName())); } } }; }
/** * Add bean-created post processors. * @param beanFactory */ public void invokePostProcessorCreators(ConfigurableListableBeanFactory beanFactory) { String[] postProcessorCreatorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessorCreator.class, false, false); for (int i = 0; i < postProcessorCreatorNames.length; i++) { BeanFactoryPostProcessorCreator postProcessorCreator = (BeanFactoryPostProcessorCreator)beanFactory.getBean(postProcessorCreatorNames[i]); for (BeanFactoryPostProcessor beanFactoryPostProcessor : postProcessorCreator.getBeanFactoryPostProcessors()) { addBeanFactoryPostProcessor(beanFactoryPostProcessor); } } }
@Override public BeanFactoryPostProcessor getBindings(LogInject<_Logger_> logInject) { return new BeanFactoryPostProcessor() { ThreadLocal<Object> injectee = new ThreadLocal<>(); @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)beanFactory; AutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver() { @Override public Object getSuggestedValue(DependencyDescriptor descriptor) { if (descriptor.getDependencyType().equals(logInject.getLoggerClass())) { return logInject.createLogger(injectee.get().getClass()); } return null; } }; AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor() { @Override public PropertyValues postProcessPropertyValues(PropertyValues values, PropertyDescriptor[] descriptors, Object bean, String beanName) throws BeansException { injectee.set(bean); return super.postProcessPropertyValues(values, descriptors, bean, beanName); } }; beanPostProcessor.setBeanFactory(defaultListableBeanFactory); defaultListableBeanFactory.addBeanPostProcessor(beanPostProcessor); defaultListableBeanFactory.setAutowireCandidateResolver(resolver); } }; }
/** * Instantiate the WebApplicationContext for the ActionServlet, either a default * XmlWebApplicationContext or a custom context class if set. * <p>This implementation expects custom contexts to implement ConfigurableWebApplicationContext. * Can be overridden in subclasses. * @throws org.springframework.beans.BeansException if the context couldn't be initialized * @see #setContextClass * @see org.springframework.web.context.support.XmlWebApplicationContext */ protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException { if (logger.isDebugEnabled()) { logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '" + getModulePrefix() + "' will try to create custom WebApplicationContext " + "context of class '" + getContextClass().getName() + "', using parent context [" + parent + "]"); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) { throw new ApplicationContextException( "Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '" + getModulePrefix() + "': custom WebApplicationContext class [" + getContextClass().getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass()); wac.setParent(parent); wac.setServletContext(getServletContext()); wac.setNamespace(getNamespace()); if (getContextConfigLocation() != null) { wac.setConfigLocations( StringUtils.tokenizeToStringArray( getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); } wac.addBeanFactoryPostProcessor( new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet())); beanFactory.ignoreDependencyType(ActionServlet.class); } } ); wac.refresh(); return wac; }
/** * 继承上级上下文中的属性配置器 * * @param inheriting 上级上下文 * @param context 当前上下文 */ public static void inheritParentProperties(ApplicationContext inheriting, GenericApplicationContext context) { if (!(inheriting instanceof AbstractApplicationContext)) return; List<BeanFactoryPostProcessor> processors = ((AbstractApplicationContext) inheriting).getBeanFactoryPostProcessors(); for (BeanFactoryPostProcessor processor : processors) { if (processor instanceof PropertyResourceConfigurer) context.addBeanFactoryPostProcessor(processor); } }
/** * Invoke the given BeanFactoryPostProcessor beans. */ private void invokeBeanFactoryPostProcessors( Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) { for (BeanFactoryPostProcessor postProcessor : postProcessors) { postProcessor.postProcessBeanFactory(beanFactory); } }