List of usage examples for org.springframework.beans.factory.config BeanDefinition getPropertyValues
MutablePropertyValues getPropertyValues();
From source file:com.clican.pluto.dataprocess.spring.parser.ExcelProcessorParser.java
@SuppressWarnings("unchecked") public void customiseBeanDefinition(BeanDefinition beanDef, Element element, ParserContext parserContext) { List excelBeanList = new ManagedList(); NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String localName = node.getLocalName(); if ("write".equals(localName) || "read".equals(localName)) { RootBeanDefinition bean = new RootBeanDefinition(); bean.setAbstract(false); bean.setBeanClass(ExcelExecBean.class); bean.setLazyInit(false); bean.setAutowireMode(Autowire.BY_NAME.value()); Element paramElement = (Element) node; String paramName = paramElement.getAttribute("paramName"); String resultName = paramElement.getAttribute("resultName"); String resource = paramElement.getAttribute("resource"); String sheetName = paramElement.getAttribute("sheetName"); String columns = paramElement.getAttribute("columns"); String columnsVarName = paramElement.getAttribute("columnsVarName"); String sheetVarName = paramElement.getAttribute("sheetVarName"); String typeMapStr = paramElement.getAttribute("typeMap"); String resourceVarName = paramElement.getAttribute("resourceVarName"); Map<String, String> typeMap = new HashMap<String, String>(); if ("read".equals(localName)) { for (String type : typeMapStr.split(";")) { typeMap.put(type.split("=>")[0], type.split("=>")[1]); }//from w ww.j a v a2 s . co m } bean.getPropertyValues().addPropertyValue("paramName", paramName); bean.getPropertyValues().addPropertyValue("resultName", resultName); bean.getPropertyValues().addPropertyValue("sheetName", sheetName); if (StringUtils.isNotEmpty(columns)) { bean.getPropertyValues().addPropertyValue("columns", columns.split(",")); } bean.getPropertyValues().addPropertyValue("resource", resource); bean.getPropertyValues().addPropertyValue("typeMap", typeMap); bean.getPropertyValues().addPropertyValue("columnsVarName", columnsVarName); bean.getPropertyValues().addPropertyValue("sheetVarName", sheetVarName); bean.getPropertyValues().addPropertyValue("resourceVarName", resourceVarName); if ("write".equals(localName)) { bean.getPropertyValues().addPropertyValue("read", false); } excelBeanList.add(bean); } } } beanDef.getPropertyValues().addPropertyValue("excelExecBeanList", excelBeanList); }
From source file:de.acosix.alfresco.utility.common.spring.BeanDefinitionFromPropertiesPostProcessor.java
protected void processRenamesOrRemovals(final BeanDefinitionRegistry registry, final Set<Object> processedKeys) { final String effectivePropertyPrefix = this.propertyPrefix + DOT; this.propertiesSource.forEach((key, value) -> { if (!processedKeys.contains(key)) { final Pair<String, String> processableKeyValue = this.getProcessableKeyValue(key, value, effectivePropertyPrefix); if (processableKeyValue != null) { LOGGER.debug("[{}] Evaluating property key {}", this.beanName, key); final String beanDefinitionKey = processableKeyValue.getFirst(); final String resolvedValue = processableKeyValue.getSecond(); final int propertyFragmentIdx = beanDefinitionKey.indexOf(FRAGMENT_PROPERTY); if (propertyFragmentIdx == -1) { LOGGER.trace("[{}] Processing entry {} = {}", this.beanName, key, resolvedValue); if (beanDefinitionKey.contains(FRAGMENT_RENAME)) { final String beanName = beanDefinitionKey.substring(0, beanDefinitionKey.indexOf(FRAGMENT_RENAME)); final String targetBeanName = beanDefinitionKey.substring( beanDefinitionKey.indexOf(FRAGMENT_RENAME) + FRAGMENT_RENAME.length()); if (Boolean.parseBoolean(resolvedValue)) { if (registry.containsBeanDefinition(beanName)) { LOGGER.debug("[{}] Renaming bean {} to {}", this.beanName, beanName, targetBeanName); final BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); registry.removeBeanDefinition(beanName); registry.registerBeanDefinition(targetBeanName, beanDefinition); } else { LOGGER.debug("[{}] Unable to rename bean {} to {} - bean has not been defined", this.beanName, beanName, targetBeanName); }// www . j a va 2 s.c om } else { LOGGER.debug("[{}] Not renaming bean {} to {} due to non-true property value", this.beanName, beanName, targetBeanName); } processedKeys.add(key); } else if (beanDefinitionKey.endsWith(SUFFIX_REMOVE)) { final String beanName = beanDefinitionKey.substring(0, beanDefinitionKey.indexOf(SUFFIX_REMOVE)); if (Boolean.parseBoolean(resolvedValue)) { if (registry.containsBeanDefinition(beanName)) { LOGGER.debug("[{}] Removing bean {}", this.beanName, beanName); registry.removeBeanDefinition(beanName); } else { LOGGER.debug("[{}] Unable to remove bean {} - bean has not been defined", this.beanName, beanName); } } else { LOGGER.debug("[{}] Not removing bean {} due to non-true property value", this.beanName, beanName); } processedKeys.add(key); } else { LOGGER.trace("[{}] Setting unsupported by processRenamesOrRemovals: {} = {}", this.beanName, key, resolvedValue); } } else { final String beanName = beanDefinitionKey.substring(0, propertyFragmentIdx); final String propertyDefinitionKey = beanDefinitionKey .substring(propertyFragmentIdx + FRAGMENT_PROPERTY.length()); if (propertyDefinitionKey.endsWith(SUFFIX_REMOVE)) { final String propertyName = propertyDefinitionKey.substring(0, propertyDefinitionKey.indexOf(SUFFIX_REMOVE)); if (registry.containsBeanDefinition(beanName)) { final BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); if (propertyValues.contains(propertyName)) { if (value instanceof String && Boolean.parseBoolean(resolvedValue)) { LOGGER.debug("[{}] Removing property {} from {}", this.beanName, propertyName, beanName); propertyValues.removePropertyValue(propertyName); } else { LOGGER.debug( "[{}] Not removing property {} from [} due to non-true property value", this.beanName, propertyName, beanName); } processedKeys.add(key); } else { LOGGER.trace( "[{}] Property {} not found in bean {} - key {} may refer to removal of values in collection-like property", this.beanName, propertyName, beanName, key); } } else { LOGGER.debug( "[{}] Unable to remove property {} from {} - bean has not been defined", this.beanName, propertyName, beanName); processedKeys.add(key); } } } } } }); }
From source file:org.alfresco.module.org_alfresco_module_rm.security.RMMethodSecurityPostProcessor.java
/** * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) *///from w w w.ja v a 2 s . co m @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { for (String bean : getSecurityBeanNames(beanFactory)) { if (beanFactory.containsBeanDefinition(bean)) { if (logger.isDebugEnabled()) { logger.debug("Adding RM method security definitions for " + bean); } BeanDefinition beanDef = beanFactory.getBeanDefinition(bean); PropertyValue beanValue = beanDef.getPropertyValues() .getPropertyValue(PROP_OBJECT_DEFINITION_SOURCE); if (beanValue != null) { String beanStringValue = (String) ((TypedStringValue) beanValue.getValue()).getValue(); String mergedStringValue = merge(beanStringValue); beanDef.getPropertyValues().addPropertyValue(PROP_OBJECT_DEFINITION_SOURCE, new TypedStringValue(mergedStringValue)); } } } }
From source file:org.alfresco.util.BeanExtender.java
/** * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) *//*from w w w.java 2 s .c om*/ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { ParameterCheck.mandatory("beanName", beanName); ParameterCheck.mandatory("extendingBeanName", extendingBeanName); // check for bean name if (!beanFactory.containsBean(beanName)) { throw new NoSuchBeanDefinitionException("Can't find bean '" + beanName + "' to be extended."); } // check for extending bean if (!beanFactory.containsBean(extendingBeanName)) { throw new NoSuchBeanDefinitionException("Can't find bean '" + extendingBeanName + "' that is going to extend original bean definition."); } // get the bean definitions BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); BeanDefinition extendingBeanDefinition = beanFactory.getBeanDefinition(extendingBeanName); // update class if (StringUtils.isNotBlank(extendingBeanDefinition.getBeanClassName()) && !beanDefinition.getBeanClassName().equals(extendingBeanDefinition.getBeanClassName())) { beanDefinition.setBeanClassName(extendingBeanDefinition.getBeanClassName()); } // update properties MutablePropertyValues properties = beanDefinition.getPropertyValues(); MutablePropertyValues extendingProperties = extendingBeanDefinition.getPropertyValues(); for (PropertyValue propertyValue : extendingProperties.getPropertyValueList()) { properties.add(propertyValue.getName(), propertyValue.getValue()); } }
From source file:org.apache.camel.spring.handler.CamelNamespaceHandler.java
protected void createBeanPostProcessor(ParserContext parserContext, String contextId, Element childElement, BeanDefinitionBuilder parentBuilder) { String beanPostProcessorId = contextId + ":beanPostProcessor"; childElement.setAttribute("id", beanPostProcessorId); BeanDefinition definition = beanPostProcessorParser.parse(childElement, parserContext); // only register to camel context id as a String. Then we can look it up later // otherwise we get a circular reference in spring and it will not allow custom bean post processing // see more at CAMEL-1663 definition.getPropertyValues().addPropertyValue("camelId", contextId); parentBuilder.addPropertyReference("beanPostProcessor", beanPostProcessorId); }
From source file:org.apache.camel.spring.handler.CamelNamespaceHandler.java
private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) { String id = childElement.getAttribute("id"); // must have an id to be registered if (ObjectHelper.isNotEmpty(id)) { BeanDefinition definition = endpointParser.parse(childElement, parserContext); definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId)); // Need to add this dependency of CamelContext for Spring 3.0 try {// w w w . j a va 2 s . c om Method method = definition.getClass().getMethod("setDependsOn", String[].class); method.invoke(definition, (Object) new String[] { contextId }); } catch (Exception e) { // Do nothing here } parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id)); } }
From source file:org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.java
/** * Prepares Spring context./*from w w w . j a v a2 s . co m*/ * * @param excludedProps Properties to be excluded. * @return application context. */ private static GenericApplicationContext prepareSpringContext(final String... excludedProps) { GenericApplicationContext springCtx = new GenericApplicationContext(); if (excludedProps.length > 0) { final List<String> excludedPropsList = Arrays.asList(excludedProps); BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() { /** * @param def Registered BeanDefinition. * @throws BeansException in case of errors. */ private void processNested(BeanDefinition def) throws BeansException { Iterator<PropertyValue> iterVals = def.getPropertyValues().getPropertyValueList().iterator(); while (iterVals.hasNext()) { PropertyValue val = iterVals.next(); if (excludedPropsList.contains(val.getName())) { iterVals.remove(); continue; } if (val.getValue() instanceof Iterable) { Iterator iterNested = ((Iterable) val.getValue()).iterator(); while (iterNested.hasNext()) { Object item = iterNested.next(); if (item instanceof BeanDefinitionHolder) { BeanDefinitionHolder h = (BeanDefinitionHolder) item; try { if (h.getBeanDefinition().getBeanClassName() != null) Class.forName(h.getBeanDefinition().getBeanClassName()); processNested(h.getBeanDefinition()); } catch (ClassNotFoundException ignored) { iterNested.remove(); } } } } } } @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); } } } }; springCtx.addBeanFactoryPostProcessor(postProc); } return springCtx; }
From source file:org.beangle.spring.bind.AutoConfigProcessor.java
protected void autowire(String beanName, BeanDefinition mbd) { Map<String, PropertyDescriptor> properties = unsatisfiedNonSimpleProperties(mbd); for (Map.Entry<String, PropertyDescriptor> entry : properties.entrySet()) { String propertyName = entry.getKey(); PropertyDescriptor pd = entry.getValue(); if (Object.class.equals(pd.getPropertyType())) continue; MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd); List<String> beanNames = bindRegistry.getBeanNames(methodParam.getParameterType()); boolean binded = false; if (beanNames.size() == 1) { mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(beanNames.get(0))); binded = true;//from w ww . j av a 2 s. c o m } else if (beanNames.size() > 1) { for (String name : beanNames) { if (name.equals(propertyName)) { mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(propertyName)); binded = true; break; } } } if (!binded) { if (beanNames.isEmpty()) { logger.debug(beanName + "'s " + propertyName + " cannot found candidate bean"); } else { logger.warn(beanName + "'s " + propertyName + " expected single bean but found {} : {}", beanNames.size(), beanNames); } } } }
From source file:org.beangle.spring.bind.AutoConfigProcessor.java
protected Map<String, PropertyDescriptor> unsatisfiedNonSimpleProperties(BeanDefinition mbd) { Map<String, PropertyDescriptor> properties = CollectUtils.newHashMap(); PropertyValues pvs = mbd.getPropertyValues(); Class<?> clazz = null;// w w w . j ava 2s . c o m try { clazz = Class.forName(mbd.getBeanClassName()); } catch (ClassNotFoundException e) { logger.error("Class " + mbd.getBeanClassName() + "not found", e); return properties; } PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) && !BeanUtils.isSimpleProperty(pd.getPropertyType())) { properties.put(pd.getName(), pd); } } return properties; }
From source file:org.beangle.spring.bind.DefinitionBindRegistry.java
public DefinitionBindRegistry(BeanDefinitionRegistry registry) { for (String name : registry.getBeanDefinitionNames()) { BeanDefinition bd = registry.getBeanDefinition(name); if (bd.isAbstract()) continue; // find classname String className = bd.getBeanClassName(); if (null == className) { String parentName = bd.getParentName(); if (null == parentName) continue; else { BeanDefinition parentDef = registry.getBeanDefinition(parentName); className = parentDef.getBeanClassName(); }// w w w .ja v a 2 s .c om } if (null == className) continue; try { Class<?> beanClass = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader()); if (FactoryBean.class.isAssignableFrom(beanClass)) { register(beanClass, "&" + name); PropertyValue pv = bd.getPropertyValues().getPropertyValue("target"); if (null == pv) { Class<?> artifactClass = ((FactoryBean<?>) beanClass.newInstance()).getObjectType(); if (null != artifactClass) register(artifactClass, name); } else { if (pv.getValue() instanceof BeanDefinitionHolder) { String nestedClassName = ((BeanDefinitionHolder) pv.getValue()).getBeanDefinition() .getBeanClassName(); if (null != nestedClassName) { register(ClassUtils.forName(nestedClassName, ClassUtils.getDefaultClassLoader()), name); } } } } else { register(beanClass, name); } } catch (Exception e) { logger.error("class not found", e); continue; } } }