Search in sources :

Example 11 with FactoryBean

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

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFactoryBeanAndPrototype.

@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("singleton", "false");
    sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);
    sac.refresh();
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    tapc.testInterceptor.nrOfInvocations = 0;
    FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
    assertThat(AopUtils.isCglibProxy(prototypeFactory)).isTrue();
    TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
    assertThat(AopUtils.isCglibProxy(tb)).isTrue();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
    tb.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(3);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 12 with FactoryBean

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

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFactoryBean.

@Test
public void testAutoProxyCreatorWithFactoryBean() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
    sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
    sac.refresh();
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    tapc.testInterceptor.nrOfInvocations = 0;
    FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
    assertThat(AopUtils.isCglibProxy(factory)).isTrue();
    TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
    assertThat(AopUtils.isCglibProxy(tb)).isTrue();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
    tb.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(3);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 13 with FactoryBean

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

the class StaticListableBeanFactory method getBean.

// ---------------------------------------------------------------------
// Implementation of BeanFactory interface
// ---------------------------------------------------------------------
@Override
public Object getBean(String name) throws BeansException {
    String beanName = BeanFactoryUtils.transformedBeanName(name);
    Object bean = this.beans.get(beanName);
    if (bean == null) {
        throw new NoSuchBeanDefinitionException(beanName, "Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
    }
    // bean factory if the bean isn't a factory
    if (BeanFactoryUtils.isFactoryDereference(name) && !(bean instanceof FactoryBean)) {
        throw new BeanIsNotAFactoryException(beanName, bean.getClass());
    }
    if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
        try {
            Object exposedObject = ((FactoryBean<?>) bean).getObject();
            if (exposedObject == null) {
                throw new BeanCreationException(beanName, "FactoryBean exposed null object");
            }
            return exposedObject;
        } catch (Exception ex) {
            throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
        }
    } else {
        return bean;
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException)

Example 14 with FactoryBean

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

the class GroovyScriptFactoryTests method testRefreshableFactoryBean.

@Test
public void testRefreshableFactoryBean() {
    ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
    Object factory = context.getBean("&refreshableFactory");
    boolean condition1 = factory instanceof FactoryBean;
    assertThat(condition1).isTrue();
    Object result = context.getBean("refreshableFactory");
    boolean condition = result instanceof String;
    assertThat(condition).isTrue();
    assertThat(result).isEqualTo("test");
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) GroovyObject(groovy.lang.GroovyObject) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 15 with FactoryBean

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

the class GroovyScriptFactoryTests method testFactoryBean.

@Test
public void testFactoryBean() {
    ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
    Object factory = context.getBean("&factory");
    boolean condition1 = factory instanceof FactoryBean;
    assertThat(condition1).isTrue();
    Object result = context.getBean("factory");
    boolean condition = result instanceof String;
    assertThat(condition).isTrue();
    assertThat(result).isEqualTo("test");
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) GroovyObject(groovy.lang.GroovyObject) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Aggregations

FactoryBean (org.springframework.beans.factory.FactoryBean)27 Test (org.junit.jupiter.api.Test)10 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)5 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)4 Bean (org.springframework.context.annotation.Bean)4 BeansException (org.springframework.beans.BeansException)3 GroovyObject (groovy.lang.GroovyObject)2 PrivilegedAction (java.security.PrivilegedAction)2 ArrayList (java.util.ArrayList)2 HttpClientProperties (org.apereo.cas.configuration.model.core.authentication.HttpClientProperties)2 Test (org.junit.Test)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)2