Java Code Examples for org.springframework.beans.factory.support.AbstractBeanDefinition#setScope()

The following examples show how to use org.springframework.beans.factory.support.AbstractBeanDefinition#setScope() . 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: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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");
}
 
Example 7
Source File: CustomizeBeanFactoryPostProcessor.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: GroovyBeanDefinitionWrapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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);
		}
	}
}
 
Example 9
Source File: BeanDefinitionParserDelegate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: GroovyBeanDefinitionWrapper.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
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);
		}
	}
}
 
Example 11
Source File: GroovyBeanDefinitionWrapper.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
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);
		}
	}
}
 
Example 12
Source File: BeanDefinitionParserDelegate.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: DataSourceAutoConfiguration.java    From spring-boot-starter-dao with Apache License 2.0 4 votes vote down vote up
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");
	}
}
 
Example 14
Source File: BeanDefinitionParserDelegate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: BeanDefinitionParserDelegate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: GroovyBeanDefinitionWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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);
		}
	}
}
 
Example 17
Source File: BeanDefinitionParserDelegate.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: GroovyBeanDefinitionWrapper.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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);
		}
	}
}