Search in sources :

Example 36 with BeanFactory

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

the class SelectivePrototypeTargetSourceCreator method testLazyInitTargetSource.

@Test
public void testLazyInitTargetSource() throws Exception {
    CountingTestBean.count = 0;
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
    assertTrue(AopUtils.isAopProxy(test));
    Advised advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
    assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
    assertEquals("Rod", test.getName());
    assertEquals("Kerry", test.getSpouse().getName());
    assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
    CountingTestBean.count = 0;
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) BeanFactory(org.springframework.beans.factory.BeanFactory) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource) Test(org.junit.Test)

Example 37 with BeanFactory

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

the class SelectivePrototypeTargetSourceCreator method testCustomTargetSourceNoMatch.

/**
	 * We have custom TargetSourceCreators but there's no match, and
	 * hence no proxying, for this bean
	 */
@Test
public void testCustomTargetSourceNoMatch() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
    ITestBean test = (ITestBean) bf.getBean("test");
    assertFalse(AopUtils.isAopProxy(test));
    assertEquals("Rod", test.getName());
    assertEquals("Kerry", test.getSpouse().getName());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 38 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierSeveralTimes.

@Test
public void determineTransactionManagerWithQualifierSeveralTimes() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
    PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual);
    // Call again, should be cached
    PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual2);
    verify(beanFactory, times(1)).containsBean("fooTransactionManager");
    verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 39 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerWithNoTransactionAttribute.

@Test
public void determineTransactionManagerWithNoTransactionAttribute() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
    assertNull(ti.determineTransactionManager(null));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 40 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerWithBeanNameSeveralTimes.

@Test
public void determineTransactionManagerWithBeanNameSeveralTimes() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName("fooTransactionManager", beanFactory);
    PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    PlatformTransactionManager actual = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual);
    // Call again, should be cached
    PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute);
    assertSame(txManager, actual2);
    verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)62 Test (org.junit.Test)28 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)17 ITestBean (org.springframework.tests.sample.beans.ITestBean)11 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)10 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)10 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 FactoryBean (org.springframework.beans.factory.FactoryBean)4 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)4 CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)4 DataSource (javax.sql.DataSource)3 Ignite (org.apache.ignite.Ignite)3 TestInjectionLifecycleBean (org.apache.ignite.TestInjectionLifecycleBean)3 BeansException (org.springframework.beans.BeansException)3 BeanNotOfRequiredTypeException (org.springframework.beans.factory.BeanNotOfRequiredTypeException)3 HierarchicalBeanFactory (org.springframework.beans.factory.HierarchicalBeanFactory)3