Search in sources :

Example 6 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project PublicCMS-preview by sanluan.

the class ApplicationConfig method hibernateSessionFactory.

/**
 * Hibernate 会话工厂类
 *
 * @param dataSource
 * @return hibernate session factory
 * @throws PropertyVetoException
 * @throws IOException
 */
@Bean
public FactoryBean<SessionFactory> hibernateSessionFactory(DataSource dataSource) throws PropertyVetoException, IOException {
    LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setPackagesToScan("com.publiccms.entities");
    Properties properties = PropertiesLoaderUtils.loadAllProperties(env.getProperty("cms.hibernate.configFilePath"));
    properties.setProperty("hibernate.search.default.indexBase", getDirPath("/indexes/"));
    MultiTokenizerFactory.setName(env.getProperty("cms.tokenizerFactory"));
    bean.setHibernateProperties(properties);
    return bean;
}
Also used : LocalSessionFactoryBean(org.springframework.orm.hibernate5.LocalSessionFactoryBean) Properties(java.util.Properties) FactoryBean(org.springframework.beans.factory.FactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) LocalSessionFactoryBean(org.springframework.orm.hibernate5.LocalSessionFactoryBean) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with FactoryBean

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

the class Spr8954Tests method repro.

@Test
public void repro() {
    AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
    bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
    bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
    bf.refresh();
    assertThat(bf.getBean("foo")).isInstanceOf(Foo.class);
    assertThat(bf.getBean("&foo")).isInstanceOf(FooFactoryBean.class);
    assertThat(bf.isTypeMatch("&foo", FactoryBean.class)).isTrue();
    @SuppressWarnings("rawtypes") Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
    assertThat(1).isEqualTo(fbBeans.size());
    assertThat("&foo").isEqualTo(fbBeans.keySet().iterator().next());
    Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
    assertThat(1).isEqualTo(aiBeans.size());
    assertThat("&foo").isEqualTo(aiBeans.keySet().iterator().next());
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 8 with FactoryBean

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

the class Spr8954Tests method findsBeansByTypeIfNotInstantiated.

@Test
public void findsBeansByTypeIfNotInstantiated() {
    AnnotationConfigApplicationContext bf = new AnnotationConfigApplicationContext();
    bf.registerBeanDefinition("fooConfig", new RootBeanDefinition(FooConfig.class));
    bf.getBeanFactory().addBeanPostProcessor(new PredictingBPP());
    bf.refresh();
    assertThat(bf.isTypeMatch("&foo", FactoryBean.class)).isTrue();
    @SuppressWarnings("rawtypes") Map<String, FactoryBean> fbBeans = bf.getBeansOfType(FactoryBean.class);
    assertThat(1).isEqualTo(fbBeans.size());
    assertThat("&foo").isEqualTo(fbBeans.keySet().iterator().next());
    Map<String, AnInterface> aiBeans = bf.getBeansOfType(AnInterface.class);
    assertThat(1).isEqualTo(aiBeans.size());
    assertThat("&foo").isEqualTo(aiBeans.keySet().iterator().next());
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 9 with FactoryBean

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

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFactoryBeanAndProxyFactoryBeanOnly.

@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyFactoryBeanOnly() {
    StaticApplicationContext sac = new StaticApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("proxyObject", "false");
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
    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)).isFalse();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
    tb.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
}
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 10 with FactoryBean

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

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly.

@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
    StaticApplicationContext sac = new StaticApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("proxyFactoryBean", "false");
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
    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.isAopProxy(factory)).isFalse();
    TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
    assertThat(AopUtils.isCglibProxy(tb)).isTrue();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    tb.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
    TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
    assertThat(tb2).isSameAs(tb);
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
    tb2.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
}
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)

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