Search in sources :

Example 51 with BeanFactory

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

the class ResourceBundleViewResolver method initFactory.

/**
 * Initialize the View {@link BeanFactory} from the {@code ResourceBundle},
 * for the given {@link Locale locale}.
 * <p>Synchronized because of access by parallel threads.
 * @param locale the target {@code Locale}
 * @return the View factory for the given Locale
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory(Locale locale) throws BeansException {
    // Have we already encountered that Locale before?
    if (isCache()) {
        BeanFactory cachedFactory = this.localeCache.get(locale);
        if (cachedFactory != null) {
            return cachedFactory;
        }
    }
    // Build list of ResourceBundle references for Locale.
    List<ResourceBundle> bundles = new ArrayList<>(this.basenames.length);
    for (String basename : this.basenames) {
        bundles.add(getBundle(basename, locale));
    }
    // even if Locale was different, same bundles might have been found.
    if (isCache()) {
        BeanFactory cachedFactory = this.bundleCache.get(bundles);
        if (cachedFactory != null) {
            this.localeCache.put(locale, cachedFactory);
            return cachedFactory;
        }
    }
    // Create child ApplicationContext for views.
    GenericWebApplicationContext factory = new GenericWebApplicationContext();
    factory.setParent(getApplicationContext());
    factory.setServletContext(getServletContext());
    // Load bean definitions from resource bundle.
    org.springframework.beans.factory.support.PropertiesBeanDefinitionReader reader = new org.springframework.beans.factory.support.PropertiesBeanDefinitionReader(factory);
    reader.setDefaultParentBean(this.defaultParentView);
    for (ResourceBundle bundle : bundles) {
        reader.registerBeanDefinitions(bundle);
    }
    factory.refresh();
    // Cache factory for both Locale and ResourceBundle list.
    if (isCache()) {
        this.localeCache.put(locale, factory);
        this.bundleCache.put(bundles, factory);
    }
    return factory;
}
Also used : ArrayList(java.util.ArrayList) BeanFactory(org.springframework.beans.factory.BeanFactory) ResourceBundle(java.util.ResourceBundle) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 52 with BeanFactory

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

the class BeanFactoryConnectionFactoryLookupUnitTests method shouldLookupWhereBeanFactoryYieldsNonConnectionFactoryType.

@Test
public void shouldLookupWhereBeanFactoryYieldsNonConnectionFactoryType() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class)).thenThrow(new BeanNotOfRequiredTypeException(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class, String.class));
    BeanFactoryConnectionFactoryLookup lookup = new BeanFactoryConnectionFactoryLookup(beanFactory);
    assertThatExceptionOfType(ConnectionFactoryLookupFailureException.class).isThrownBy(() -> lookup.getConnectionFactory(CONNECTION_FACTORY_BEAN_NAME));
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) Test(org.junit.jupiter.api.Test)

Example 53 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerDefaultSeveralTimes.

@Test
public void determineTransactionManagerDefaultSeveralTimes() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
    PlatformTransactionManager txManager = mock(PlatformTransactionManager.class);
    given(beanFactory.getBean(TransactionManager.class)).willReturn(txManager);
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    TransactionManager actual = ti.determineTransactionManager(attribute);
    assertThat(actual).isSameAs(txManager);
    // Call again, should be cached
    TransactionManager actual2 = ti.determineTransactionManager(attribute);
    assertThat(actual2).isSameAs(txManager);
    verify(beanFactory, times(1)).getBean(TransactionManager.class);
}
Also used : TransactionManager(org.springframework.transaction.TransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 54 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierUnknown.

@Test
public void determineTransactionManagerWithQualifierUnknown() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory);
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> ti.determineTransactionManager(attribute)).withMessageContaining("'fooTransactionManager'");
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test)

Example 55 with BeanFactory

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

the class TransactionInterceptorTests method determineTransactionManagerWithQualifierAndDefault.

@Test
public void determineTransactionManagerWithQualifierAndDefault() {
    BeanFactory beanFactory = mock(BeanFactory.class);
    PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
    TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, beanFactory);
    PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager");
    DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
    attribute.setQualifier("fooTransactionManager");
    assertThat(ti.determineTransactionManager(attribute)).isSameAs(fooTransactionManager);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4