Search in sources :

Example 36 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class BeanAnnotationAttributePropagationTests method beanDef.

private AbstractBeanDefinition beanDef(Class<?> configClass) {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(factory);
    return (AbstractBeanDefinition) factory.getBeanDefinition("foo");
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurationClassPostProcessor(org.springframework.context.annotation.ConfigurationClassPostProcessor)

Example 37 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class PropertyPlaceholderConfigurerTests method twoPlacholderConfigurers_withConflictingSettings.

/**
	 * Creates a scenario in which two PPCs are configured, each with different
	 * settings regarding resolving properties from the environment.
	 */
@Test
public void twoPlacholderConfigurers_withConflictingSettings() {
    String P2 = "p2";
    String P2_LOCAL_PROPS_VAL = "p2LocalPropsVal";
    String P2_SYSTEM_PROPS_VAL = "p2SystemPropsVal";
    String P2_SYSTEM_ENV_VAL = "p2SystemEnvVal";
    AbstractBeanDefinition p2BeanDef = rootBeanDefinition(TestBean.class).addPropertyValue("name", "${" + P1 + "}").addPropertyValue("country", "${" + P2 + "}").getBeanDefinition();
    bf.registerBeanDefinition("p1Bean", p1BeanDef);
    bf.registerBeanDefinition("p2Bean", p2BeanDef);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    System.setProperty(P2, P2_SYSTEM_PROPS_VAL);
    getModifiableSystemEnvironment().put(P2, P2_SYSTEM_ENV_VAL);
    Properties ppc2Properties = new Properties();
    ppc2Properties.put(P2, P2_LOCAL_PROPS_VAL);
    PropertyPlaceholderConfigurer ppc2 = new PropertyPlaceholderConfigurer();
    ppc2.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
    ppc2.setProperties(ppc2Properties);
    ppc2Properties = new Properties();
    ppc2Properties.setProperty(P2, P2_LOCAL_PROPS_VAL);
    ppc2.postProcessBeanFactory(bf);
    TestBean p1Bean = bf.getBean("p1Bean", TestBean.class);
    assertThat(p1Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
    TestBean p2Bean = bf.getBean("p2Bean", TestBean.class);
    assertThat(p2Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
    assertThat(p2Bean.getCountry(), equalTo(P2_SYSTEM_PROPS_VAL));
    System.clearProperty(P2);
    getModifiableSystemEnvironment().remove(P2);
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) TestBean(org.springframework.tests.sample.beans.TestBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 38 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-security by spring-projects.

the class AbstractUserDetailsServiceBeanDefinitionParser method resolveId.

private String resolveId(Element element, AbstractBeanDefinition definition, ParserContext pc) throws BeanDefinitionStoreException {
    String id = element.getAttribute("id");
    if (pc.isNested()) {
        // We're inside an <authentication-provider> element
        if (!StringUtils.hasText(id)) {
            id = pc.getReaderContext().generateBeanName(definition);
        }
        BeanDefinition container = pc.getContainingBeanDefinition();
        container.getPropertyValues().add("userDetailsService", new RuntimeBeanReference(id));
    }
    if (StringUtils.hasText(id)) {
        return id;
    }
    // If top level, use the default name or throw an exception if already used
    if (pc.getRegistry().containsBeanDefinition(BeanIds.USER_DETAILS_SERVICE)) {
        throw new BeanDefinitionStoreException("No id supplied and another " + "bean is already registered as " + BeanIds.USER_DETAILS_SERVICE);
    }
    return BeanIds.USER_DETAILS_SERVICE;
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 39 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class AbstractInterceptorDrivenBeanDefinitionDecorator method decorate.

@Override
public final BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definitionHolder, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    // get the root bean name - will be the name of the generated proxy factory bean
    String existingBeanName = definitionHolder.getBeanName();
    BeanDefinition targetDefinition = definitionHolder.getBeanDefinition();
    BeanDefinitionHolder targetHolder = new BeanDefinitionHolder(targetDefinition, existingBeanName + ".TARGET");
    // delegate to subclass for interceptor definition
    BeanDefinition interceptorDefinition = createInterceptorDefinition(node);
    // generate name and register the interceptor
    String interceptorName = existingBeanName + '.' + getInterceptorNameSuffix(interceptorDefinition);
    BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(interceptorDefinition, interceptorName), registry);
    BeanDefinitionHolder result = definitionHolder;
    if (!isProxyFactoryBeanDefinition(targetDefinition)) {
        // create the proxy definition
        RootBeanDefinition proxyDefinition = new RootBeanDefinition();
        // create proxy factory bean definition
        proxyDefinition.setBeanClass(ProxyFactoryBean.class);
        proxyDefinition.setScope(targetDefinition.getScope());
        proxyDefinition.setLazyInit(targetDefinition.isLazyInit());
        // set the target
        proxyDefinition.setDecoratedDefinition(targetHolder);
        proxyDefinition.getPropertyValues().add("target", targetHolder);
        // create the interceptor names list
        proxyDefinition.getPropertyValues().add("interceptorNames", new ManagedList<String>());
        // copy autowire settings from original bean definition.
        proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
        proxyDefinition.setPrimary(targetDefinition.isPrimary());
        if (targetDefinition instanceof AbstractBeanDefinition) {
            proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
        }
        // wrap it in a BeanDefinitionHolder with bean name
        result = new BeanDefinitionHolder(proxyDefinition, existingBeanName);
    }
    addInterceptorNameToList(interceptorName, result.getBeanDefinition());
    return result;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 40 with AbstractBeanDefinition

use of org.springframework.beans.factory.support.AbstractBeanDefinition in project spring-framework by spring-projects.

the class ConfigBeanDefinitionParser method parseAdvisor.

/**
	 * Parses the supplied {@code <advisor>} element and registers the resulting
	 * {@link org.springframework.aop.Advisor} and any resulting {@link org.springframework.aop.Pointcut}
	 * with the supplied {@link BeanDefinitionRegistry}.
	 */
private void parseAdvisor(Element advisorElement, ParserContext parserContext) {
    AbstractBeanDefinition advisorDef = createAdvisorBeanDefinition(advisorElement, parserContext);
    String id = advisorElement.getAttribute(ID);
    try {
        this.parseState.push(new AdvisorEntry(id));
        String advisorBeanName = id;
        if (StringUtils.hasText(advisorBeanName)) {
            parserContext.getRegistry().registerBeanDefinition(advisorBeanName, advisorDef);
        } else {
            advisorBeanName = parserContext.getReaderContext().registerWithGeneratedName(advisorDef);
        }
        Object pointcut = parsePointcutProperty(advisorElement, parserContext);
        if (pointcut instanceof BeanDefinition) {
            advisorDef.getPropertyValues().add(POINTCUT, pointcut);
            parserContext.registerComponent(new AdvisorComponentDefinition(advisorBeanName, advisorDef, (BeanDefinition) pointcut));
        } else if (pointcut instanceof String) {
            advisorDef.getPropertyValues().add(POINTCUT, new RuntimeBeanReference((String) pointcut));
            parserContext.registerComponent(new AdvisorComponentDefinition(advisorBeanName, advisorDef));
        }
    } finally {
        this.parseState.pop();
    }
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)54 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)6 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)4 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 BeanComponentDefinitionBuilder (org.springframework.data.config.BeanComponentDefinitionBuilder)4 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)3 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)3 ArrayList (java.util.ArrayList)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)2 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)2