Search in sources :

Example 66 with BeanFactory

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

the class AbstractBeanFactory method getMergedBeanDefinition.

/**
	 * Return a RootBeanDefinition for the given bean, by merging with the
	 * parent if the given bean's definition is a child bean definition.
	 * @param beanName the name of the bean definition
	 * @param bd the original bean definition (Root/ChildBeanDefinition)
	 * @param containingBd the containing bean definition in case of inner bean,
	 * or {@code null} in case of a top-level bean
	 * @return a (potentially merged) RootBeanDefinition for the given bean
	 * @throws BeanDefinitionStoreException in case of an invalid bean definition
	 */
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException {
    synchronized (this.mergedBeanDefinitions) {
        RootBeanDefinition mbd = null;
        // Check with full lock now in order to enforce the same merged instance.
        if (containingBd == null) {
            mbd = this.mergedBeanDefinitions.get(beanName);
        }
        if (mbd == null) {
            if (bd.getParentName() == null) {
                // Use copy of given root bean definition.
                if (bd instanceof RootBeanDefinition) {
                    mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
                } else {
                    mbd = new RootBeanDefinition(bd);
                }
            } else {
                // Child bean definition: needs to be merged with parent.
                BeanDefinition pbd;
                try {
                    String parentBeanName = transformedBeanName(bd.getParentName());
                    if (!beanName.equals(parentBeanName)) {
                        pbd = getMergedBeanDefinition(parentBeanName);
                    } else {
                        BeanFactory parent = getParentBeanFactory();
                        if (parent instanceof ConfigurableBeanFactory) {
                            pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
                        } else {
                            throw new NoSuchBeanDefinitionException(parentBeanName, "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent");
                        }
                    }
                } catch (NoSuchBeanDefinitionException ex) {
                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
                }
                // Deep copy with overridden values.
                mbd = new RootBeanDefinition(pbd);
                mbd.overrideFrom(bd);
            }
            // Set default singleton scope, if not configured before.
            if (!StringUtils.hasLength(mbd.getScope())) {
                mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
            }
            // definition will not have inherited the merged outer bean's singleton status.
            if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                mbd.setScope(containingBd.getScope());
            }
            // instance of the bean, or at least have already created an instance before.
            if (containingBd == null && isCacheBeanMetadata()) {
                this.mergedBeanDefinitions.put(beanName, mbd);
            }
        }
        return mbd;
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 67 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project ignite by apache.

the class SpringTransactionManagerContextInjectionTest method testBeanInjectionUsingConfigPath.

/**
 * @throws Exception If failed.
 */
public void testBeanInjectionUsingConfigPath() throws Exception {
    BeanFactory factory = new AnnotationConfigApplicationContext(TestPathConfiguration.class);
    Ignite grid = IgnitionEx.grid("springInjectionTest");
    IgniteConfiguration cfg = grid.configuration();
    LifecycleBean[] beans = cfg.getLifecycleBeans();
    assertEquals(2, beans.length);
    TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean) beans[0];
    TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean) beans[1];
    bean1.checkState();
    bean2.checkState();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BeanFactory(org.springframework.beans.factory.BeanFactory) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean) Ignite(org.apache.ignite.Ignite) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean)

Example 68 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project ignite by apache.

the class GridSpringCacheManagerSelfTest method beforeTest.

/**
 * {@inheritDoc}
 */
@Override
protected void beforeTest() throws Exception {
    BeanFactory factory = new ClassPathXmlApplicationContext("org/apache/ignite/cache/spring/spring-caching.xml");
    svc = (GridSpringCacheTestService) factory.getBean("testService");
    dynamicSvc = (GridSpringDynamicCacheTestService) factory.getBean("dynamicTestService");
    svc.reset();
    dynamicSvc.reset();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory)

Example 69 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project geronimo-xbean by apache.

the class SpringInitialContextFactory method loadContext.

protected BeanFactory loadContext(Resource resource, String key) {
    synchronized (cache) {
        BeanFactory answer = (BeanFactory) cache.get(key);
        if (answer == null) {
            answer = createContext(resource);
            cache.put(key, answer);
        }
        return answer;
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) XBeanXmlBeanFactory(org.apache.xbean.spring.context.impl.XBeanXmlBeanFactory)

Example 70 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project geronimo-xbean by apache.

the class ComponentTest method test.

protected void test(String file) {
    BeanFactory f = new ClassPathXmlApplicationContext(file);
    ContainerBean container = (ContainerBean) f.getBean("container");
    assertNotNull(container);
    assertNotNull(container.getBeans());
    assertEquals(1, container.getBeans().length);
}
Also used : ContainerBean(org.apache.xbean.spring.example.ContainerBean) ClassPathXmlApplicationContext(org.apache.xbean.spring.context.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory)

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