Search in sources :

Example 26 with ConfigurableListableBeanFactory

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

the class XmlWebApplicationContextTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    InitAndIB.constructed = false;
    root = new XmlWebApplicationContext();
    root.getEnvironment().addActiveProfile("rootProfile1");
    MockServletContext sc = new MockServletContext("");
    root.setServletContext(sc);
    root.setConfigLocations(new String[] { "/org/springframework/web/context/WEB-INF/applicationContext.xml" });
    root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.addBeanPostProcessor(new BeanPostProcessor() {

                @Override
                public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
                    if (bean instanceof TestBean) {
                        ((TestBean) bean).getFriends().add("myFriend");
                    }
                    return bean;
                }

                @Override
                public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
                    return bean;
                }
            });
        }
    });
    root.refresh();
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.getEnvironment().addActiveProfile("wacProfile1");
    wac.setParent(root);
    wac.setServletContext(sc);
    wac.setNamespace("test-servlet");
    wac.setConfigLocations(new String[] { "/org/springframework/web/context/WEB-INF/test-servlet.xml" });
    wac.refresh();
    return wac;
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 27 with ConfigurableListableBeanFactory

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

the class ResetMocksTestExecutionListener method resetMocks.

private void resetMocks(ConfigurableApplicationContext applicationContext, MockReset reset) {
    ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
    String[] names = beanFactory.getBeanDefinitionNames();
    Set<String> instantiatedSingletons = new HashSet<>(Arrays.asList(beanFactory.getSingletonNames()));
    for (String name : names) {
        BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
            Object bean = beanFactory.getSingleton(name);
            if (reset.equals(MockReset.get(bean))) {
                Mockito.reset(bean);
            }
        }
    }
    try {
        MockitoBeans mockedBeans = beanFactory.getBean(MockitoBeans.class);
        for (Object mockedBean : mockedBeans) {
            if (reset.equals(MockReset.get(mockedBean))) {
                Mockito.reset(mockedBean);
            }
        }
    } catch (NoSuchBeanDefinitionException ex) {
    // Continue
    }
    if (applicationContext.getParent() != null) {
        resetMocks(applicationContext.getParent(), reset);
    }
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) HashSet(java.util.HashSet)

Example 28 with ConfigurableListableBeanFactory

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

the class AbstractAutoProxyCreator method createProxy.

/**
	 * Create an AOP proxy for the given bean.
	 * @param beanClass the class of the bean
	 * @param beanName the name of the bean
	 * @param specificInterceptors the set of interceptors that is
	 * specific to this bean (may be empty, but not null)
	 * @param targetSource the TargetSource for the proxy,
	 * already pre-configured to access the bean
	 * @return the AOP proxy for the bean
	 * @see #buildAdvisors
	 */
protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {
    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.copyFrom(this);
    if (!proxyFactory.isProxyTargetClass()) {
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        } else {
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }
    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    for (Advisor advisor : advisors) {
        proxyFactory.addAdvisor(advisor);
    }
    proxyFactory.setTargetSource(targetSource);
    customizeProxyFactory(proxyFactory);
    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
    return proxyFactory.getProxy(getProxyClassLoader());
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advisor(org.springframework.aop.Advisor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 29 with ConfigurableListableBeanFactory

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

the class LiveBeansView method generateJson.

/**
	 * Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
	 * <p>This implementation doesn't use any JSON parsing libraries in order to avoid
	 * third-party library dependencies. It produces an array of context description
	 * objects, each containing a context and parent attribute as well as a beans
	 * attribute with nested bean description objects. Each bean object contains a
	 * bean, scope, type and resource attribute, as well as a dependencies attribute
	 * with a nested array of bean names that the present bean depends on.
	 * @param contexts the set of ApplicationContexts
	 * @return the JSON document
	 */
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
    StringBuilder result = new StringBuilder("[\n");
    for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext(); ) {
        ConfigurableApplicationContext context = it.next();
        result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
        if (context.getParent() != null) {
            result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
        } else {
            result.append("\"parent\": null,\n");
        }
        result.append("\"beans\": [\n");
        ConfigurableListableBeanFactory bf = context.getBeanFactory();
        String[] beanNames = bf.getBeanDefinitionNames();
        boolean elementAppended = false;
        for (String beanName : beanNames) {
            BeanDefinition bd = bf.getBeanDefinition(beanName);
            if (isBeanEligible(beanName, bd, bf)) {
                if (elementAppended) {
                    result.append(",\n");
                }
                result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
                result.append("\"aliases\": ");
                appendArray(result, bf.getAliases(beanName));
                result.append(",\n");
                String scope = bd.getScope();
                if (!StringUtils.hasText(scope)) {
                    scope = BeanDefinition.SCOPE_SINGLETON;
                }
                result.append("\"scope\": \"").append(scope).append("\",\n");
                Class<?> beanType = bf.getType(beanName);
                if (beanType != null) {
                    result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
                } else {
                    result.append("\"type\": null,\n");
                }
                result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
                result.append("\"dependencies\": ");
                appendArray(result, bf.getDependenciesForBean(beanName));
                result.append("\n}");
                elementAppended = true;
            }
        }
        result.append("]\n");
        result.append("}");
        if (it.hasNext()) {
            result.append(",\n");
        }
    }
    result.append("]");
    return result.toString();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 30 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-cloud-connectors by spring-cloud.

the class CloudScanHelper method initializeCloud.

private void initializeCloud(BeanDefinitionRegistry registry) {
    if (cloud != null) {
        return;
    }
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry;
    if (beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) {
        beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory());
    }
    CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next();
    cloud = cloudFactory.getCloud();
}
Also used : CloudFactory(org.springframework.cloud.CloudFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)35 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)6 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 ApplicationContext (org.springframework.context.ApplicationContext)4 Test (org.junit.Test)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 CloudFactory (org.springframework.cloud.CloudFactory)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 BeansException (org.springframework.beans.BeansException)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)2 GrailsPlugin (grails.plugins.GrailsPlugin)1 ConfigObject (groovy.util.ConfigObject)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ExternalContext (javax.faces.context.ExternalContext)1 FacesContext (javax.faces.context.FacesContext)1