Java Code Examples for org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerBeanDefinition()

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerBeanDefinition() . 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: AbstractHasorDefinitionParser.java    From hasor with Apache License 2.0 6 votes vote down vote up
/**摘抄 Spring 源码,将Bean注册到容器中*/
private BeanDefinition registerBean(Element element, ParserContext parserContext, NamedNodeMap attributes, AbstractBeanDefinition definition) {
    if (!parserContext.isNested()) {
        try {
            String id = beanID(element, attributes);
            if (!StringUtils.hasText(id)) {
                parserContext.getReaderContext().error(beanID(element, attributes) + " is undefined. for element '" + element.getLocalName(), element);
            }
            BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id);
            BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
            parserContext.registerComponent(new BeanComponentDefinition(holder));
        } catch (BeanDefinitionStoreException ex) {
            parserContext.getReaderContext().error(ex.getMessage(), element);
            return null;
        }
    }
    return definition;
}
 
Example 2
Source File: DefaultBeanDefinitionDocumentReader.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Process the given bean element, parsing the bean definition
 * and registering it with the registry.
 */
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
	// 注释 1.15 解析 bean 名称的元素
	BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
	if (bdHolder != null) {
		bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
		try {
			// Register the final decorated instance. (注释 1.16 注册最后修饰后的实例)
			BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
		}
		catch (BeanDefinitionStoreException ex) {
			getReaderContext().error("Failed to register bean definition with name '" +
					bdHolder.getBeanName() + "'", ele, ex);
		}
		// Send registration event. 通知相关的监听器,表示这个 bean 已经加载完成
		getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
	}
}
 
Example 3
Source File: DefaultBeanDefinitionDocumentReader.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Process the given bean element, parsing the bean definition
 * and registering it with the registry.
 */
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
	BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
	if (bdHolder != null) {
		bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
		try {
			// Register the final decorated instance.
			BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
		}
		catch (BeanDefinitionStoreException ex) {
			getReaderContext().error("Failed to register bean definition with name '" +
					bdHolder.getBeanName() + "'", ele, ex);
		}
		// Send registration event.
		getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
	}
}
 
Example 4
Source File: DefaultBeanDefinitionDocumentReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Process the given bean element, parsing the bean definition
 * and registering it with the registry.
 */
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
	BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
	if (bdHolder != null) {
		bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
		try {
			// Register the final decorated instance.
			BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
		}
		catch (BeanDefinitionStoreException ex) {
			getReaderContext().error("Failed to register bean definition with name '" +
					bdHolder.getBeanName() + "'", ele, ex);
		}
		// Send registration event.
		getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
	}
}
 
Example 5
Source File: DefaultBeanDefinitionDocumentReader.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Process the given bean element, parsing the bean definition
 * and registering it with the registry.
 */
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
	BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
	if (bdHolder != null) {
		bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
		try {
			// Register the final decorated instance.
			BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
		}
		catch (BeanDefinitionStoreException ex) {
			getReaderContext().error("Failed to register bean definition with name '" +
					bdHolder.getBeanName() + "'", ele, ex);
		}
		// Send registration event.
		getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
	}
}
 
Example 6
Source File: AnnotatedBeanDefinitionReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 * @param annotatedClass the class of the bean
 * @param name an explicit name for the bean
 * @param supplier a callback for creating an instance of the bean
 * (may be {@code null})
 * @param qualifiers specific qualifier annotations to consider, if any,
 * in addition to qualifiers at the bean class level
 * @param customizers one or more callbacks for customizing the factory's
 * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
 * @since 5.0
 */
private <T> void doRegisterBean(Class<T> annotatedClass, @Nullable String name,
		@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
		@Nullable BeanDefinitionCustomizer[] customizers) {

	AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
	if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
		return;
	}

	abd.setInstanceSupplier(supplier);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
	abd.setScope(scopeMetadata.getScopeName());
	String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));

	AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
	if (qualifiers != null) {
		for (Class<? extends Annotation> qualifier : qualifiers) {
			if (Primary.class == qualifier) {
				abd.setPrimary(true);
			}
			else if (Lazy.class == qualifier) {
				abd.setLazyInit(true);
			}
			else {
				abd.addQualifier(new AutowireCandidateQualifier(qualifier));
			}
		}
	}
	if (customizers != null) {
		for (BeanDefinitionCustomizer customizer : customizers) {
			customizer.customize(abd);
		}
	}

	BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
	definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
 
Example 7
Source File: AnnotatedBeanDefinitionReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 * @param annotatedClass the class of the bean
 * @param instanceSupplier a callback for creating an instance of the bean
 * (may be {@code null})
 * @param name an explicit name for the bean
 * @param qualifiers specific qualifier annotations to consider, if any,
 * in addition to qualifiers at the bean class level
 * @param definitionCustomizers one or more callbacks for customizing the
 * factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
 * @since 5.0
 */
<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
		@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {

	AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
	if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
		return;
	}

	abd.setInstanceSupplier(instanceSupplier);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
	abd.setScope(scopeMetadata.getScopeName());
	String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));

	AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
	if (qualifiers != null) {
		for (Class<? extends Annotation> qualifier : qualifiers) {
			if (Primary.class == qualifier) {
				abd.setPrimary(true);
			}
			else if (Lazy.class == qualifier) {
				abd.setLazyInit(true);
			}
			else {
				abd.addQualifier(new AutowireCandidateQualifier(qualifier));
			}
		}
	}
	for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
		customizer.customize(abd);
	}

	BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
	definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
 
Example 8
Source File: ContextConfigurationUtils.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
public static void registerRegionProvider(BeanDefinitionRegistry registry,
		boolean autoDetect, boolean useDefaultRegionChain, String configuredRegion) {
	if (autoDetect && StringUtils.hasText(configuredRegion)) {
		throw new IllegalArgumentException(
				"No region must be configured if autoDetect is defined as true");
	}

	AbstractBeanDefinition beanDefinition;

	if (autoDetect) {
		beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
				useDefaultRegionChain ? DefaultAwsRegionProviderChainDelegate.class
						: Ec2MetadataRegionProvider.class)
				.getBeanDefinition();
	}
	else if (StringUtils.hasText(configuredRegion)) {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder
				.genericBeanDefinition(StaticRegionProvider.class);
		builder.addConstructorArgValue(configuredRegion);
		beanDefinition = builder.getBeanDefinition();
	}
	else {
		throw new IllegalArgumentException(
				"Region must be manually configured or autoDetect enabled");
	}

	BeanDefinitionReaderUtils.registerBeanDefinition(
			new BeanDefinitionHolder(beanDefinition, REGION_PROVIDER_BEAN_NAME),
			registry);
	AmazonWebserviceClientConfigurationUtils.replaceDefaultRegionProvider(registry,
			REGION_PROVIDER_BEAN_NAME);
}
 
Example 9
Source File: JupiterBeanDefinitionParser.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private BeanDefinition registerBean(RootBeanDefinition definition, Element element, ParserContext parserContext) {
    String id = element.getAttribute("id");
    if (Strings.isNullOrEmpty(id)) {
        id = beanClass.getSimpleName();
    }
    if (parserContext.getRegistry().containsBeanDefinition(id)) {
        throw new IllegalStateException("Duplicate jupiter bean id: " + id);
    }

    BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id);
    BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());

    return definition;
}
 
Example 10
Source File: StateHandlerAnnotationBeanFactoryPostProcessor.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void configureDefaultActivitiRegistry(String registryBeanName, BeanDefinitionRegistry registry) {


		if (!beanAlreadyConfigured(registry, registryBeanName, ActivitiStateHandlerRegistry.class)) {
			String registryName =ActivitiStateHandlerRegistry.class.getName();
			log.info( "registering a " + registryName + " instance under bean name "+ ActivitiContextUtils.ACTIVITI_REGISTRY_BEAN_NAME+ ".");

			RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
			rootBeanDefinition.setBeanClassName( registryName );
			rootBeanDefinition.getPropertyValues().addPropertyValue("processEngine", this.processEngine);

			BeanDefinitionHolder holder = new BeanDefinitionHolder(rootBeanDefinition, registryBeanName);
			BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
		}
	}
 
Example 11
Source File: AnnotatedBeanDefinitionReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 * @param annotatedClass the class of the bean
 * @param name an explicit name for the bean
 * @param qualifiers specific qualifier annotations to consider,
 * in addition to qualifiers at the bean class level
 */
@SuppressWarnings("unchecked")
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
	AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
	if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
		return;
	}

	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
	abd.setScope(scopeMetadata.getScopeName());
	String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
	AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
	if (qualifiers != null) {
		for (Class<? extends Annotation> qualifier : qualifiers) {
			if (Primary.class == qualifier) {
				abd.setPrimary(true);
			}
			else if (Lazy.class == qualifier) {
				abd.setLazyInit(true);
			}
			else {
				abd.addQualifier(new AutowireCandidateQualifier(qualifier));
			}
		}
	}

	BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
	definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
 
Example 12
Source File: ZookeeperUnifiedConfigurer.java    From easyooo-framework with Apache License 2.0 4 votes vote down vote up
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
 
Example 13
Source File: ParserContext.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
public void registerBeanComponent(BeanComponentDefinition component) {
	BeanDefinitionReaderUtils.registerBeanDefinition(component, getRegistry());
	registerComponent(component);
}
 
Example 14
Source File: ClassPathBeanDefinitionScanner.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Register the specified bean with the given registry.
 * <p>Can be overridden in subclasses, e.g. to adapt the registration
 * process or to register further bean definitions for each scanned bean.
 * @param definitionHolder the bean definition plus bean name for the bean
 * @param registry the BeanDefinitionRegistry to register the bean with
 */
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
 
Example 15
Source File: AbstractBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Register the supplied {@link BeanDefinitionHolder bean} with the supplied
 * {@link BeanDefinitionRegistry registry}.
 * <p>Subclasses can override this method to control whether or not the supplied
 * {@link BeanDefinitionHolder bean} is actually even registered, or to
 * register even more beans.
 * <p>The default implementation registers the supplied {@link BeanDefinitionHolder bean}
 * with the supplied {@link BeanDefinitionRegistry registry} only if the {@code isNested}
 * parameter is {@code false}, because one typically does not want inner beans
 * to be registered as top level beans.
 * @param definition the bean definition to be registered
 * @param registry the registry that the bean is to be registered with
 * @see BeanDefinitionReaderUtils#registerBeanDefinition(BeanDefinitionHolder, BeanDefinitionRegistry)
 */
protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry);
}
 
Example 16
Source File: ClassPathBeanDefinitionScanner.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Register the specified bean with the given registry.
 * <p>Can be overridden in subclasses, e.g. to adapt the registration
 * process or to register further bean definitions for each scanned bean.
 * @param definitionHolder the bean definition plus bean name for the bean
 * @param registry the BeanDefinitionRegistry to register the bean with
 */
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
 
Example 17
Source File: AbstractBeanDefinitionParser.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Register the supplied {@link BeanDefinitionHolder bean} with the supplied
 * {@link BeanDefinitionRegistry registry}.
 * <p>Subclasses can override this method to control whether or not the supplied
 * {@link BeanDefinitionHolder bean} is actually even registered, or to
 * register even more beans.
 * <p>The default implementation registers the supplied {@link BeanDefinitionHolder bean}
 * with the supplied {@link BeanDefinitionRegistry registry} only if the {@code isNested}
 * parameter is {@code false}, because one typically does not want inner beans
 * to be registered as top level beans.
 * @param definition the bean definition to be registered
 * @param registry the registry that the bean is to be registered with
 * @see BeanDefinitionReaderUtils#registerBeanDefinition(BeanDefinitionHolder, BeanDefinitionRegistry)
 */
protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry);
}
 
Example 18
Source File: ClassPathBeanDefinitionScanner.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Register the specified bean with the given registry.
 * <p>Can be overridden in subclasses, e.g. to adapt the registration
 * process or to register further bean definitions for each scanned bean.
 * @param definitionHolder the bean definition plus bean name for the bean
 * @param registry the BeanDefinitionRegistry to register the bean with
 */
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
 
Example 19
Source File: AbstractBeanDefinitionParser.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Register the supplied {@link BeanDefinitionHolder bean} with the supplied
 * {@link BeanDefinitionRegistry registry}.
 * <p>Subclasses can override this method to control whether or not the supplied
 * {@link BeanDefinitionHolder bean} is actually even registered, or to
 * register even more beans.
 * <p>The default implementation registers the supplied {@link BeanDefinitionHolder bean}
 * with the supplied {@link BeanDefinitionRegistry registry} only if the {@code isNested}
 * parameter is {@code false}, because one typically does not want inner beans
 * to be registered as top level beans.
 * @param definition the bean definition to be registered
 * @param registry the registry that the bean is to be registered with
 * @see BeanDefinitionReaderUtils#registerBeanDefinition(BeanDefinitionHolder, BeanDefinitionRegistry)
 */
protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry);
}
 
Example 20
Source File: AbstractBeanDefinitionParser.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Register the supplied {@link BeanDefinitionHolder bean} with the supplied
 * {@link BeanDefinitionRegistry registry}.
 * <p>Subclasses can override this method to control whether or not the supplied
 * {@link BeanDefinitionHolder bean} is actually even registered, or to
 * register even more beans.
 * <p>The default implementation registers the supplied {@link BeanDefinitionHolder bean}
 * with the supplied {@link BeanDefinitionRegistry registry} only if the {@code isNested}
 * parameter is {@code false}, because one typically does not want inner beans
 * to be registered as top level beans.
 * @param definition the bean definition to be registered
 * @param registry the registry that the bean is to be registered with
 * @see BeanDefinitionReaderUtils#registerBeanDefinition(BeanDefinitionHolder, BeanDefinitionRegistry)
 */
protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) {
	BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry);
}