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