use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-framework by spring-projects.
the class EventListenerMethodProcessor method afterSingletonsInstantiated.
@Override
public void afterSingletonsInstantiated() {
ConfigurableListableBeanFactory beanFactory = this.beanFactory;
Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
if (!ScopedProxyUtils.isScopedTarget(beanName)) {
Class<?> type = null;
try {
type = AutoProxyUtils.determineTargetClass(beanFactory, beanName);
} catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
}
}
if (type != null) {
if (ScopedObject.class.isAssignableFrom(type)) {
try {
Class<?> targetClass = AutoProxyUtils.determineTargetClass(beanFactory, ScopedProxyUtils.getTargetBeanName(beanName));
if (targetClass != null) {
type = targetClass;
}
} catch (Throwable ex) {
// An invalid scoped proxy arrangement - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex);
}
}
}
try {
processBean(beanName, type);
} catch (Throwable ex) {
throw new BeanInitializationException("Failed to process @EventListener " + "annotation on bean with name '" + beanName + "'", ex);
}
}
}
}
}
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, @Nullable String beanName, @Nullable 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()) {
// Explicit handling of JDK proxy targets (for introduction advice scenarios)
if (Proxy.isProxyClass(beanClass)) {
// Must allow for introductions; can't just set interfaces to the proxy's interfaces only.
for (Class<?> ifc : beanClass.getInterfaces()) {
proxyFactory.addInterface(ifc);
}
}
} else {
// No proxyTargetClass flag enforced, let's apply our default checks...
if (shouldProxyTargetClass(beanClass, beanName)) {
proxyFactory.setProxyTargetClass(true);
} else {
evaluateProxyInterfaces(beanClass, proxyFactory);
}
}
Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
proxyFactory.addAdvisors(advisors);
proxyFactory.setTargetSource(targetSource);
customizeProxyFactory(proxyFactory);
proxyFactory.setFrozen(this.freezeProxy);
if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true);
}
// Use original ClassLoader if bean class not locally loaded in overriding class loader
ClassLoader classLoader = getProxyClassLoader();
if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
}
return proxyFactory.getProxy(classLoader);
}
use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-framework by spring-projects.
the class LazyAutowiredAnnotationBeanPostProcessorTests method doTestLazyResourceInjection.
private void doTestLazyResourceInjection(Class<? extends TestBeanHolder> annotatedBeanClass) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
RootBeanDefinition abd = new RootBeanDefinition(annotatedBeanClass);
abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
ac.registerBeanDefinition("annotatedBean", abd);
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
tbd.setLazyInit(true);
ac.registerBeanDefinition("testBean", tbd);
ac.refresh();
ConfigurableListableBeanFactory bf = ac.getBeanFactory();
TestBeanHolder bean = ac.getBean("annotatedBean", TestBeanHolder.class);
assertThat(bf.containsSingleton("testBean")).isFalse();
assertThat(bean.getTestBean()).isNotNull();
assertThat(bean.getTestBean().getName()).isNull();
assertThat(bf.containsSingleton("testBean")).isTrue();
TestBean tb = (TestBean) ac.getBean("testBean");
tb.setName("tb");
assertThat(bean.getTestBean().getName()).isSameAs("tb");
assertThat(ObjectUtils.containsElement(bf.getDependenciesForBean("annotatedBean"), "testBean")).isTrue();
assertThat(ObjectUtils.containsElement(bf.getDependentBeans("testBean"), "annotatedBean")).isTrue();
}
use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-framework by spring-projects.
the class ServletInvocableHandlerMethodTests method invokeAndHandle_responseStatusAndReasonCode.
@Test
public void invokeAndHandle_responseStatusAndReasonCode() throws Exception {
Locale locale = Locale.ENGLISH;
String beanName = "handler";
StaticApplicationContext context = new StaticApplicationContext();
context.registerBean(beanName, Handler.class);
context.addMessage("BadRequest.error", locale, "Bad request message");
context.refresh();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
LocaleContextHolder.setLocale(locale);
try {
Method method = ResolvableMethod.on(Handler.class).named("responseStatusWithReasonCode").resolveMethod();
HandlerMethod handlerMethod = new HandlerMethod(beanName, beanFactory, context, method);
handlerMethod = handlerMethod.createWithResolvedBean();
new ServletInvocableHandlerMethod(handlerMethod).invokeAndHandle(this.webRequest, this.mavContainer);
} finally {
LocaleContextHolder.resetLocaleContext();
}
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(this.response.getErrorMessage()).isEqualTo("Bad request message");
}
use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-framework by spring-projects.
the class ApplicationEventsTestExecutionListener method registerListenerAndResolvableDependencyIfNecessary.
private void registerListenerAndResolvableDependencyIfNecessary(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext, "The ApplicationContext for the test must be an AbstractApplicationContext");
AbstractApplicationContext aac = (AbstractApplicationContext) applicationContext;
// Synchronize to avoid race condition in parallel test execution
synchronized (applicationEventsMonitor) {
boolean notAlreadyRegistered = aac.getApplicationListeners().stream().map(Object::getClass).noneMatch(ApplicationEventsApplicationListener.class::equals);
if (notAlreadyRegistered) {
// Register a new ApplicationEventsApplicationListener.
aac.addApplicationListener(new ApplicationEventsApplicationListener());
// Register ApplicationEvents as a resolvable dependency for @Autowired support in test classes.
ConfigurableListableBeanFactory beanFactory = aac.getBeanFactory();
beanFactory.registerResolvableDependency(ApplicationEvents.class, new ApplicationEventsObjectFactory());
}
}
}
Aggregations