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