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;
}
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());
}
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());
}
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);
}
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);
}
Aggregations