org.springframework.beans.factory.support.AbstractBeanDefinition#setScope ( )源码实例Demo

下面列出了org.springframework.beans.factory.support.AbstractBeanDefinition#setScope ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() {
	String theChildScope = "bonanza!";

	RootBeanDefinition parent = new RootBeanDefinition();
	parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);

	AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
	child.setBeanClass(TestBean.class);
	child.setScope(theChildScope);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parent);
	factory.registerBeanDefinition("child", child);

	AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
	assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
 
@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() {
	String theChildScope = "bonanza!";

	RootBeanDefinition parent = new RootBeanDefinition();
	parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);

	AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
	child.setBeanClass(TestBean.class);
	child.setScope(theChildScope);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parent);
	factory.registerBeanDefinition("child", child);

	AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
	assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
 
@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() throws Exception {
	String theChildScope = "bonanza!";

	RootBeanDefinition parent = new RootBeanDefinition();
	parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);

	AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
	child.setBeanClass(TestBean.class);
	child.setScope(theChildScope);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parent);
	factory.registerBeanDefinition("child", child);

	AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
	assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
 
@Test(expected = IllegalStateException.class)
public void testScopingBeanToUnregisteredScopeResultsInAnException() {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
	AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
	beanDefinition.setScope("he put himself so low could hardly look me in the face");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("testBean", beanDefinition);
	factory.getBean("testBean");
}
 
@Test(expected = IllegalStateException.class)
public void testScopingBeanToUnregisteredScopeResultsInAnException() {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
	AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
	beanDefinition.setScope("he put himself so low could hardly look me in the face");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("testBean", beanDefinition);
	factory.getBean("testBean");
}
 
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    AbstractBeanDefinition abstractBeanDefinition = (AbstractBeanDefinition) beanFactory.getBeanDefinition("calculateService");

    MutablePropertyValues pv =  abstractBeanDefinition.getPropertyValues();
    pv.addPropertyValue("desc", "Desc is changed from bean factory post processor");
    abstractBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
}
 
@Test(expected = IllegalStateException.class)
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
	AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
	beanDefinition.setScope("he put himself so low could hardly look me in the face");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("testBean", beanDefinition);
	factory.getBean("testBean");
}
 
/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element
 * @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) {

	// 注释 2.5 老版本 1.x 用的是 singleton 属性,应该升级使用 scope 属性,报错
	if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
		error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);
	}
	// 解析 scope 领域属性
	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.
		// 在嵌入 beanDefinition 情况,并且没有单独指定 scope 属性,将会使用父类默认的属性
		bd.setScope(containingBean.getScope());
	}

	// 解析 abstract 属性
	if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
		bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
	}

	// 是否延迟初始化,就是在第一次被调用时才进行实例化
	String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
	if (isDefaultValue(lazyInit)) {
		lazyInit = this.defaults.getLazyInit();
	}
	bd.setLazyInit(TRUE_VALUE.equals(lazyInit));
	// 解析 autowire 属性
	String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
	bd.setAutowireMode(getAutowireMode(autowire));

	// 解析 depends-on 属性
	if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
		String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
		bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));
	}
	// 解析 autowire-candidate 属性
	String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
	if (isDefaultValue(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));
	}
	// 解析 primary 属性
	if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {
		bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));
	}
	// 解析 init-method 属性(这个方法,在初始化时会被调用~)
	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);
	}
	// 解析 destroy-method 属性
	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);
	}
	// 解析 factory-method 属性
	if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
		bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
	}
	// 解析 factory-bean 属性
	if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
		bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
	}

	return bd;
}
 
@Override
public void setProperty(String property, Object newValue) {
	if (PARENT.equals(property)) {
		setParent(newValue);
	}
	else {
		AbstractBeanDefinition bd = getBeanDefinition();
		if (AUTOWIRE.equals(property)) {
			if ("byName".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
			}
			else if ("byType".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
			}
			else if ("constructor".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
			}
			else if (Boolean.TRUE.equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
			}
		}
		// constructorArgs
		else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
			ConstructorArgumentValues cav = new ConstructorArgumentValues();
			List args = (List) newValue;
			for (Object arg : args) {
				cav.addGenericArgumentValue(arg);
			}
			bd.setConstructorArgumentValues(cav);
		}
		// 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());
			}
		}
		// destroyMethod
		else if (DESTROY_METHOD.equals(property)) {
			if (newValue != null) {
				bd.setDestroyMethodName(newValue.toString());
			}
		}
		// singleton property
		else if (SINGLETON.equals(property)) {
			bd.setScope(Boolean.TRUE.equals(newValue) ?
					BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
		}
		else if (this.definitionWrapper.isWritableProperty(property)) {
			this.definitionWrapper.setPropertyValue(property, newValue);
		}
		else {
			super.setProperty(property, newValue);
		}
	}
}
 
/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element
 * @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 (isDefaultValue(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 (isDefaultValue(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;
}
 
@Override
public void setProperty(String property, Object newValue) {
	if (PARENT.equals(property)) {
		setParent(newValue);
	}
	else {
		AbstractBeanDefinition bd = getBeanDefinition();
		if (AUTOWIRE.equals(property)) {
			if ("byName".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
			}
			else if ("byType".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
			}
			else if ("constructor".equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
			}
			else if (Boolean.TRUE.equals(newValue)) {
				bd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
			}
		}
		// constructorArgs
		else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
			ConstructorArgumentValues cav = new ConstructorArgumentValues();
			List args = (List) newValue;
			for (Object arg : args) {
				cav.addGenericArgumentValue(arg);
			}
			bd.setConstructorArgumentValues(cav);
		}
		// 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());
			}
		}
		// destroyMethod
		else if (DESTROY_METHOD.equals(property)) {
			if (newValue != null) {
				bd.setDestroyMethodName(newValue.toString());
			}
		}
		// singleton property
		else if (SINGLETON.equals(property)) {
			bd.setScope(Boolean.TRUE.equals(newValue) ?
					BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
		}
		else if (this.definitionWrapper.isWritableProperty(property)) {
			this.definitionWrapper.setPropertyValue(property, newValue);
		}
		else {
			super.setProperty(property, newValue);
		}
	}
}
 
源代码12 项目: lams   文件: BeanDefinitionParserDelegate.java
/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element
 * @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,
		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));

	String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
	bd.setDependencyCheck(getDependencyCheck(dependencyCheck));

	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);
		if (!"".equals(initMethodName)) {
			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;
}
 
源代码13 项目: lams   文件: GroovyBeanDefinitionWrapper.java
public void setProperty(String property, Object newValue) {
	if (PARENT.equals(property)) {
		setParent(newValue);
	}
	else {
		AbstractBeanDefinition bd = getBeanDefinition();
		if (AUTOWIRE.equals(property)) {
			if ("byName".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
			else if ("byType".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
			}
			else if ("constructor".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
			}
			else if (Boolean.TRUE.equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
		}
		// constructorArgs
		else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
			ConstructorArgumentValues cav = new ConstructorArgumentValues();
			List args = (List) newValue;
			for (Object arg : args) {
				cav.addGenericArgumentValue(arg);
			}
			bd.setConstructorArgumentValues(cav);
		}
		// 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());
			}
		}
		// destroyMethod
		else if (DESTROY_METHOD.equals(property)) {
			if (newValue != null) {
				bd.setDestroyMethodName(newValue.toString());
			}
		}
		// singleton property
		else if (SINGLETON.equals(property)) {
			bd.setScope(Boolean.TRUE.equals(newValue) ?
					BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
		}
		else if (this.definitionWrapper.isWritableProperty(property)) {
			this.definitionWrapper.setPropertyValue(property, newValue);
		}
		else {
			super.setProperty(property, newValue);
		}
	}
}
 
private void registryBean(String druidNodeName, MybatisNodeProperties nodeProperties,
		DruidProperties defaultProperties, Configuration configuration, BeanDefinitionRegistry registry) {
	if (nodeProperties == null) {
		return;
	}
	String mapperPackage = nodeProperties.getMapperPackage();
	String typeAliasesPackage = nodeProperties.getTypeAliasesPackage();
	String dbType = super.getDbType(nodeProperties.getMaster(), defaultProperties);
	Order order = nodeProperties.getOrder();
	Dialect dialect = nodeProperties.getDialect();
	Style style = nodeProperties.getStyle();
	if (null == dialect) {
		dialect = Dialect.valoueOfName(dbType);
	}
	Mapper mappers = nodeProperties.getMapper();
	if (mappers == null) {
		mappers = Mapper.valueOfDialect(dialect);
	}
	String basepackage = nodeProperties.getBasePackage();
	if (StringUtils.isEmpty(basepackage)) {
		log.warn("BasePackage为空,db配置异常,当前配置数据源对象的名字{}", druidNodeName);
		basepackage = "";
	}
	boolean primary = nodeProperties.isPrimary();
	String dataSourceName = druidNodeName + "DataSource";
	// String dataSourceMasterName = druidNodeName + "DataSource-Master";
	String jdbcTemplateName = druidNodeName + "JdbcTemplate";
	String transactionManagerName = druidNodeName;
	String sqlSessionFactoryBeanName = druidNodeName + "RegerSqlSessionFactoryBean";
	String scannerConfigurerName = druidNodeName + "RegerScannerConfigurer";

	AbstractBeanDefinition dataSource = super.createDataSource(nodeProperties, defaultProperties, dataSourceName);
	// AbstractBeanDefinition dataSourceMaster =
	// super.createDataSourceMaster(dataSourceName);
	AbstractBeanDefinition jdbcTemplate = super.createJdbcTemplate(dataSourceName);
	AbstractBeanDefinition transactionManager = super.createTransactionManager(dataSourceName);

	AbstractBeanDefinition sqlSessionFactoryBean = super.createSqlSessionFactoryBean(dataSourceName, mapperPackage,
			typeAliasesPackage, dialect, configuration);
	AbstractBeanDefinition scannerConfigurer = super.createScannerConfigurerBean(sqlSessionFactoryBeanName,
			basepackage, mappers, order, style,nodeProperties.getProperties());

	dataSource.setLazyInit(true);
	dataSource.setPrimary(primary);
	dataSource.setScope(BeanDefinition.SCOPE_SINGLETON);
	// dataSourceMaster.setLazyInit(true);
	// dataSourceMaster.setScope(BeanDefinition.SCOPE_SINGLETON);
	jdbcTemplate.setLazyInit(true);
	jdbcTemplate.setPrimary(primary);
	jdbcTemplate.setScope(BeanDefinition.SCOPE_SINGLETON);
	transactionManager.setLazyInit(true);
	transactionManager.setPrimary(primary);
	transactionManager.setScope(BeanDefinition.SCOPE_SINGLETON);
	sqlSessionFactoryBean.setLazyInit(true);
	sqlSessionFactoryBean.setPrimary(primary);
	sqlSessionFactoryBean.setScope(BeanDefinition.SCOPE_SINGLETON);
	scannerConfigurer.setLazyInit(true);
	scannerConfigurer.setPrimary(primary);
	scannerConfigurer.setScope(BeanDefinition.SCOPE_SINGLETON);

	registry.registerBeanDefinition(dataSourceName, dataSource);
	// registry.registerBeanDefinition(dataSourceMasterName,
	// dataSourceMaster);
	registry.registerBeanDefinition(jdbcTemplateName, jdbcTemplate);
	registry.registerBeanDefinition(transactionManagerName, transactionManager);
	registry.registerBeanDefinition(sqlSessionFactoryBeanName, sqlSessionFactoryBean);
	registry.registerBeanDefinition(scannerConfigurerName, scannerConfigurer);

	if (primary) {
		registry.registerAlias(dataSourceName, "dataSource");
		registry.registerAlias(jdbcTemplateName, "jdbcTemplate");
		registry.registerAlias(transactionManagerName, "transactionManager");
	}
}
 
源代码15 项目: blog_demos   文件: BeanDefinitionParserDelegate.java
/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element
 * @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,
		BeanDefinition containingBean, AbstractBeanDefinition bd) {

	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));

	String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
	bd.setDependencyCheck(getDependencyCheck(dependencyCheck));

	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);
		if (!"".equals(initMethodName)) {
			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);
		if (!"".equals(destroyMethodName)) {
			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;
}
 
源代码16 项目: blog_demos   文件: GroovyBeanDefinitionWrapper.java
public void setProperty(String property, Object newValue) {
	if (PARENT.equals(property)) {
		setParent(newValue);
	}
	else {
		AbstractBeanDefinition bd = getBeanDefinition();
		if (AUTOWIRE.equals(property)) {
			if ("byName".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
			else if ("byType".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
			}
			else if ("constructor".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
			}
			else if (Boolean.TRUE.equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
		}
		// constructorArgs
		else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
			ConstructorArgumentValues cav = new ConstructorArgumentValues();
			List args = (List) newValue;
			for (Object arg : args) {
				cav.addGenericArgumentValue(arg);
			}
			bd.setConstructorArgumentValues(cav);
		}
		// 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());
			}
		}
		// destroyMethod
		else if (DESTROY_METHOD.equals(property)) {
			if (newValue != null) {
				bd.setDestroyMethodName(newValue.toString());
			}
		}
		// singleton property
		else if (SINGLETON.equals(property)) {
			bd.setScope(Boolean.TRUE.equals(newValue) ?
					BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
		}
		else if (this.definitionWrapper.isWritableProperty(property)) {
			this.definitionWrapper.setPropertyValue(property, newValue);
		}
		else {
			super.setProperty(property, newValue);
		}
	}
}
 
public void setProperty(String property, Object newValue) {
	if (PARENT.equals(property)) {
		setParent(newValue);
	}
	else {
		AbstractBeanDefinition bd = getBeanDefinition();
		if (AUTOWIRE.equals(property)) {
			if ("byName".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
			else if ("byType".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
			}
			else if ("constructor".equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
			}
			else if (Boolean.TRUE.equals(newValue)) {
				bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
			}
		}
		// constructorArgs
		else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
			ConstructorArgumentValues cav = new ConstructorArgumentValues();
			List args = (List) newValue;
			for (Object arg : args) {
				cav.addGenericArgumentValue(arg);
			}
			bd.setConstructorArgumentValues(cav);
		}
		// 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());
			}
		}
		// destroyMethod
		else if (DESTROY_METHOD.equals(property)) {
			if (newValue != null) {
				bd.setDestroyMethodName(newValue.toString());
			}
		}
		// singleton property
		else if (SINGLETON.equals(property)) {
			bd.setScope(Boolean.TRUE.equals(newValue) ?
					BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
		}
		else if (this.definitionWrapper.isWritableProperty(property)) {
			this.definitionWrapper.setPropertyValue(property, newValue);
		}
		else {
			super.setProperty(property, newValue);
		}
	}
}
 
/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element
 * @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,
		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));

	String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
	bd.setDependencyCheck(getDependencyCheck(dependencyCheck));

	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);
		if (!"".equals(initMethodName)) {
			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;
}