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

The following examples show how to use org.springframework.beans.factory.support.AbstractBeanDefinition#setPrimary() . 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: DatasourceBeanDefinitionRegistry.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
                                                                              throws BeansException {
    BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder
        .genericBeanDefinition(HikariDataSource.class);
    AbstractBeanDefinition beanDefinition = definitionBuilder.getRawBeanDefinition();
    beanDefinition.setDestroyMethodName("close");
    beanDefinition.setPrimary(false);

    definitionBuilder.addPropertyValue("driverClassName", "org.h2.Driver");
    definitionBuilder
        .addPropertyValue(
            "jdbcUrl",
            "jdbc:mysql://1.1.1.1:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
    definitionBuilder.addPropertyValue("username", "sofa");
    definitionBuilder.addPropertyValue("password", "123456");

    registry.registerBeanDefinition("manualDataSource",
        definitionBuilder.getRawBeanDefinition());
}
 
Example 2
Source File: LoggerBeanNamesResolverTest.java    From eclair with Apache License 2.0 6 votes vote down vote up
@Test
public void resolveManyLoggersWithPrimary() {
    // given
    String beanName = "simpleLogger";
    String emptyLoggerName = "";
    String alias = "logger";
    String beanName2 = "simpleLogger2";
    String alias2 = "logger2";
    AbstractBeanDefinition primaryDefinition = givenLoggerBeanDefinition();
    primaryDefinition.setPrimary(true);
    GenericApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerBeanDefinition(beanName, primaryDefinition);
    applicationContext.getBeanFactory().registerAlias(beanName, alias);
    applicationContext.registerBeanDefinition(beanName2, givenLoggerBeanDefinition());
    applicationContext.getBeanFactory().registerAlias(beanName2, alias2);
    // when
    Set<String> namesByBeanName = loggerBeanNamesResolver.resolve(applicationContext, beanName);
    // then
    assertThat(namesByBeanName, hasSize(3));
    assertThat(namesByBeanName, containsInAnyOrder(beanName, emptyLoggerName, alias));
}
 
Example 3
Source File: AbstractRetrofitClientsRegistrar.java    From spring-cloud-square with Apache License 2.0 6 votes vote down vote up
private void registerRetrofitClient(BeanDefinitionRegistry registry,
								 AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
	String className = annotationMetadata.getClassName();
	BeanDefinitionBuilder definition = BeanDefinitionBuilder
			.genericBeanDefinition(getFactoryBeanClass());
	validate(attributes);
	definition.addPropertyValue("url", getUrl(attributes));
	String name = getName(attributes);
	definition.addPropertyValue("name", name);
	definition.addPropertyValue("type", className);
	definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

	String alias = name + "RetrofitClient";
	AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
	beanDefinition.setPrimary(true);

	String qualifier = getQualifier(attributes);
	if (StringUtils.hasText(qualifier)) {
		alias = qualifier;
	}

	BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
			new String[] { alias });
	BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
 
Example 4
Source File: FeignClientsRegistrar.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
private void registerFeignClient(BeanDefinitionRegistry registry,
		AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
	String className = annotationMetadata.getClassName();
	BeanDefinitionBuilder definition = BeanDefinitionBuilder
			.genericBeanDefinition(FeignClientFactoryBean.class);
	validate(attributes);
	definition.addPropertyValue("url", getUrl(attributes));
	definition.addPropertyValue("path", getPath(attributes));
	String name = getName(attributes);
	definition.addPropertyValue("name", name);
	String contextId = getContextId(attributes);
	definition.addPropertyValue("contextId", contextId);
	definition.addPropertyValue("type", className);
	definition.addPropertyValue("decode404", attributes.get("decode404"));
	definition.addPropertyValue("fallback", attributes.get("fallback"));
	definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
	definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

	String alias = contextId + "FeignClient";
	AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
	beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, className);

	// has a default, won't be null
	boolean primary = (Boolean) attributes.get("primary");

	beanDefinition.setPrimary(primary);

	String qualifier = getQualifier(attributes);
	if (StringUtils.hasText(qualifier)) {
		alias = qualifier;
	}

	BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
			new String[] { alias });
	BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
 
Example 5
Source File: NettyRpcRegistrar.java    From BootNettyRpc with Apache License 2.0 5 votes vote down vote up
private void registerRpcClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
    String className = annotationMetadata.getClassName();
    BeanDefinitionBuilder definition = BeanDefinitionBuilder
            .genericBeanDefinition( RpcClientEntity.class );
    definition.addPropertyValue( "name", attributes.get( "name" ) );
    definition.addPropertyValue( "isSyn", attributes.get( "isSyn" ) );
    definition.addPropertyValue( "host", attributes.get( "host" ) );
    definition.addPropertyValue( "port", attributes.get( "port" ) );
    definition.addPropertyValue( "rpcClz", attributes.get( "rpcClz" ) );
    definition.addPropertyValue( "traceIdIndex", attributes.get( "traceIdIndex" ) );
    definition.setAutowireMode( AbstractBeanDefinition.AUTOWIRE_BY_NAME );

    try {
        definition.addPropertyValue( "interfaze", Class.forName( className ) );
    } catch (ClassNotFoundException e) {
        LOG.error( "Get interface for name error", e );
    }
    // definition.setAutowireMode( AbstractBeanDefinition.AUTOWIRE_BY_TYPE );
    String alias = className;
    AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
    beanDefinition.setPrimary( false );
    definition.addPropertyValue( "interceptor", getInterceptor( beanDefinition.getPropertyValues() ) );

    BeanDefinitionHolder holder = new BeanDefinitionHolder( beanDefinition, className,
            new String[]{alias} );
    LOG.info( "registerRpcClient:" + className );
    BeanDefinitionReaderUtils.registerBeanDefinition( holder, registry );

}
 
Example 6
Source File: AbstractImportRegistrar.java    From onetwo with Apache License 2.0 5 votes vote down vote up
protected void registerComponent(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, AnnotationAttributes tagAttributes) {
	String className = annotationMetadata.getClassName();
	String beanName = resolveName(tagAttributes, className);
	if(logger.isInfoEnabled()){
		logger.info("register api client beanName: {}, class: {}", beanName, className);
	}
	
	BeanDefinitionBuilder definition = createComponentFactoryBeanBuilder(annotationMetadata, tagAttributes);
	
	String alias = beanName + "-" + getComponentAnnotationClass().getSimpleName();
	AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
	beanDefinition.setPrimary(true);
	BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, beanName, new String[] { alias });
	BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
 
Example 7
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 8
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 9
Source File: BladeFeignClientsRegistrar.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
	List<String> feignClients = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
	// 如果 spring.factories 里为空
	if (feignClients.isEmpty()) {
		return;
	}
	for (String className : feignClients) {
		try {
			Class<?> clazz = beanClassLoader.loadClass(className);
			AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, FeignClient.class);
			if (attributes == null) {
				continue;
			}
			// 如果已经存在该 bean,支持原生的 Feign
			if (registry.containsBeanDefinition(className)) {
				continue;
			}
			registerClientConfiguration(registry, getClientName(attributes), attributes.get("configuration"));

			validate(attributes);
			BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class);
			definition.addPropertyValue("url", getUrl(attributes));
			definition.addPropertyValue("path", getPath(attributes));
			String name = getName(attributes);
			definition.addPropertyValue("name", name);

			// 兼容最新版本的 spring-cloud-openfeign,尚未发布
			StringBuilder aliasBuilder = new StringBuilder(18);
			if (attributes.containsKey("contextId")) {
				String contextId = getContextId(attributes);
				aliasBuilder.append(contextId);
				definition.addPropertyValue("contextId", contextId);
			} else {
				aliasBuilder.append(name);
			}

			definition.addPropertyValue("type", className);
			definition.addPropertyValue("decode404", attributes.get("decode404"));
			definition.addPropertyValue("fallback", attributes.get("fallback"));
			definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
			definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

			AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();

			// alias
			String alias = aliasBuilder.append("FeignClient").toString();

			// has a default, won't be null
			boolean primary = (Boolean)attributes.get("primary");

			beanDefinition.setPrimary(primary);

			String qualifier = getQualifier(attributes);
			if (StringUtils.hasText(qualifier)) {
				alias = qualifier;
			}

			BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias });
			BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
 
Example 10
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 11
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 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: 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;
}