Search in sources :

Example 16 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");
    thrown.expect(NoSuchBeanDefinitionException.class);
    thrown.expectMessage("'fooTransactionManager'");
    ti.determineTransactionManager(attribute);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 17 with BeanFactory

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

the class OnBeanCondition method getMatchingBeans.

private MatchResult getMatchingBeans(ConditionContext context, BeanSearchSpec beans) {
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beans.getStrategy() == SearchStrategy.ANCESTORS) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent, "Unable to use SearchStrategy.PARENTS");
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }
    MatchResult matchResult = new MatchResult();
    boolean considerHierarchy = beans.getStrategy() != SearchStrategy.CURRENT;
    List<String> beansIgnoredByType = getNamesOfBeansIgnoredByType(beans.getIgnoredTypes(), beanFactory, context, considerHierarchy);
    for (String type : beans.getTypes()) {
        Collection<String> typeMatches = getBeanNamesForType(beanFactory, type, context.getClassLoader(), considerHierarchy);
        typeMatches.removeAll(beansIgnoredByType);
        if (typeMatches.isEmpty()) {
            matchResult.recordUnmatchedType(type);
        } else {
            matchResult.recordMatchedType(type, typeMatches);
        }
    }
    for (String annotation : beans.getAnnotations()) {
        List<String> annotationMatches = Arrays.asList(getBeanNamesForAnnotation(beanFactory, annotation, context.getClassLoader(), considerHierarchy));
        annotationMatches.removeAll(beansIgnoredByType);
        if (annotationMatches.isEmpty()) {
            matchResult.recordUnmatchedAnnotation(annotation);
        } else {
            matchResult.recordMatchedAnnotation(annotation, annotationMatches);
        }
    }
    for (String beanName : beans.getNames()) {
        if (!beansIgnoredByType.contains(beanName) && containsBean(beanFactory, beanName, considerHierarchy)) {
            matchResult.recordMatchedName(beanName);
        } else {
            matchResult.recordUnmatchedName(beanName);
        }
    }
    return matchResult;
}
Also used : HierarchicalBeanFactory(org.springframework.beans.factory.HierarchicalBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 18 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 LinkedList<>();
    for (String basename : this.basenames) {
        ResourceBundle bundle = getBundle(basename, locale);
        bundles.add(bundle);
    }
    // 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.
    PropertiesBeanDefinitionReader reader = new 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 : PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) BeanFactory(org.springframework.beans.factory.BeanFactory) ResourceBundle(java.util.ResourceBundle) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) LinkedList(java.util.LinkedList)

Example 19 with BeanFactory

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

the class Rollback method testNoProxy.

/**
	 * If no pointcuts match (no attrs) there should be proxying.
	 */
@Test
public void testNoProxy() throws Exception {
    BeanFactory bf = getBeanFactory();
    Object o = bf.getBean("noSetters");
    assertFalse(AopUtils.isAopProxy(o));
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 20 with BeanFactory

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

the class Rollback method testTxIsProxied.

@Test
public void testTxIsProxied() throws Exception {
    BeanFactory bf = getBeanFactory();
    ITestBean test = (ITestBean) bf.getBean("test");
    assertTrue(AopUtils.isAopProxy(test));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) BeanFactory(org.springframework.beans.factory.BeanFactory) 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