Java Code Examples for org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getBeanDefinition()
The following examples show how to use
org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getBeanDefinition() .
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
Source File: ParentBeanFactoryPostProcessor.java From jdal with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] names = beanFactory.getBeanDefinitionNames(); for (String beanName : names) { Parent parent = beanFactory.findAnnotationOnBean(beanName, Parent.class); if (parent != null) { BeanDefinition bd = beanFactory.getBeanDefinition(beanName); bd.setParentName(parent.value()); } } }
Example 2
Source File: UifBeanFactoryPostProcessor.java From rice with Educational Community License v2.0 | 6 votes |
/** * Retrieves the expression graph map set on the bean with given name. If the bean has not been processed * by the bean factory post processor, that is done before retrieving the map * * @param parentBeanName name of the parent bean to retrieve map for (if empty a new map will be returned) * @param beanFactory bean factory to retrieve bean definition from * @param processedBeanNames set of bean names that have been processed so far * @return expression graph map from parent or new instance */ protected Map<String, String> getExpressionGraphFromParent(String parentBeanName, ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) { Map<String, String> expressionGraph = new HashMap<String, String>(); if (StringUtils.isBlank(parentBeanName) || !beanFactory.containsBeanDefinition(parentBeanName)) { return expressionGraph; } BeanDefinition beanDefinition = beanFactory.getBeanDefinition(parentBeanName); if (!processedBeanNames.contains(parentBeanName)) { processBeanDefinition(parentBeanName, beanDefinition, beanFactory, processedBeanNames); } MutablePropertyValues pvs = beanDefinition.getPropertyValues(); PropertyValue propertyExpressionsPV = pvs.getPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH); if (propertyExpressionsPV != null) { Object value = propertyExpressionsPV.getValue(); if ((value != null) && (value instanceof ManagedMap)) { expressionGraph.putAll((ManagedMap) value); } } return expressionGraph; }
Example 3
Source File: UifBeanFactoryPostProcessor.java From rice with Educational Community License v2.0 | 6 votes |
/** * Retrieves the class for the object that will be created from the bean definition. Since the class might not * be configured on the bean definition, but by a parent, each parent bean definition is recursively checked for * a class until one is found * * @param beanDefinition bean definition to get class for * @param beanFactory bean factory that contains the bean definition * @return class configured for the bean definition, or null */ protected Class<?> getBeanClass(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) { if (StringUtils.isNotBlank(beanDefinition.getBeanClassName())) { try { return Class.forName(beanDefinition.getBeanClassName()); } catch (ClassNotFoundException e) { // swallow exception and return null so bean is not processed return null; } } else if (StringUtils.isNotBlank(beanDefinition.getParentName())) { BeanDefinition parentBeanDefinition = beanFactory.getBeanDefinition(beanDefinition.getParentName()); if (parentBeanDefinition != null) { return getBeanClass(parentBeanDefinition, beanFactory); } } return null; }
Example 4
Source File: KotlinLambdaToFunctionAutoConfiguration.java From spring-cloud-function with Apache License 2.0 | 6 votes |
/** * Will transform all discovered Kotlin's Function lambdas to java * Supplier, Function and Consumer, retaining the original Kotlin type * characteristics. * * @return the bean factory post processor */ @Bean public BeanFactoryPostProcessor kotlinToFunctionTransformer() { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName); ResolvableType rt = beanDefinition.getResolvableType(); if (rt.getType().getTypeName().startsWith("kotlin.jvm.functions.Function")) { RootBeanDefinition cbd = new RootBeanDefinition(KotlinFunctionWrapper.class); ConstructorArgumentValues ca = new ConstructorArgumentValues(); ca.addGenericArgumentValue(beanDefinition); cbd.setConstructorArgumentValues(ca); ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(beanDefinitionName + FunctionRegistration.REGISTRATION_NAME_SUFFIX, cbd); } } } }; }
Example 5
Source File: CarBeanFactoryPostProcessor.java From spring-analysis-note with MIT License | 6 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // 从 beanFactory 中获取 bean 名字列表 String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String beanName : beanNames) { BeanDefinition definition = beanFactory.getBeanDefinition(beanName); StringValueResolver valueResolver = strVal -> { if (isObscene(strVal)) return "*****"; return strVal; }; BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); // 这一步才是真正处理 bean 的配置信息 visitor.visitBeanDefinition(definition); } }
Example 6
Source File: ReloadingPropertyPlaceholderConfigurer.java From disconf with Apache License 2.0 | 5 votes |
/** * copy & paste, just so we can insert our own visitor. * 启动时 进行配置的解析 */ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { BeanDefinitionVisitor visitor = new ReloadingPropertyPlaceholderConfigurer.PlaceholderResolvingBeanDefinitionVisitor(props); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (int i = 0; i < beanNames.length; i++) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in net.unicon.iamlabs.spring.properties.example.net // .unicon.iamlabs.spring.properties file locations. if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { this.currentBeanName = beanNames[i]; try { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); try { visitor.visitBeanDefinition(bd); } catch (BeanDefinitionStoreException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); } } finally { currentBeanName = null; } } } StringValueResolver stringValueResolver = new PlaceholderResolvingStringValueResolver(props); // New in Spring 2.5: resolve placeholders in alias target names and aliases as well. beanFactoryToProcess.resolveAliases(stringValueResolver); // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes. beanFactoryToProcess.addEmbeddedValueResolver(stringValueResolver); }
Example 7
Source File: AutoProxyUtils.java From java-technology-stack with MIT License | 5 votes |
/** * Determine whether the given bean should be proxied with its target * class rather than its interfaces. Checks the * {@link #PRESERVE_TARGET_CLASS_ATTRIBUTE "preserveTargetClass" attribute} * of the corresponding bean definition. * @param beanFactory the containing ConfigurableListableBeanFactory * @param beanName the name of the bean * @return whether the given bean should be proxied with its target class */ public static boolean shouldProxyTargetClass( ConfigurableListableBeanFactory beanFactory, @Nullable String beanName) { if (beanName != null && beanFactory.containsBeanDefinition(beanName)) { BeanDefinition bd = beanFactory.getBeanDefinition(beanName); return Boolean.TRUE.equals(bd.getAttribute(PRESERVE_TARGET_CLASS_ATTRIBUTE)); } return false; }
Example 8
Source File: DoNotExecuteMongeezPostProcessor.java From mongeez-spring-boot-starter with Apache License 2.0 | 5 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] mongeezBeanNames = beanFactory.getBeanNamesForType(Mongeez.class); Assert.state(mongeezBeanNames.length == 1); BeanDefinition beanDefinition = beanFactory.getBeanDefinition(mongeezBeanNames[0]); Assert.state(beanDefinition instanceof RootBeanDefinition); ((RootBeanDefinition) beanDefinition).setInitMethodName(null); }
Example 9
Source File: AutoJsonRpcServiceExporter.java From jsonrpc4j with MIT License | 5 votes |
/** * Find a {@link BeanDefinition} in the {@link BeanFactory} or it's parents. */ private BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory, String serviceBeanName) { if (beanFactory.containsLocalBean(serviceBeanName)) return beanFactory.getBeanDefinition(serviceBeanName); BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory(); if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory)) return findBeanDefinition((ConfigurableListableBeanFactory) parentBeanFactory, serviceBeanName); throw new RuntimeException(format("Bean with name '%s' can no longer be found.", serviceBeanName)); }
Example 10
Source File: RequiredAnnotationBeanPostProcessor.java From blog_demos with Apache License 2.0 | 5 votes |
/** * Check whether the given bean definition is not subject to the annotation-based * required property check as performed by this post-processor. * <p>The default implementations check for the presence of the * {@link #SKIP_REQUIRED_CHECK_ATTRIBUTE} attribute in the bean definition, if any. * It also suggests skipping in case of a bean definition with a "factory-bean" * reference set, assuming that instance-based factories pre-populate the bean. * @param beanFactory the BeanFactory to check against * @param beanName the name of the bean to check against * @return {@code true} to skip the bean; {@code false} to process it */ protected boolean shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName) { if (beanFactory == null || !beanFactory.containsBeanDefinition(beanName)) { return false; } BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); if (beanDefinition.getFactoryBeanName() != null) { return true; } Object value = beanDefinition.getAttribute(SKIP_REQUIRED_CHECK_ATTRIBUTE); return (value != null && (Boolean.TRUE.equals(value) || Boolean.valueOf(value.toString()))); }
Example 11
Source File: MyBeanFactoryPostProcessor.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("[BeanFactoryPostProcessor] BeanFactoryPostProcessor call postProcessBeanFactory"); String[] beanNamesForType = beanFactory.getBeanNamesForType(Person.class); for (String name : beanNamesForType) { BeanDefinition bd = beanFactory.getBeanDefinition(name); bd.getPropertyValues().addPropertyValue("phone", "110"); } }
Example 12
Source File: ExtensionsAdminController.java From olat with Apache License 2.0 | 5 votes |
/** * hmmm, does not work yet, how to get a list with all overwritten beans like the output from to the log.info * http://www.docjar.com/html/api/org/springframework/beans/factory/support/DefaultListableBeanFactory.java.html * * @return */ private List getOverwrittenBeans() { final ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext); final XmlWebApplicationContext context = (XmlWebApplicationContext) applicationContext; final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (int i = 0; i < beanDefinitionNames.length; i++) { final String beanName = beanDefinitionNames[i]; if (!beanName.contains("#")) { final BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); // System.out.println(beanDef.getOriginatingBeanDefinition()); } } return null; }
Example 13
Source File: AutoJsonRpcServiceImplExporter.java From jsonrpc4j with MIT License | 5 votes |
/** * Find a {@link BeanDefinition} in the {@link BeanFactory} or it's parents. */ private BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory, String serviceBeanName) { if (beanFactory.containsLocalBean(serviceBeanName)) { return beanFactory.getBeanDefinition(serviceBeanName); } BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory(); if (parentBeanFactory != null && ConfigurableListableBeanFactory.class.isInstance(parentBeanFactory)) { return findBeanDefinition((ConfigurableListableBeanFactory) parentBeanFactory, serviceBeanName); } throw new NoSuchBeanDefinitionException(serviceBeanName); }
Example 14
Source File: BeanDependencyManager.java From jetcache with Apache License 2.0 | 5 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] autoInitBeanNames = beanFactory.getBeanNamesForType(AbstractCacheAutoInit.class, false, false); if (autoInitBeanNames != null) { BeanDefinition bd = beanFactory.getBeanDefinition(JetCacheAutoConfiguration.GLOBAL_CACHE_CONFIG_NAME); String[] dependsOn = bd.getDependsOn(); if (dependsOn == null) { dependsOn = new String[0]; } int oldLen = dependsOn.length; dependsOn = Arrays.copyOf(dependsOn, dependsOn.length + autoInitBeanNames.length); System.arraycopy(autoInitBeanNames,0, dependsOn, oldLen, autoInitBeanNames.length); bd.setDependsOn(dependsOn); } }
Example 15
Source File: Spr7167Tests.java From spring-analysis-note with MIT License | 4 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { AbstractBeanDefinition bd = (AbstractBeanDefinition) beanFactory.getBeanDefinition("someDependency"); bd.setDescription("post processed by MyPostProcessor"); }
Example 16
Source File: MyBeanFactoryPostProcessor.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 4 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("[BeanFactoryPostProcessor] BeanFactoryPostProcessor call postProcessBeanFactory"); BeanDefinition bd = beanFactory.getBeanDefinition("person"); bd.getPropertyValues().addPropertyValue("phone", "110"); }
Example 17
Source File: BaseAutoConfiguration.java From graphql-spqr-spring-boot-starter with Apache License 2.0 | 4 votes |
@SuppressWarnings({"unchecked"}) private List<SpqrBean> findGraphQLApiBeans() { ConfigurableListableBeanFactory factory = context.getBeanFactory(); List<SpqrBean> spqrBeans = new ArrayList<>(); for (String beanName : factory.getBeanDefinitionNames()) { BeanDefinition bd = factory.getBeanDefinition(beanName); if (bd.getSource() instanceof StandardMethodMetadata) { StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource(); Map<String, Object> attributes = metadata.getAnnotationAttributes(GraphQLApi.class.getName()); if (null == attributes) { continue; } SpqrBean spqrBean = new SpqrBean(context, beanName, metadata.getIntrospectedMethod().getAnnotatedReturnType()); Map<String, Object> withResolverBuildersAttributes = metadata.getAnnotationAttributes(WithResolverBuilders.class.getTypeName()); if (withResolverBuildersAttributes != null) { AnnotationAttributes[] annotationAttributesArray = (AnnotationAttributes[]) withResolverBuildersAttributes.get("value"); Arrays.stream(annotationAttributesArray) .forEach(annotationAttributes -> spqrBean.getResolverBuilders().add( new ResolverBuilderBeanIdentity( (Class<? extends ResolverBuilder>) annotationAttributes.get("value"), (String) annotationAttributes.get("qualifierValue"), (Class<? extends Annotation>) annotationAttributes.get("qualifierType")) ) ); } else { Map<String, Object> withResolverBuilderAttributes = metadata.getAnnotationAttributes(WithResolverBuilder.class.getTypeName()); if (withResolverBuilderAttributes != null) { spqrBean.getResolverBuilders().add( new ResolverBuilderBeanIdentity( (Class<? extends ResolverBuilder>) withResolverBuilderAttributes.get("value"), (String) withResolverBuilderAttributes.get("qualifierValue"), (Class<? extends Annotation>) withResolverBuilderAttributes.get("qualifierType")) ); } } spqrBeans.add(spqrBean); } } return spqrBeans; }
Example 18
Source File: BusWiringBeanFactoryPostProcessor.java From cxf with Apache License 2.0 | 4 votes |
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) { Object inject = bus; if (inject == null) { inject = getBusForName(Bus.DEFAULT_BUS_ID, factory, true, null); } else { if (!factory.containsBeanDefinition(Bus.DEFAULT_BUS_ID) && !factory.containsSingleton(Bus.DEFAULT_BUS_ID)) { factory.registerSingleton(Bus.DEFAULT_BUS_ID, bus); } } for (String beanName : factory.getBeanDefinitionNames()) { BeanDefinition beanDefinition = factory.getBeanDefinition(beanName); BusWiringType type = (BusWiringType)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE); if (type == null) { continue; } String busname = (String)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME); String create = (String)beanDefinition .getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE); Object inj = inject; if (busname != null) { if (bus != null) { continue; } inj = getBusForName(busname, factory, create != null, create); } beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME); beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE); beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE); if (create == null) { if (BusWiringType.PROPERTY == type) { beanDefinition.getPropertyValues() .addPropertyValue("bus", inj); } else if (BusWiringType.CONSTRUCTOR == type) { ConstructorArgumentValues constructorArgs = beanDefinition.getConstructorArgumentValues(); insertConstructorArg(constructorArgs, inj); } } } }
Example 19
Source File: LiveBeansView.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Actually generate a JSON snapshot of the beans in the given ApplicationContexts. * <p>This implementation doesn't use any JSON parsing libraries in order to avoid * third-party library dependencies. It produces an array of context description * objects, each containing a context and parent attribute as well as a beans * attribute with nested bean description objects. Each bean object contains a * bean, scope, type and resource attribute, as well as a dependencies attribute * with a nested array of bean names that the present bean depends on. * @param contexts the set of ApplicationContexts * @return the JSON document */ protected String generateJson(Set<ConfigurableApplicationContext> contexts) { StringBuilder result = new StringBuilder("[\n"); 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"); } else { result.append("\"parent\": null,\n"); } result.append("\"beans\": [\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) { result.append(",\n"); } result.append("{\n\"bean\": \"").append(beanName).append("\",\n"); result.append("\"aliases\": "); appendArray(result, bf.getAliases(beanName)); result.append(",\n"); String scope = bd.getScope(); if (!StringUtils.hasText(scope)) { scope = BeanDefinition.SCOPE_SINGLETON; } result.append("\"scope\": \"").append(scope).append("\",\n"); Class<?> beanType = bf.getType(beanName); if (beanType != null) { result.append("\"type\": \"").append(beanType.getName()).append("\",\n"); } else { result.append("\"type\": null,\n"); } result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n"); result.append("\"dependencies\": "); appendArray(result, bf.getDependenciesForBean(beanName)); result.append("\n}"); elementAppended = true; } } result.append("]\n"); result.append("}"); if (it.hasNext()) { result.append(",\n"); } } result.append("]"); return result.toString(); }
Example 20
Source File: LiveBeansView.java From spring-analysis-note with MIT License | 4 votes |
/** * Actually generate a JSON snapshot of the beans in the given ApplicationContexts. * <p>This implementation doesn't use any JSON parsing libraries in order to avoid * third-party library dependencies. It produces an array of context description * objects, each containing a context and parent attribute as well as a beans * attribute with nested bean description objects. Each bean object contains a * bean, scope, type and resource attribute, as well as a dependencies attribute * with a nested array of bean names that the present bean depends on. * @param contexts the set of ApplicationContexts * @return the JSON document */ protected String generateJson(Set<ConfigurableApplicationContext> contexts) { StringBuilder result = new StringBuilder("[\n"); 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"); } else { result.append("\"parent\": null,\n"); } result.append("\"beans\": [\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) { result.append(",\n"); } result.append("{\n\"bean\": \"").append(beanName).append("\",\n"); result.append("\"aliases\": "); appendArray(result, bf.getAliases(beanName)); result.append(",\n"); String scope = bd.getScope(); if (!StringUtils.hasText(scope)) { scope = BeanDefinition.SCOPE_SINGLETON; } result.append("\"scope\": \"").append(scope).append("\",\n"); Class<?> beanType = bf.getType(beanName); if (beanType != null) { result.append("\"type\": \"").append(beanType.getName()).append("\",\n"); } else { result.append("\"type\": null,\n"); } result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n"); result.append("\"dependencies\": "); appendArray(result, bf.getDependenciesForBean(beanName)); result.append("\n}"); elementAppended = true; } } result.append("]\n"); result.append("}"); if (it.hasNext()) { result.append(",\n"); } } result.append("]"); return result.toString(); }