Search in sources :

Example 96 with RootBeanDefinition

use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method beanDefinition.

private BeanDefinition beanDefinition(Object bean) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(getClass());
    beanDefinition.setFactoryMethodName("getBean");
    ConstructorArgumentValues constructorArguments = new ConstructorArgumentValues();
    constructorArguments.addGenericArgumentValue(bean);
    beanDefinition.setConstructorArgumentValues(constructorArguments);
    return beanDefinition;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 97 with RootBeanDefinition

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

the class AnnotationAsyncExecutionAspectTests method qualifiedAsyncMethodsAreRoutedToCorrectExecutor.

/*
	@Test
	public void methodReturningNonVoidNonFutureInAsyncClassGetsRoutedSynchronously() {
		ClassWithAsyncAnnotation obj = new ClassWithAsyncAnnotation();
		int returnValue = obj.return5();
		assertEquals(5, returnValue);
		assertEquals(0, executor.submitStartCounter);
		assertEquals(0, executor.submitCompleteCounter);
	}
	*/
@Test
public void qualifiedAsyncMethodsAreRoutedToCorrectExecutor() throws InterruptedException, ExecutionException {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
    AnnotationAsyncExecutionAspect.aspectOf().setBeanFactory(beanFactory);
    ClassWithQualifiedAsyncMethods obj = new ClassWithQualifiedAsyncMethods();
    Future<Thread> defaultThread = obj.defaultWork();
    assertThat(defaultThread.get(), not(Thread.currentThread()));
    assertThat(defaultThread.get().getName(), not(startsWith("e1-")));
    ListenableFuture<Thread> e1Thread = obj.e1Work();
    assertThat(e1Thread.get().getName(), startsWith("e1-"));
    CompletableFuture<Thread> e1OtherThread = obj.e1OtherWork();
    assertThat(e1OtherThread.get().getName(), startsWith("e1-"));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Test(org.junit.Test)

Example 98 with RootBeanDefinition

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

the class QualifierAnnotationAutowireCandidateResolver method checkQualifier.

/**
	 * Match the given qualifier annotation against the candidate bean definition.
	 */
protected boolean checkQualifier(BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {
    Class<? extends Annotation> type = annotation.annotationType();
    RootBeanDefinition bd = (RootBeanDefinition) bdHolder.getBeanDefinition();
    AutowireCandidateQualifier qualifier = bd.getQualifier(type.getName());
    if (qualifier == null) {
        qualifier = bd.getQualifier(ClassUtils.getShortName(type));
    }
    if (qualifier == null) {
        // First, check annotation on qualified element, if any
        Annotation targetAnnotation = getQualifiedElementAnnotation(bd, type);
        // Then, check annotation on factory method, if applicable
        if (targetAnnotation == null) {
            targetAnnotation = getFactoryMethodAnnotation(bd, type);
        }
        if (targetAnnotation == null) {
            RootBeanDefinition dbd = getResolvedDecoratedDefinition(bd);
            if (dbd != null) {
                targetAnnotation = getFactoryMethodAnnotation(dbd, type);
            }
        }
        if (targetAnnotation == null) {
            // Look for matching annotation on the target class
            if (getBeanFactory() != null) {
                try {
                    Class<?> beanType = getBeanFactory().getType(bdHolder.getBeanName());
                    if (beanType != null) {
                        targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(beanType), type);
                    }
                } catch (NoSuchBeanDefinitionException ex) {
                // Not the usual case - simply forget about the type check...
                }
            }
            if (targetAnnotation == null && bd.hasBeanClass()) {
                targetAnnotation = AnnotationUtils.getAnnotation(ClassUtils.getUserClass(bd.getBeanClass()), type);
            }
        }
        if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
            return true;
        }
    }
    Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
    if (attributes.isEmpty() && qualifier == null) {
        // If no attributes, the qualifier must be present
        return false;
    }
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        String attributeName = entry.getKey();
        Object expectedValue = entry.getValue();
        Object actualValue = null;
        // Check qualifier first
        if (qualifier != null) {
            actualValue = qualifier.getAttribute(attributeName);
        }
        if (actualValue == null) {
            // Fall back on bean definition attribute
            actualValue = bd.getAttribute(attributeName);
        }
        if (actualValue == null && attributeName.equals(AutowireCandidateQualifier.VALUE_KEY) && expectedValue instanceof String && bdHolder.matchesName((String) expectedValue)) {
            // Fall back on bean name (or alias) match
            continue;
        }
        if (actualValue == null && qualifier != null) {
            // Fall back on default, but only if the qualifier is present
            actualValue = AnnotationUtils.getDefaultValue(annotation, attributeName);
        }
        if (actualValue != null) {
            actualValue = typeConverter.convertIfNecessary(actualValue, expectedValue.getClass());
        }
        if (!expectedValue.equals(actualValue)) {
            return false;
        }
    }
    return true;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 99 with RootBeanDefinition

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

the class PrototypeBasedTargetSourceTests method testSerializability.

@Test
public void testSerializability() throws Exception {
    MutablePropertyValues tsPvs = new MutablePropertyValues();
    tsPvs.add("targetBeanName", "person");
    RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class);
    tsBd.setPropertyValues(tsPvs);
    MutablePropertyValues pvs = new MutablePropertyValues();
    RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class);
    bd.setPropertyValues(pvs);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("ts", tsBd);
    bf.registerBeanDefinition("person", bd);
    TestTargetSource cpts = (TestTargetSource) bf.getBean("ts");
    TargetSource serialized = (TargetSource) SerializationTestUtils.serializeAndDeserialize(cpts);
    assertTrue("Changed to SingletonTargetSource on deserialization", serialized instanceof SingletonTargetSource);
    SingletonTargetSource sts = (SingletonTargetSource) serialized;
    assertNotNull(sts.getTarget());
}
Also used : TargetSource(org.springframework.aop.TargetSource) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 100 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testGetBeanByTypeWithPrimary.

@Test
public void testGetBeanByTypeWithPrimary() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setLazyInit(true);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPrimary(true);
    lbf.registerBeanDefinition("bd1", bd1);
    lbf.registerBeanDefinition("bd2", bd2);
    TestBean bean = lbf.getBean(TestBean.class);
    assertThat(bean.getBeanName(), equalTo("bd2"));
    assertFalse(lbf.containsSingleton("bd1"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)696 Test (org.junit.Test)521 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)308 TestBean (org.springframework.tests.sample.beans.TestBean)159 ITestBean (org.springframework.tests.sample.beans.ITestBean)143 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)133 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)84 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)79 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)74 Element (org.w3c.dom.Element)47 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)46 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)46 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)36 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)31 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)30 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)30 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)28 Properties (java.util.Properties)26 ManagedList (org.springframework.beans.factory.support.ManagedList)25