List of usage examples for org.springframework.beans.factory.support AbstractBeanDefinition setAutowireMode
public void setAutowireMode(int autowireMode)
From source file:br.com.caelum.vraptor.ioc.spring.ComponentScanner.java
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override/* ww w . ja va2 s . c o m*/ protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { super.postProcessBeanDefinition(beanDefinition, beanName); beanDefinition.setPrimary(true); beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR); try { Class<?> componentType = Class.forName(beanDefinition.getBeanClassName()); if (ComponentFactory.class.isAssignableFrom(componentType) && checkCandidate(beanName, beanDefinition)) { registry.registerSingleton(beanDefinition.getBeanClassName(), new ComponentFactoryBean(container, componentType)); } } catch (ClassNotFoundException e) { logger.warn("Class " + beanDefinition.getBeanClassName() + " was not found during bean definition proccess"); } catch (ExceptionInInitializerError e) { // log and rethrow antipattern is needed, this is rally important logger.warn("Class " + beanDefinition.getBeanClassName() + " has problems during initialization", e.getCause()); throw e; } }
From source file:atunit.spring.SpringContainer.java
protected AbstractBeanDefinition defineAutowireBean(Class<?> type) throws Exception { AbstractBeanDefinition beandef = BeanDefinitionReaderUtils.createBeanDefinition(null, type.getName(), type.getClassLoader());//from w w w.j a v a 2 s .c o m beandef.setAutowireCandidate(true); beandef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_AUTODETECT); return beandef; }
From source file:hudson.util.spring.DefaultBeanConfiguration.java
public void setProperty(String property, Object newValue) { if (PARENT.equals(property)) { setParent(newValue);/*from w w w .j a v a 2s . c om*/ } else { AbstractBeanDefinition bd = getBeanDefinition(); if (AUTOWIRE.equals(property)) { if (BY_NAME.equals(newValue)) { bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME); } else if (BY_TYPE.equals(newValue)) { bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE); } else if (Boolean.TRUE.equals(newValue)) { bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME); } else if (BY_CONSTRUCTOR.equals(newValue)) { bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR); } } // constructorArgs else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) { ConstructorArgumentValues cav = new ConstructorArgumentValues(); List args = (List) newValue; for (Object e : args) { cav.addGenericArgumentValue(e); } bd.setConstructorArgumentValues(cav); } // destroyMethod else if (DESTROY_METHOD.equals(property)) { if (newValue != null) bd.setDestroyMethodName(newValue.toString()); } // factoryBean else if (FACTORY_BEAN.equals(property)) { if (newValue != null) bd.setFactoryBeanName(newValue.toString()); } // factoryMethod else if (FACTORY_METHOD.equals(property)) { if (newValue != null) bd.setFactoryMethodName(newValue.toString()); } // initMethod else if (INIT_METHOD.equals(property)) { if (newValue != null) bd.setInitMethodName(newValue.toString()); } else if (wrapper.isWritableProperty(property)) { wrapper.setPropertyValue(property, newValue); } // autowire else { super.setProperty(property, newValue); } } }
From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Apply the attributes of the given bean element to the given bean * definition. * @param ele bean declaration element/* w w w .j a v a2 s .co m*/ * @param beanName bean name * @param containingBean containing bean definition * @return a bean definition initialized according to the bean element attributes */ public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName, @Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) { if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) { error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele); } else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) { bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE)); } else if (containingBean != null) { // Take default from containing bean in case of an inner bean definition. bd.setScope(containingBean.getScope()); } if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) { bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE))); } String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE); if (DEFAULT_VALUE.equals(lazyInit)) { lazyInit = this.defaults.getLazyInit(); } bd.setLazyInit(TRUE_VALUE.equals(lazyInit)); String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE); bd.setAutowireMode(getAutowireMode(autowire)); if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) { String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE); bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS)); } String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE); if ("".equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) { String candidatePattern = this.defaults.getAutowireCandidates(); if (candidatePattern != null) { String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern); bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName)); } } else { bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate)); } if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) { bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE))); } if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) { String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE); bd.setInitMethodName(initMethodName); } else if (this.defaults.getInitMethod() != null) { bd.setInitMethodName(this.defaults.getInitMethod()); bd.setEnforceInitMethod(false); } if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) { String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE); bd.setDestroyMethodName(destroyMethodName); } else if (this.defaults.getDestroyMethod() != null) { bd.setDestroyMethodName(this.defaults.getDestroyMethod()); bd.setEnforceDestroyMethod(false); } if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) { bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE)); } if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) { bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE)); } return bd; }
From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Parse the BeanDefinition itself, without regard to name or aliases. *//*from ww w . j a v a 2 s .co m*/ protected BeanDefinition parseBeanDefinitionElement(Element ele, String beanName) throws BeanDefinitionStoreException { String className = null; if (ele.hasAttribute(CLASS_ATTRIBUTE)) { className = ele.getAttribute(CLASS_ATTRIBUTE).trim(); } String parent = null; if (ele.hasAttribute(PARENT_ATTRIBUTE)) { parent = ele.getAttribute(PARENT_ATTRIBUTE); } try { ConstructorArgumentValues cargs = parseConstructorArgElements(ele, beanName); MutablePropertyValues pvs = parsePropertyElements(ele, beanName); AbstractBeanDefinition bd = BeanDefinitionReaderUtils.createBeanDefinition(className, parent, cargs, pvs, getBeanDefinitionReader().getBeanClassLoader()); if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) { String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE); bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BEAN_NAME_DELIMITERS)); } if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) { bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE)); } if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) { bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE)); } String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE); if (DEFAULT_VALUE.equals(dependencyCheck)) { dependencyCheck = getDefaultDependencyCheck(); } bd.setDependencyCheck(getDependencyCheck(dependencyCheck)); String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE); if (DEFAULT_VALUE.equals(autowire)) { autowire = getDefaultAutowire(); } bd.setAutowireMode(getAutowireMode(autowire)); if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) { String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE); if (!"".equals(initMethodName)) { bd.setInitMethodName(initMethodName); } } else { if (getDefaultInitMethod() != null) { bd.setInitMethodName(getDefaultInitMethod()); bd.setEnforceInitMethod(false); } } if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) { String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE); if (!"".equals(destroyMethodName)) { bd.setDestroyMethodName(destroyMethodName); } } else { if (getDefaultDestroyMethod() != null) { bd.setDestroyMethodName(getDefaultDestroyMethod()); bd.setEnforceDestroyMethod(false); } } parseLookupOverrideSubElements(ele, beanName, bd.getMethodOverrides()); parseReplacedMethodSubElements(ele, beanName, bd.getMethodOverrides()); bd.setResourceDescription(getResource().getDescription()); if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) { bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE))); } if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) { bd.setSingleton(TRUE_VALUE.equals(ele.getAttribute(SINGLETON_ATTRIBUTE))); } String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE); if (DEFAULT_VALUE.equals(lazyInit) && bd.isSingleton()) { // Just apply default to singletons, as lazy-init has no meaning for prototypes. lazyInit = getDefaultLazyInit(); } bd.setLazyInit(TRUE_VALUE.equals(lazyInit)); return bd; } catch (BeanDefinitionStoreException ex) { throw ex; } catch (ClassNotFoundException ex) { throw new BeanDefinitionStoreException(getResource(), beanName, "Bean class [" + className + "] not found", ex); } catch (NoClassDefFoundError err) { throw new BeanDefinitionStoreException(getResource(), beanName, "Class that bean class [" + className + "] depends on not found", err); } catch (Throwable ex) { throw new BeanDefinitionStoreException(getResource(), beanName, "Unexpected failure during bean definition parsing", ex); } }