/** * register bean in spring ioc. * @param beanName bean name * @param obj bean */ public void registerBean(final String beanName, final Object obj) { cfgContext.getBeanFactory().registerSingleton(beanName, obj); }
/** * Apply the given property value to the corresponding bean. */ protected void applyPropertyValue( ConfigurableListableBeanFactory factory, String beanName, String property, String value) { BeanDefinition bd = factory.getBeanDefinition(beanName); BeanDefinition bdToUse = bd; while (bd != null) { bdToUse = bd; bd = bd.getOriginatingBeanDefinition(); } PropertyValue pv = new PropertyValue(property, value); pv.setOptional(this.ignoreInvalidKeys); bdToUse.getPropertyValues().addPropertyValue(pv); }
/** * Finish the initialization of this context's bean factory, * initializing all remaining singleton beans. */ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons(); }
@Override protected boolean isEligibleAdvisorBean(String beanName) { return (this.beanFactory != null && this.beanFactory.containsBeanDefinition(beanName) && this.beanFactory.getBeanDefinition(beanName).getRole() == BeanDefinition.ROLE_INFRASTRUCTURE); }
@Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.addBeanPostProcessor(new BootstrapContextAwareProcessor(this.bootstrapContext)); beanFactory.ignoreDependencyInterface(BootstrapContextAware.class); beanFactory.registerResolvableDependency(BootstrapContext.class, this.bootstrapContext); // JCA WorkManager resolved lazily - may not be available. beanFactory.registerResolvableDependency(WorkManager.class, (ObjectFactory<WorkManager>) this.bootstrapContext::getWorkManager); }
/** * Expose the given target class for the specified bean, if possible. * @param beanFactory the containing ConfigurableListableBeanFactory * @param beanName the name of the bean * @param targetClass the corresponding target class * @since 4.2.3 */ static void exposeTargetClass( ConfigurableListableBeanFactory beanFactory, @Nullable String beanName, Class<?> targetClass) { if (beanName != null && beanFactory.containsBeanDefinition(beanName)) { beanFactory.getMergedBeanDefinition(beanName).setAttribute(ORIGINAL_TARGET_CLASS_ATTRIBUTE, targetClass); } }
private static Bus getBusForName(String name, ApplicationContext context, boolean create) { if (!context.containsBean(name) && (create || Bus.DEFAULT_BUS_ID.equals(name))) { SpringBus b = new SpringBus(); ConfigurableApplicationContext cctx = (ConfigurableApplicationContext)context; cctx.getBeanFactory().registerSingleton(name, b); b.setApplicationContext(context); } return context.getBean(name, Bus.class); } private Object getBusForName(String name,
private Component getReferencedProcessor(String name) { if (applicationContext instanceof MuleArtifactContext) { MuleArtifactContext muleArtifactContext = (MuleArtifactContext) applicationContext; try { if (muleArtifactContext.getBeanFactory().getBeanDefinition(name).isPrototype()) { muleArtifactContext.getPrototypeBeanWithRootContainer(name, getRootContainerLocation().toString()); } } catch (NoSuchBeanDefinitionException e) { // Null is handled by the caller method return null; } } return (Component) applicationContext.getBean(name); }
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] beanNameArray = beanFactory.getBeanDefinitionNames(); for (int i = 0; i < beanNameArray.length; i++) { BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNameArray[i]); String beanClassName = beanDef.getBeanClassName(); if (FEIGN_FACTORY_CLASS.equals(beanClassName) == false) { continue; } MutablePropertyValues mpv = beanDef.getPropertyValues(); PropertyValue pv = mpv.getPropertyValue("name"); String client = String.valueOf(pv.getValue() == null ? "" : pv.getValue()); if (StringUtils.isNotBlank(client)) { this.transientClientSet.add(client); } } this.fireAfterPropertiesSet(); }
private void processPropertyPlaceHolders() { Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class); if (!prcs.isEmpty() && applicationContext instanceof ConfigurableApplicationContext) { BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) applicationContext) .getBeanFactory().getBeanDefinition(beanName); // PropertyResourceConfigurer does not expose any methods to explicitly perform // property placeholder substitution. Instead, create a BeanFactory that just // contains this mapper scanner and post process the factory. DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition(beanName, mapperScannerBean); for (PropertyResourceConfigurer prc : prcs.values()) { prc.postProcessBeanFactory(factory); } PropertyValues values = mapperScannerBean.getPropertyValues(); this.basePackage = updatePropertyValue("basePackage", values); this.sqlSessionFactoryBeanName = updatePropertyValue("sqlSessionFactoryBeanName", values); this.sqlSessionTemplateBeanName = updatePropertyValue("sqlSessionTemplateBeanName", values); } }
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); List<BeanDefinition> beanDefList = new ArrayList<BeanDefinition>(); String[] beanNameArray = beanFactory.getBeanDefinitionNames(); for (int i = 0; i < beanNameArray.length; i++) { String beanName = beanNameArray[i]; BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); String beanClassName = beanDef.getBeanClassName(); Class<?> beanClass = null; try { beanClass = cl.loadClass(beanClassName); } catch (Exception ex) { logger.debug("Cannot load class {}, beanId= {}!", beanClassName, beanName, ex); continue; } if (CompensableEndpointAware.class.isAssignableFrom(beanClass)) { beanDefList.add(beanDef); } } this.initializeEndpointIfNecessary(); for (int i = 0; i < beanDefList.size(); i++) { BeanDefinition beanDef = beanDefList.get(i); MutablePropertyValues mpv = beanDef.getPropertyValues(); mpv.addPropertyValue(CompensableEndpointAware.ENDPOINT_FIELD_NAME, this.identifier); } }
public void visitBeanDefinition(BeanDefinition beanDefinition) { String beanClassName = beanDefinition.getBeanClassName(); if(StepFactoryBean.class.getName().equals(beanClassName)) { PropertyValue [] values = beanDefinition.getPropertyValues().getPropertyValues(); for (PropertyValue propertyValue : values) { if(propertyValue.getName().equalsIgnoreCase("partitionReducer")) { RuntimeBeanReference ref = (RuntimeBeanReference) propertyValue.getValue(); beanFactory.getBeanDefinition(ref.getBeanName()).setLazyInit(true); } } } if(JsrPartitionHandler.class.getName().equals(beanClassName)) { PropertyValue [] values = beanDefinition.getPropertyValues().getPropertyValues(); for (PropertyValue propertyValue : values) { String propertyName = propertyValue.getName(); if(propertyName.equalsIgnoreCase("partitionMapper") || propertyName.equalsIgnoreCase("partitionAnalyzer")) { RuntimeBeanReference ref = (RuntimeBeanReference) propertyValue.getValue(); beanFactory.getBeanDefinition(ref.getBeanName()).setLazyInit(true); } } } } }
for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) { ConfigurableApplicationContext context = it.next(); result.append("{\n\"context\": \"").append(context.getId()).append("\",\n"); if (context.getParent() != null) { result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n"); ConfigurableListableBeanFactory bf = context.getBeanFactory(); String[] beanNames = bf.getBeanDefinitionNames(); boolean elementAppended = false; for (String beanName : beanNames) { BeanDefinition bd = bf.getBeanDefinition(beanName); if (isBeanEligible(beanName, bd, bf)) { if (elementAppended) { appendArray(result, bf.getAliases(beanName)); result.append(",\n"); String scope = bd.getScope(); if (!StringUtils.hasText(scope)) { scope = BeanDefinition.SCOPE_SINGLETON; Class<?> beanType = bf.getType(beanName); if (beanType != null) { result.append("\"type\": \"").append(beanType.getName()).append("\",\n"); appendArray(result, bf.getDependenciesForBean(beanName)); result.append("\n}"); elementAppended = true;
private void registerPersistenceExceptionTranslator() { if (this.applicationContext != null && this.applicationContext.getBeansOfType(PersistenceExceptionTranslator.class).isEmpty()) { if (this.applicationContext instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) this.applicationContext).getBeanFactory() .registerSingleton("solrExceptionTranslator", EXCEPTION_TRANSLATOR); } } }
private void processPropertyPlaceHolders() { Map<String, PropertyResourceConfigurer> prcs = applicationContext .getBeansOfType(PropertyResourceConfigurer.class); if (!prcs.isEmpty() && applicationContext instanceof GenericApplicationContext) { BeanDefinition mapperScannerBean = ((GenericApplicationContext) applicationContext).getBeanFactory() .getBeanDefinition(beanName); // PropertyResourceConfigurer does not expose any methods to // explicitly perform // property placeholder substitution. Instead, create a BeanFactory // that just // contains this mapper scanner and post process the factory. DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition(beanName, mapperScannerBean); for (PropertyResourceConfigurer prc : prcs.values()) { prc.postProcessBeanFactory(factory); } PropertyValues values = mapperScannerBean.getPropertyValues(); this.basePackage = updatePropertyValue("basePackage", values); this.sqlSessionFactoryBeanName = updatePropertyValue("sqlSessionFactoryBeanName", values); this.sqlSessionTemplateBeanName = updatePropertyValue("sqlSessionTemplateBeanName", values); } }
/** * Registers an {@link EntityManagerFactoryBeanDefinition} for the bean with the given name. Drops * {@link JndiObjectFactoryBean} instances that don't point to an {@link EntityManagerFactory} bean as expected type. * * @param name * @param beanFactory * @param definitions */ private static void registerEntityManagerFactoryBeanDefinition(String name, ConfigurableListableBeanFactory beanFactory, List<EntityManagerFactoryBeanDefinition> definitions) { BeanDefinition definition = beanFactory.getBeanDefinition(name); if (JNDI_OBJECT_FACTORY_BEAN.equals(definition.getBeanClassName())) { if (!EntityManagerFactory.class.getName().equals(definition.getPropertyValues().get("expectedType"))) { return; } } Class<?> type = beanFactory.getType(name); if (type == null || !EntityManagerFactory.class.isAssignableFrom(type)) { return; } definitions.add(new EntityManagerFactoryBeanDefinition(name, beanFactory)); }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String curName : beanNames) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(curName); PropertyValue[] values = beanDefinition.getPropertyValues().getPropertyValues(); for (PropertyValue propertyValue : values) { Object value = propertyValue.getValue(); if(value instanceof RuntimeBeanReference) { RuntimeBeanReference ref = (RuntimeBeanReference) value; if(!beanFactory.containsBean(ref.getBeanName())) { AbstractBeanDefinition newBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(ref.getBeanName()).getBeanDefinition(); newBeanDefinition.setScope("step"); ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(ref.getBeanName(), newBeanDefinition); } } } } }
@Test public void test() { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); assertThat("someDependency was not post processed", ctx.getBeanFactory().getBeanDefinition("someDependency").getDescription(), equalTo("post processed by MyPostProcessor")); MyConfig config = ctx.getBean(MyConfig.class); assertTrue("Config class was not enhanced", ClassUtils.isCglibProxy(config)); } }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { for (String beanName : beanFactory.getBeanDefinitionNames()) { try { BeanDefinition def = beanFactory.getBeanDefinition(beanName); if (def.getBeanClassName() != null) Class.forName(def.getBeanClassName()); processNested(def); } catch (ClassNotFoundException ignored) { ((BeanDefinitionRegistry)beanFactory).removeBeanDefinition(beanName); } } } };
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { BeanDefinition bd = beanFactory.getBeanDefinition("beanPostProcessor"); bd.getPropertyValues().addPropertyValue("nameSuffix", "-processed-" + myProp); } };