Search in sources :

Example 81 with ConfigurableListableBeanFactory

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);
                }
            }
        }
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 82 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, @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);
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) SmartClassLoader(org.springframework.core.SmartClassLoader) Advisor(org.springframework.aop.Advisor) SmartClassLoader(org.springframework.core.SmartClassLoader) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 83 with ConfigurableListableBeanFactory

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();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 84 with ConfigurableListableBeanFactory

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");
}
Also used : Locale(java.util.Locale) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 85 with ConfigurableListableBeanFactory

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());
        }
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)104 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)10 ApplicationContext (org.springframework.context.ApplicationContext)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Map (java.util.Map)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)6 Collectors (java.util.stream.Collectors)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 HashMap (java.util.HashMap)4 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)4