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

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionBuilder#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: MqProducerBeanFactory.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
/**
 * Build producer bean.
 *
 * @param producerDto the producer dto
 */
public static void buildProducerBean(ReliableMessageRegisterDto producerDto) {

	String pid = producerDto.getProducerGroup();
	DefaultMQProducer mQProducer = DEFAULT_MQ_PRODUCER_MAP.get(pid);
	if (mQProducer == null) {
		String simpleName = producerDto.getProducerGroup();
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMQProducer.class);
		beanDefinitionBuilder.setScope(BeanDefinition.SCOPE_SINGLETON);
		beanDefinitionBuilder.addPropertyValue("producerGroup", producerDto.getProducerGroup());
		beanDefinitionBuilder.addPropertyValue("namesrvAddr", producerDto.getNamesrvAddr());
		beanDefinitionBuilder.setInitMethodName("start");
		beanDefinitionBuilder.setDestroyMethodName("shutdown");
		SpringContextHolder.getDefaultListableBeanFactory().registerBeanDefinition(simpleName, beanDefinitionBuilder.getBeanDefinition());
		DEFAULT_MQ_PRODUCER_MAP.put(simpleName, SpringContextHolder.getBean(simpleName));
	}
}
 
Example 2
Source File: UtilNamespaceHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	Properties parsedProps = parserContext.getDelegate().parsePropsElement(element);
	builder.addPropertyValue("properties", parsedProps);

	String location = element.getAttribute("location");
	if (StringUtils.hasLength(location)) {
		location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
		String[] locations = StringUtils.commaDelimitedListToStringArray(location);
		builder.addPropertyValue("locations", locations);
	}

	builder.addPropertyValue("ignoreResourceNotFound",
			Boolean.valueOf(element.getAttribute("ignore-resource-not-found")));

	builder.addPropertyValue("localOverride",
			Boolean.valueOf(element.getAttribute("local-override")));

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 3
Source File: DefaultsBeanDefinitionParser.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * Register default TablePanel Actions
 * @param element current element
 * @param parserContext parserContext
 * @return a new ComponentDefinition with default table action list.
 */
private ComponentDefinition registerDefaultTableActions(Element element, ParserContext parserContext) {
	ManagedList<Object> actions = new ManagedList<Object>(7);
	actions.add(createBeanDefinition(AddAction.class, parserContext));
	actions.add(createBeanDefinition(RefreshAction.class, parserContext));
	actions.add(createBeanDefinition(RemoveAction.class, parserContext));
	actions.add(createBeanDefinition(FindAction.class, parserContext));
	actions.add(createBeanDefinition(ClearFilterAction.class, parserContext));
	
	BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(ListFactoryBean.class);
	bdb.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	bdb.addPropertyValue("sourceList", actions);
	bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	BeanComponentDefinition bcd = new BeanComponentDefinition(bdb.getBeanDefinition(), 
			DEFAULT_TABLE_ACTIONS);
	registerBeanComponentDefinition(element, parserContext, bcd);
	return bcd;
}
 
Example 4
Source File: ScriptFactoryPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeScriptedBean() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());

	BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
	scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
	scriptedBeanBuilder.addPropertyReference("messenger", "messenger");

	final String BEAN_WITH_DEPENDENCY_NAME = "needsMessenger";
	ctx.registerBeanDefinition(BEAN_WITH_DEPENDENCY_NAME, scriptedBeanBuilder.getBeanDefinition());
	ctx.registerBeanDefinition("scriptProcessor", createScriptFactoryPostProcessor(true));
	ctx.refresh();

	Messenger messenger1 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	Messenger messenger2 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	assertNotSame(messenger1, messenger2);
}
 
Example 5
Source File: UtilNamespaceHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	Properties parsedProps = parserContext.getDelegate().parsePropsElement(element);
	builder.addPropertyValue("properties", parsedProps);

	String location = element.getAttribute("location");
	if (StringUtils.hasLength(location)) {
		location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
		String[] locations = StringUtils.commaDelimitedListToStringArray(location);
		builder.addPropertyValue("locations", locations);
	}

	builder.addPropertyValue("ignoreResourceNotFound",
			Boolean.valueOf(element.getAttribute("ignore-resource-not-found")));

	builder.addPropertyValue("localOverride",
			Boolean.valueOf(element.getAttribute("local-override")));

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 6
Source File: ScriptFactoryPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeScriptedBean() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());

	BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
	scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
	scriptedBeanBuilder.addPropertyReference("messenger", "messenger");

	final String BEAN_WITH_DEPENDENCY_NAME = "needsMessenger";
	ctx.registerBeanDefinition(BEAN_WITH_DEPENDENCY_NAME, scriptedBeanBuilder.getBeanDefinition());
	ctx.registerBeanDefinition("scriptProcessor", createScriptFactoryPostProcessor(true));
	ctx.refresh();

	Messenger messenger1 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	Messenger messenger2 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	assertNotSame(messenger1, messenger2);
}
 
Example 7
Source File: AbstractSingleBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Creates a {@link BeanDefinitionBuilder} instance for the
 * {@link #getBeanClass bean Class} and passes it to the
 * {@link #doParse} strategy method.
 * @param element the element that is to be parsed into a single BeanDefinition
 * @param parserContext the object encapsulating the current state of the parsing process
 * @return the BeanDefinition resulting from the parsing of the supplied {@link Element}
 * @throws IllegalStateException if the bean {@link Class} returned from
 * {@link #getBeanClass(org.w3c.dom.Element)} is {@code null}
 * @see #doParse
 */
@Override
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
	String parentName = getParentName(element);
	if (parentName != null) {
		builder.getRawBeanDefinition().setParentName(parentName);
	}
	Class<?> beanClass = getBeanClass(element);
	if (beanClass != null) {
		builder.getRawBeanDefinition().setBeanClass(beanClass);
	}
	else {
		String beanClassName = getBeanClassName(element);
		if (beanClassName != null) {
			builder.getRawBeanDefinition().setBeanClassName(beanClassName);
		}
	}
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	BeanDefinition containingBd = parserContext.getContainingBeanDefinition();
	if (containingBd != null) {
		// Inner bean definition must receive same scope as containing bean.
		builder.setScope(containingBd.getScope());
	}
	if (parserContext.isDefaultLazyInit()) {
		// Default-lazy-init applies to custom bean definitions as well.
		builder.setLazyInit(true);
	}
	// 注释 3.11 在这里调用了我们写的解析方法
	doParse(element, parserContext, builder);
	return builder.getBeanDefinition();
}
 
Example 8
Source File: UtilEncryptorBeanDefinitionParser.java    From jasypt with Apache License 2.0 5 votes vote down vote up
@Override
protected void doParse(final Element element, final BeanDefinitionBuilder builder) {
    
    processStringAttribute(element, builder, PARAM_PASSWORD, "password");
    
    String scope = element.getAttribute(SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        builder.setScope(scope);
    }
    
}
 
Example 9
Source File: UtilNamespaceHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	List<Object> parsedList = parserContext.getDelegate().parseListElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceList", parsedList);

	String listClass = element.getAttribute("list-class");
	if (StringUtils.hasText(listClass)) {
		builder.addPropertyValue("targetListClass", listClass);
	}

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 10
Source File: UtilNamespaceHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	Set<Object> parsedSet = parserContext.getDelegate().parseSetElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceSet", parsedSet);

	String setClass = element.getAttribute("set-class");
	if (StringUtils.hasText(setClass)) {
		builder.addPropertyValue("targetSetClass", setClass);
	}

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 11
Source File: ColumnBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
	beanDefinition.setScope("prototype");
	
	if (element.hasAttribute(RENDERER_ATTRIBUTE))
		beanDefinition.addPropertyReference(RENDERER_ATTRIBUTE, element.getAttribute(RENDERER_ATTRIBUTE));
}
 
Example 12
Source File: UtilNamespaceHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	List<Object> parsedList = parserContext.getDelegate().parseListElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceList", parsedList);

	String listClass = element.getAttribute("list-class");
	if (StringUtils.hasText(listClass)) {
		builder.addPropertyValue("targetListClass", listClass);
	}

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 13
Source File: UtilNamespaceHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	Map<Object, Object> parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceMap", parsedMap);

	String mapClass = element.getAttribute("map-class");
	if (StringUtils.hasText(mapClass)) {
		builder.addPropertyValue("targetMapClass", mapClass);
	}

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 14
Source File: UtilNamespaceHandler.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	String mapClass = element.getAttribute("map-class");
	Map<Object, Object> parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceMap", parsedMap);
	if (StringUtils.hasText(mapClass)) {
		builder.addPropertyValue("targetMapClass", mapClass);
	}
	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 15
Source File: UtilNamespaceHandler.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	String setClass = element.getAttribute("set-class");
	Set<Object> parsedSet = parserContext.getDelegate().parseSetElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceSet", parsedSet);
	if (StringUtils.hasText(setClass)) {
		builder.addPropertyValue("targetSetClass", setClass);
	}
	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 16
Source File: UtilNamespaceHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	Set<Object> parsedSet = parserContext.getDelegate().parseSetElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceSet", parsedSet);

	String setClass = element.getAttribute("set-class");
	if (StringUtils.hasText(setClass)) {
		builder.addPropertyValue("targetSetClass", setClass);
	}

	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	if (StringUtils.hasLength(scope)) {
		builder.setScope(scope);
	}
}
 
Example 17
Source File: ColumnBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
	beanDefinition.setScope("prototype");
	
	
	BeanDefinitionUtils.addPropertyReferenceIfNeeded(beanDefinition, element, COLUMN_GENERATOR_ATTRIBUTE);
	BeanDefinitionUtils.addPropertyReferenceIfNeeded(beanDefinition, element, RENDERER_ATTRIBUTE);
	BeanDefinitionUtils.addPropertyReferenceIfNeeded(beanDefinition, element, PROPERTY_EDITOR_ATTRIBUTE);
	
	if (element.hasAttribute(ALIGN_ATTRIBUTE))
		beanDefinition.addPropertyValue(ALIGN_ATTRIBUTE, getAlignValue(element.getAttribute(ALIGN_ATTRIBUTE)));
}
 
Example 18
Source File: RedissonNamespaceParserSupport.java    From redisson with Apache License 2.0 5 votes vote down vote up
public BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, ParserContext parserContext, Class<?> cls) {
    BeanDefinitionBuilder builder
            = BeanDefinitionBuilder.genericBeanDefinition();
    builder.getRawBeanDefinition().setBeanClass(cls);
    builder.getRawBeanDefinition()
            .setSource(parserContext.extractSource(element));
    if (parserContext.isNested()) {
        builder.setScope(parserContext.getContainingBeanDefinition()
                .getScope());
    }
    if (parserContext.isDefaultLazyInit()) {
        builder.setLazyInit(true);
    }
    return builder;
}
 
Example 19
Source File: UtilDigesterBeanDefinitionParser.java    From jasypt with Apache License 2.0 5 votes vote down vote up
@Override
protected void doParse(final Element element, final BeanDefinitionBuilder builder) {
    
    processStringAttribute(element, builder, PARAM_ALGORITHM, "algorithm");
    processBeanAttribute(element, builder, PARAM_CONFIG_BEAN, "config");
    processBeanAttribute(element, builder, PARAM_PROVIDER_BEAN, "provider");
    processStringAttribute(element, builder, PARAM_PROVIDER_NAME, "providerName");
    processStringAttribute(element, builder, PARAM_STRING_OUTPUT_TYPE, "stringOutputType");
    
    String scope = element.getAttribute(SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        builder.setScope(scope);
    }
    
}
 
Example 20
Source File: ListBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	List<Object> columns = parserContext.getDelegate().parseListElement(element, builder.getRawBeanDefinition());
	builder.addPropertyValue("sourceList", columns);
	
	String scope = element.getAttribute(SCOPE_ATTRIBUTE);
	
	if (StringUtils.isEmpty(scope))
		scope = defaultScope;

	builder.setScope(defaultScope);
}