Search in sources :

Example 56 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project hutool by looly.

the class SpringUtil method registerBean.

/**
 * 动态向Spring注册Bean
 * <p>
 * 由{@link org.springframework.beans.factory.BeanFactory} 实现,通过工具开放API
 * <p>
 * 更新: shadow 2021-07-29 17:20:44 增加自动注入,修复注册bean无法反向注入的问题
 *
 * @param <T>      Bean类型
 * @param beanName 名称
 * @param bean     bean
 * @author shadow
 * @since 5.4.2
 */
public static <T> void registerBean(String beanName, T bean) {
    final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
    factory.autowireBean(bean);
    factory.registerSingleton(beanName, bean);
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 57 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project incubator-dubbo-spring-boot-project by apache.

the class DubboConfigBeanDefinitionConflictApplicationListener method resolveUniqueApplicationConfigBean.

/**
 * Resolve the unique {@link ApplicationConfig} Bean
 * @param registry {@link BeanDefinitionRegistry} instance
 * @param beanFactory {@link ConfigurableListableBeanFactory} instance
 * @see EnableDubboConfig
 */
private void resolveUniqueApplicationConfigBean(BeanDefinitionRegistry registry, ListableBeanFactory beanFactory) {
    String[] beansNames = beanNamesForTypeIncludingAncestors(beanFactory, ApplicationConfig.class);
    if (beansNames.length < 2) {
        // If the number of ApplicationConfig beans is less than two, return immediately.
        return;
    }
    Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
    // Remove ApplicationConfig Beans that are configured by "dubbo.application.*"
    Stream.of(beansNames).filter(beansName -> isConfiguredApplicationConfigBeanName(environment, beansName)).forEach(registry::removeBeanDefinition);
    beansNames = beanNamesForTypeIncludingAncestors(beanFactory, ApplicationConfig.class);
    if (beansNames.length > 1) {
        throw new IllegalStateException(String.format("There are more than one instances of %s, whose bean definitions : %s", ApplicationConfig.class.getSimpleName(), Stream.of(beansNames).map(registry::getBeanDefinition).collect(Collectors.toList())));
    }
}
Also used : Ordered(org.springframework.core.Ordered) Logger(org.slf4j.Logger) ENVIRONMENT_BEAN_NAME(org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) LoggerFactory(org.slf4j.LoggerFactory) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ApplicationListener(org.springframework.context.ApplicationListener) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Objects(java.util.Objects) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Stream(java.util.stream.Stream) Environment(org.springframework.core.env.Environment) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) EnableDubboConfig(org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig) BeanFactoryUtils.beanNamesForTypeIncludingAncestors(org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) Environment(org.springframework.core.env.Environment)

Example 58 with ConfigurableListableBeanFactory

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

the class AbstractApplicationContext method initLifecycleProcessor.

/**
	 * Initialize the LifecycleProcessor.
	 * Uses DefaultLifecycleProcessor if none defined in the context.
	 * @see org.springframework.context.support.DefaultLifecycleProcessor
	 */
protected void initLifecycleProcessor() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
        this.lifecycleProcessor = beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
        if (logger.isDebugEnabled()) {
            logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
        }
    } else {
        DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
        defaultProcessor.setBeanFactory(beanFactory);
        this.lifecycleProcessor = defaultProcessor;
        beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate LifecycleProcessor with name '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "': using default [" + this.lifecycleProcessor + "]");
        }
    }
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LifecycleProcessor(org.springframework.context.LifecycleProcessor)

Example 59 with ConfigurableListableBeanFactory

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

the class PersistenceAnnotationBeanPostProcessor method findDefaultEntityManagerFactory.

/**
	 * Find a single default EntityManagerFactory in the Spring application context.
	 * @return the default EntityManagerFactory
	 * @throws NoSuchBeanDefinitionException if there is no single EntityManagerFactory in the context
	 */
protected EntityManagerFactory findDefaultEntityManagerFactory(String requestingBeanName) throws NoSuchBeanDefinitionException {
    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        // Fancy variant with dependency registration
        ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory;
        NamedBeanHolder<EntityManagerFactory> emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class);
        clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName);
        return emfHolder.getBeanInstance();
    } else {
        // Plain variant: just find a default bean
        return this.beanFactory.getBean(EntityManagerFactory.class);
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 60 with ConfigurableListableBeanFactory

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

the class ServletTestExecutionListener method setUpRequestContextIfNecessary.

private void setUpRequestContextIfNecessary(TestContext testContext) {
    if (!isActivated(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
        return;
    }
    ApplicationContext context = testContext.getApplicationContext();
    if (context instanceof WebApplicationContext) {
        WebApplicationContext wac = (WebApplicationContext) context;
        ServletContext servletContext = wac.getServletContext();
        Assert.state(servletContext instanceof MockServletContext, () -> String.format("The WebApplicationContext for test context %s must be configured with a MockServletContext.", testContext));
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.", testContext));
        }
        MockServletContext mockServletContext = (MockServletContext) servletContext;
        MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
        request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
        MockHttpServletResponse response = new MockHttpServletResponse();
        ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);
        RequestContextHolder.setRequestAttributes(servletWebRequest);
        testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
        testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
        if (wac instanceof ConfigurableApplicationContext) {
            @SuppressWarnings("resource") ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
            ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
            bf.registerResolvableDependency(MockHttpServletResponse.class, response);
            bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
        }
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

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