Search in sources :

Example 11 with UnsatisfiedDependencyException

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

the class ComponentScanParserBeanDefinitionDefaultsTests method testAutowireByType.

@Test
public void testAutowireByType() {
    GenericApplicationContext context = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
    reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireByTypeTests.xml");
    try {
        context.refresh();
        fail("expected exception due to multiple matches for byType autowiring");
    } catch (UnsatisfiedDependencyException ex) {
    // expected
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 12 with UnsatisfiedDependencyException

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

the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFound.

@Test
public void testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFound() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setAutowiredAnnotationType(MyAutowired.class);
    bpp.setRequiredParameterName("optional");
    bpp.setRequiredParameterValue(false);
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationOptionalMethodResourceInjectionBean.class));
    TestBean tb1 = new TestBean();
    bf.registerSingleton("testBean1", tb1);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    try {
        bf.getBean("customBean");
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(CustomAnnotationOptionalMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
    }
    bf.destroySingletons();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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)

Example 13 with UnsatisfiedDependencyException

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

the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound.

@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setAutowiredAnnotationType(MyAutowired.class);
    bpp.setRequiredParameterName("optional");
    bpp.setRequiredParameterValue(false);
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationRequiredFieldResourceInjectionBean.class));
    TestBean tb1 = new TestBean();
    bf.registerSingleton("testBean1", tb1);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    try {
        bf.getBean("customBean");
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass());
    }
    bf.destroySingletons();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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)

Example 14 with UnsatisfiedDependencyException

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

the class AutowiredAnnotationBeanPostProcessorTests method testMethodInjectionWithMapAndMultipleMatches.

@Test
public void testMethodInjectionWithMapAndMultipleMatches() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
    bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));
    try {
        bf.getBean("annotatedBean");
        fail("should have failed, more than one bean of type");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(MapMethodInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
    }
    bf.destroySingletons();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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)

Example 15 with UnsatisfiedDependencyException

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

the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound.

@Test
public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setAutowiredAnnotationType(MyAutowired.class);
    bpp.setRequiredParameterName("optional");
    bpp.setRequiredParameterValue(false);
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationOptionalFieldResourceInjectionBean.class));
    TestBean tb1 = new TestBean();
    bf.registerSingleton("testBean1", tb1);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    try {
        bf.getBean("customBean");
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(CustomAnnotationOptionalFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass());
    }
    bf.destroySingletons();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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

UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)24 Test (org.junit.Test)17 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)9 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)7 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)5 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 LinkedHashSet (java.util.LinkedHashSet)4 BeansException (org.springframework.beans.BeansException)4 TypeMismatchException (org.springframework.beans.TypeMismatchException)4 InjectionPoint (org.springframework.beans.factory.InjectionPoint)4 TypeConverter (org.springframework.beans.TypeConverter)3 MethodParameter (org.springframework.core.MethodParameter)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 Constructor (java.lang.reflect.Constructor)2 PrivilegedAction (java.security.PrivilegedAction)2