Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBeanProperties

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBeanProperties

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support DefaultListableBeanFactory autowireBeanProperties.

Prototype

@Override
    public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
            throws BeansException 

Source Link

Usage

From source file:com.extjs.djn.spring.loader.SpringLoaderHelper.java

/**
 * Allow to autowired a non spring instanciated object
 * //from w  w  w. ja  v  a2 s.  c  o m
 * @param objToLoad
 * @param prefixBeanName
 */
public static void autowireBean(Object objToLoad, String prefixBeanName) {
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ContextLoader
            .getCurrentWebApplicationContext().getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(objToLoad, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
    beanFactory.initializeBean(objToLoad, prefixBeanName);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(existingBean.getSpouse(), spouse);
    assertSame(spouse, BeanFactoryUtils.beanOfType(lbf, TestBean.class));
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByNameWithDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    try {/* w w w  . j a v a 2  s.  co  m*/
        lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
    }
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByNameWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertNull(existingBean.getSpouse());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByType() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("test", bd);
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    TestBean test = (TestBean) lbf.getBean("test");
    assertEquals(existingBean.getSpouse(), test);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByTypeWithDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DependenciesBean existingBean = new DependenciesBean();
    try {/*from  ww w .j  a v a  2  s .c  om*/
        lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
    }
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testAutowireExistingBeanByTypeWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    DependenciesBean existingBean = new DependenciesBean();
    lbf.autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    assertNull(existingBean.getSpouse());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testInvalidAutowireMode() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    try {//from  w w  w . ja va2 s .c  o  m
        lbf.autowireBeanProperties(new TestBean(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}