Search in sources :

Example 51 with XmlBeanDefinitionReader

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

the class ComponentScanParserBeanDefinitionDefaultsTests method testAutowireConstructor.

@Test
public void testAutowireConstructor() {
    GenericApplicationContext context = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
    reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireConstructorTests.xml");
    context.refresh();
    DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME);
    assertNotNull("constructor dependency should have been autowired", bean.getConstructorDependency());
    assertEquals("cd", bean.getConstructorDependency().getName());
    assertNull("property dependencies should not have been autowired", bean.getPropertyDependency1());
    assertNull("property dependencies should not have been autowired", bean.getPropertyDependency2());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 52 with XmlBeanDefinitionReader

use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader 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 53 with XmlBeanDefinitionReader

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

the class ComponentScanParserBeanDefinitionDefaultsTests method testDefaultInitAndDestroyMethodsNotDefined.

@Test
public void testDefaultInitAndDestroyMethodsNotDefined() {
    GenericApplicationContext context = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
    reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultWithNoOverridesTests.xml");
    context.refresh();
    DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME);
    assertFalse("bean should not have been initialized", bean.isInitialized());
    context.close();
    assertFalse("bean should not have been destroyed", bean.isDestroyed());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 54 with XmlBeanDefinitionReader

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

the class ComponentScanParserBeanDefinitionDefaultsTests method testAutowireByName.

@Test
public void testAutowireByName() {
    GenericApplicationContext context = new GenericApplicationContext();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
    reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireByNameTests.xml");
    context.refresh();
    DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME);
    assertNull("constructor dependency should not have been autowired", bean.getConstructorDependency());
    assertNull("propertyDependency1 should not have been autowired", bean.getPropertyDependency1());
    assertNotNull("propertyDependency2 should have been autowired", bean.getPropertyDependency2());
    assertEquals("pd2", bean.getPropertyDependency2().getName());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 55 with XmlBeanDefinitionReader

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

the class AutowiredConfigurationTests method testAutowiredSingleConstructorSupported.

@Test
public void testAutowiredSingleConstructorSupported() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
    GenericApplicationContext ctx = new GenericApplicationContext(factory);
    ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
    ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
    ctx.refresh();
    assertSame(ctx.getBean(AutowiredConstructorConfig.class).colour, ctx.getBean(Colour.class));
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ClassPathResource(org.springframework.core.io.ClassPathResource) Colour(org.springframework.tests.sample.beans.Colour) Test(org.junit.Test)

Aggregations

XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)152 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)82 Test (org.junit.Test)80 ClassPathResource (org.springframework.core.io.ClassPathResource)62 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)38 Before (org.junit.Before)30 ITestBean (org.springframework.tests.sample.beans.ITestBean)18 TestBean (org.springframework.tests.sample.beans.TestBean)11 BeansException (org.springframework.beans.BeansException)9 Resource (org.springframework.core.io.Resource)8 UrlResource (org.springframework.core.io.UrlResource)8 URL (java.net.URL)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)6 HashMap (java.util.HashMap)5 Person (org.springframework.tests.sample.beans.Person)5 ArrayList (java.util.ArrayList)4 BeanCreationException (org.springframework.beans.factory.BeanCreationException)4 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)4 ResourceEntityResolver (org.springframework.beans.factory.xml.ResourceEntityResolver)4