Search in sources :

Example 6 with StaticListableBeanFactory

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

the class DefaultMessageHandlerMethodFactoryTests method createInstance.

private DefaultMessageHandlerMethodFactory createInstance() {
    DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
    factory.setBeanFactory(new StaticListableBeanFactory());
    return factory;
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory)

Example 7 with StaticListableBeanFactory

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

the class BeanFactoryUtilsTests method testHierarchicalCountBeansWithNonHierarchicalFactory.

@Test
public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
    StaticListableBeanFactory lbf = new StaticListableBeanFactory();
    lbf.addBean("t1", new TestBean());
    lbf.addBean("t2", new TestBean());
    assertTrue(BeanFactoryUtils.countBeansIncludingAncestors(lbf) == 2);
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 8 with StaticListableBeanFactory

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

the class BeanFactoryUtilsTests method testNoBeansOfType.

@Test
public void testNoBeansOfType() {
    StaticListableBeanFactory lbf = new StaticListableBeanFactory();
    lbf.addBean("foo", new Object());
    Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
    assertTrue(beans.isEmpty());
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) Test(org.junit.Test)

Example 9 with StaticListableBeanFactory

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

the class DataSourceJtaTransactionTests method doTestJtaTransactionWithIsolationLevelDataSourceRouter.

private void doTestJtaTransactionWithIsolationLevelDataSourceRouter(boolean dataSourceLookup) throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    final DataSource dataSource1 = mock(DataSource.class);
    final Connection connection1 = mock(Connection.class);
    given(dataSource1.getConnection()).willReturn(connection1);
    final DataSource dataSource2 = mock(DataSource.class);
    final Connection connection2 = mock(Connection.class);
    given(dataSource2.getConnection()).willReturn(connection2);
    final IsolationLevelDataSourceRouter dsToUse = new IsolationLevelDataSourceRouter();
    Map<Object, Object> targetDataSources = new HashMap<>();
    if (dataSourceLookup) {
        targetDataSources.put("ISOLATION_REPEATABLE_READ", "ds2");
        dsToUse.setDefaultTargetDataSource("ds1");
        StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
        beanFactory.addBean("ds1", dataSource1);
        beanFactory.addBean("ds2", dataSource2);
        dsToUse.setDataSourceLookup(new BeanFactoryDataSourceLookup(beanFactory));
    } else {
        targetDataSources.put("ISOLATION_REPEATABLE_READ", dataSource2);
        dsToUse.setDefaultTargetDataSource(dataSource1);
    }
    dsToUse.setTargetDataSources(targetDataSources);
    dsToUse.afterPropertiesSet();
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    ptm.setAllowCustomIsolationLevels(true);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(connection1, c);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
            assertSame(connection2, c);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    verify(userTransaction, times(2)).begin();
    verify(userTransaction, times(2)).commit();
    verify(connection1).close();
    verify(connection2).close();
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) HashMap(java.util.HashMap) Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) IsolationLevelDataSourceRouter(org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter) TransactionStatus(org.springframework.transaction.TransactionStatus) BeanFactoryDataSourceLookup(org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup) DataSource(javax.sql.DataSource) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionObject(org.springframework.transaction.jta.JtaTransactionObject) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 10 with StaticListableBeanFactory

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

the class JCacheInterceptorTests method createOperationSource.

protected JCacheOperationSource createOperationSource(CacheManager cacheManager, CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) {
    DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
    source.setCacheManager(cacheManager);
    source.setCacheResolver(cacheResolver);
    source.setExceptionCacheResolver(exceptionCacheResolver);
    source.setKeyGenerator(keyGenerator);
    source.setBeanFactory(new StaticListableBeanFactory());
    source.afterPropertiesSet();
    source.afterSingletonsInstantiated();
    return source;
}
Also used : StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory)

Aggregations

StaticListableBeanFactory (org.springframework.beans.factory.support.StaticListableBeanFactory)11 Test (org.junit.Test)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 FooServiceImpl (example.scannable.FooServiceImpl)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 DataSource (javax.sql.DataSource)1 Before (org.junit.Before)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 MessageSource (org.springframework.context.MessageSource)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 BeanFactoryDataSourceLookup (org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup)1 IsolationLevelDataSourceRouter (org.springframework.jdbc.datasource.lookup.IsolationLevelDataSourceRouter)1 DummyFactory (org.springframework.tests.sample.beans.factory.DummyFactory)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)1 JtaTransactionObject (org.springframework.transaction.jta.JtaTransactionObject)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1