List of usage examples for org.springframework.beans.factory.config BeanDefinitionHolder getBeanDefinition
public BeanDefinition getBeanDefinition()
From source file:br.com.caelum.vraptor.ioc.spring.SpringRegistry.java
/** * From org.springframework.context.annotation.ClassPathBeanDefinitionScanner#applyScope() * @param definition// www .j a v a2s. c o m * @param scopeMetadata * * @return */ private BeanDefinitionHolder applyScopeOn(BeanDefinitionHolder definition, ScopeMetadata scopeMetadata) { String scope = scopeMetadata.getScopeName(); ScopedProxyMode proxyMode = scopeMetadata.getScopedProxyMode(); definition.getBeanDefinition().setScope(scope); if (BeanDefinition.SCOPE_SINGLETON.equals(scope) || BeanDefinition.SCOPE_PROTOTYPE.equals(scope) || proxyMode.equals(ScopedProxyMode.NO)) { return definition; } else { boolean proxyTargetClass = proxyMode.equals(ScopedProxyMode.TARGET_CLASS); return ScopedProxyUtils.createScopedProxy(definition, (BeanDefinitionRegistry) beanFactory, proxyTargetClass); } }
From source file:com.comstar.mars.env.EnvClassPathMapperScanner.java
/** * Calls the parent search that will search and register all the candidates. * Then the registered objects are post processed to set them as * MapperFactoryBeans/*from w w w.j a v a 2 s . co m*/ */ @Override public Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); } else { for (BeanDefinitionHolder holder : beanDefinitions) { GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition(); if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); } // the mapper interface is the original class of the bean // but, the actual class of the bean is MapperFactoryBean definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName()); definition.setBeanClass(EnvMapperFactoryBean.class); boolean explicitFactoryUsed = false; if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionFactory != null) { definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); explicitFactoryUsed = true; } if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { if (explicitFactoryUsed) { logger.warn( "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionTemplate != null) { if (explicitFactoryUsed) { logger.warn( "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); explicitFactoryUsed = true; } if (!explicitFactoryUsed) { if (logger.isDebugEnabled()) { logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'."); } definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); } } } return beanDefinitions; }
From source file:org.solmix.runtime.support.spring.AbstractBeanDefinitionParser.java
protected void firstChildAsProperty(Element element, ParserContext ctx, BeanDefinitionBuilder bean, String propertyName) {/*from w ww . ja va 2 s .c o m*/ Element first = getFirstChild(element); if (first == null) { throw new IllegalStateException(propertyName + " property must have child elements!"); } String id; BeanDefinition child; if (first.getNamespaceURI().equals(BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI)) { String name = first.getLocalName(); if ("ref".equals(name)) { id = first.getAttribute("bean"); if (id == null) { throw new IllegalStateException("<ref> elements must have a \"bean\" attribute!"); } bean.addPropertyReference(propertyName, id); return; } else if ("bean".equals(name)) { BeanDefinitionHolder bdh = ctx.getDelegate().parseBeanDefinitionElement(first); child = bdh.getBeanDefinition(); bean.addPropertyValue(propertyName, child); return; } else { throw new UnsupportedOperationException("Elements with the name " + name + " are not currently " + "supported as sub elements of " + element.getLocalName()); } } child = ctx.getDelegate().parseCustomElement(first, bean.getBeanDefinition()); bean.addPropertyValue(propertyName, child); }
From source file:framework.generic.mybatis.ClassPathMapperScanner.java
/** * Calls the parent search that will search and register all the candidates. * Then the registered objects are post processed to set them as * MapperFactoryBeans//from w w w. j a va 2 s .c om */ @Override public Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); } else { for (BeanDefinitionHolder holder : beanDefinitions) { GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition(); if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); } // the mapper interface is the original class of the bean // but, the actual class of the bean is MapperFactoryBean definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName()); definition.setBeanClass(MapperFactoryBean.class); // definition.setBeanClass(ExecutorFactoryBean.class); definition.getPropertyValues().add("executor", this.executor); definition.getPropertyValues().add("addToConfig", this.addToConfig); boolean explicitFactoryUsed = false; if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionFactory != null) { definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); explicitFactoryUsed = true; } if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { if (explicitFactoryUsed) { logger.warn( "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionTemplate != null) { if (explicitFactoryUsed) { logger.warn( "Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); explicitFactoryUsed = true; } if (!explicitFactoryUsed) { if (logger.isDebugEnabled()) { logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'."); } definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); } } } return beanDefinitions; }
From source file:org.springmodules.cache.config.BeanReferenceParserImpl.java
/** * @see BeanReferenceParser#parse(Element,ParserContext,boolean) *//*from w ww . j a v a 2s . com*/ public Object parse(Element element, ParserContext parserContext, boolean registerInnerBean) { String refId = element.getAttribute("refId"); if (StringUtils.hasText(refId)) { return new RuntimeBeanReference(refId); } Element beanElement = null; List beanElements = DomUtils.getChildElementsByTagName(element, "bean"); if (!CollectionUtils.isEmpty(beanElements)) { beanElement = (Element) beanElements.get(0); } if (beanElement == null) { throw new IllegalStateException("The XML element " + StringUtils.quote(element.getNodeName()) + " should either have a " + "reference to an already registered bean definition or contain a " + "bean definition"); } BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement(beanElement); String beanName = holder.getBeanName(); if (registerInnerBean && StringUtils.hasText(beanName)) { BeanDefinitionRegistry registry = parserContext.getRegistry(); BeanDefinition beanDefinition = holder.getBeanDefinition(); registry.registerBeanDefinition(beanName, beanDefinition); return new RuntimeBeanReference(beanName); } return holder; }
From source file:org.xeustechnologies.jcl.spring.JclBeanDefinitionDecorator.java
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) { String jclRef = node.getAttributes().getNamedItem(JCL_REF).getNodeValue(); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setFactoryBeanName(JCL_FACTORY);//from w w w.j ava 2s .c o m bd.setFactoryMethodName(JCL_FACTORY_METHOD); bd.setConstructorArgumentValues(holder.getBeanDefinition().getConstructorArgumentValues()); bd.setPropertyValues(holder.getBeanDefinition().getPropertyValues()); bd.getConstructorArgumentValues().addIndexedArgumentValue(0, new ConstructorArgumentValues.ValueHolder(parserContext.getRegistry().getBeanDefinition(jclRef))); bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new ConstructorArgumentValues.ValueHolder(holder.getBeanDefinition().getBeanClassName())); BeanDefinitionHolder newHolder = new BeanDefinitionHolder(bd, holder.getBeanName()); createDependencyOnJcl(node, newHolder, parserContext); return newHolder; }
From source file:com.mtgi.analytics.aop.config.v11.BtInnerBeanDefinitionParser.java
public BeanDefinition parse(Element element, ParserContext parserContext) { //no custom attributes, delegate to default definition parser. BeanDefinitionHolder ret = parserContext.getDelegate().parseBeanDefinitionElement(element); if (ret != null) { //add parsed inner bean to containing manager definition if applicable if (!BtManagerBeanDefinitionParser.registerNestedBean(ret, property, parserContext)) { //add bean to global registry BeanDefinition def = ret.getBeanDefinition(); String id = element.getAttribute("id"); if (StringUtils.hasText(id)) parserContext.getRegistry().registerBeanDefinition(id, def); else/*ww w . ja va 2 s .c o m*/ parserContext.getReaderContext().registerWithGeneratedName(def); return def; } } //global bean definition not created, probably some parse error. return null; }
From source file:org.guicerecipes.spring.converter.SpringConverter.java
@SuppressWarnings("unchecked") protected void generateBeanDefinition(ModuleGenerator generator, String name, BeanDefinition definition, String className) {/* w w w . j a v a 2 s .com*/ String shortClassName = addImport(className); ProduceMethod method = generator.startProvides(name, shortClassName); ConstructorArgumentValues constructors = definition.getConstructorArgumentValues(); Map map = constructors.getIndexedArgumentValues(); for (int i = 0, size = map.size(); i < size; i++) { ValueHolder valueHolder = (ValueHolder) map.get(i); if (valueHolder != null) { Object value = valueHolder.getValue(); if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; String text = stringValue.getValue(); System.out.printf("param %s=\"%s\"\n", i, text); String expression = null; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + text + "\""; } method.addConstructorExpression(expression); } else if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addConstructorExpression(expression); } } } MutablePropertyValues propertyValues = definition.getPropertyValues(); PropertyValue[] propertyArray = propertyValues.getPropertyValues(); for (PropertyValue propertyValue : propertyArray) { String property = getSetterMethod(propertyValue); Object value = propertyValue.getConvertedValue(); if (value == null) { value = propertyValue.getValue(); } if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value; addChildBeanDefinition(generator, method, name, propertyValue, beanDefinitionHolder.getBeanDefinition()); } else if (value instanceof ChildBeanDefinition) { ChildBeanDefinition childBeanDefinition = (ChildBeanDefinition) value; addChildBeanDefinition(generator, method, name, propertyValue, childBeanDefinition); } else { if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; value = stringValue.getValue(); } String valueType = (value == null) ? null : value.getClass().getName(); System.out.printf("property %s=%s of type %s\n", property, value, valueType); String expression; if (value instanceof String) { String text = (String) value; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + value + "\""; } } else if (value == null) { expression = "null"; } else { expression = value.toString(); } method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } } }
From source file:org.guiceyfruit.spring.converter.SpringConverter.java
protected void generateBeanDefinition(ModuleGenerator generator, String name, BeanDefinition definition, String className) {//from w w w . jav a 2 s . c om String shortClassName = addImport(className); ProduceMethod method = generator.startProvides(name, shortClassName); ConstructorArgumentValues constructors = definition.getConstructorArgumentValues(); Map map = constructors.getIndexedArgumentValues(); for (int i = 0, size = map.size(); i < size; i++) { ValueHolder valueHolder = (ValueHolder) map.get(i); if (valueHolder != null) { Object value = valueHolder.getValue(); if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; String text = stringValue.getValue(); System.out.printf("param %s=\"%s\"\n", i, text); String expression = null; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + text + "\""; } method.addConstructorExpression(expression); } else if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addConstructorExpression(expression); } } } MutablePropertyValues propertyValues = definition.getPropertyValues(); PropertyValue[] propertyArray = propertyValues.getPropertyValues(); for (PropertyValue propertyValue : propertyArray) { String property = getSetterMethod(propertyValue); Object value = propertyValue.getConvertedValue(); if (value == null) { value = propertyValue.getValue(); } if (value instanceof BeanReference) { BeanReference reference = (BeanReference) value; String beanRef = reference.getBeanName(); // TODO String typeName = "Object"; String expression = addParameter(method, typeName, beanRef); method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value; addChildBeanDefinition(generator, method, name, propertyValue, beanDefinitionHolder.getBeanDefinition()); } else if (value instanceof ChildBeanDefinition) { ChildBeanDefinition childBeanDefinition = (ChildBeanDefinition) value; addChildBeanDefinition(generator, method, name, propertyValue, childBeanDefinition); } else { if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; value = stringValue.getValue(); } String valueType = (value == null) ? null : value.getClass().getName(); System.out.printf("property %s=%s of type %s\n", property, value, valueType); String expression; if (value instanceof String) { String text = (String) value; String namedParameter = namedParameter(text); if (namedParameter != null) { expression = addParameter(method, "String", namedParameter); } else { expression = "\"" + value + "\""; } } else if (value == null) { expression = "null"; } else { expression = value.toString(); } method.addMethodCall("answer", getSetterMethod(propertyValue), expression); } } }
From source file:com.brienwheeler.apps.schematool.SchemaToolBean.java
@SuppressWarnings("unchecked") private PersistenceUnitManager simulateDefaultPum(NonInitializingClassPathXmlApplicationContext context, BeanDefinition emfBeanDef) {//from ww w. j a va2 s. c om // Simulate Spring's use of DefaultPersistenceUnitManager DefaultPersistenceUnitManager defpum = new DefaultPersistenceUnitManager(); // Set the location of the persistence XML -- when using the DPUM, // you can only set one persistence XML location on the EntityManagerFactory PropertyValue locationProperty = emfBeanDef.getPropertyValues().getPropertyValue("persistenceXmlLocation"); if (locationProperty == null || !(locationProperty.getValue() instanceof TypedStringValue)) { throw new RuntimeException( "no property 'persistenceXmlLocation' defined on bean: " + emfContextBeanName); } // Since PersistenceUnitPostProcessors may do things like set properties // onto the persistence unit, we need to instantiate them here so that // they get called when preparePersistenceUnitInfos() executes PropertyValue puiPostProcProperty = emfBeanDef.getPropertyValues() .getPropertyValue("persistenceUnitPostProcessors"); if (puiPostProcProperty != null && puiPostProcProperty.getValue() instanceof ManagedList) { List<PersistenceUnitPostProcessor> postProcessors = new ArrayList<PersistenceUnitPostProcessor>(); for (BeanDefinitionHolder postProcBeanDef : (ManagedList<BeanDefinitionHolder>) puiPostProcProperty .getValue()) { String beanName = postProcBeanDef.getBeanName(); BeanDefinition beanDefinition = postProcBeanDef.getBeanDefinition(); PersistenceUnitPostProcessor postProcessor = (PersistenceUnitPostProcessor) context .createBean(beanName, beanDefinition); postProcessors.add(postProcessor); } defpum.setPersistenceUnitPostProcessors( postProcessors.toArray(new PersistenceUnitPostProcessor[postProcessors.size()])); } defpum.setPersistenceXmlLocation(((TypedStringValue) locationProperty.getValue()).getValue()); defpum.preparePersistenceUnitInfos(); return defpum; }