Search in sources :

Example 51 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project openolat by klemens.

the class SpringInitDestroyVerficationTest method testInitMethodCalls.

@Test
public void testInitMethodCalls() {
    XmlWebApplicationContext context = (XmlWebApplicationContext) applicationContext;
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    Map<String, Initializable> beans = applicationContext.getBeansOfType(Initializable.class);
    for (Iterator<String> iterator = beans.keySet().iterator(); iterator.hasNext(); ) {
        String beanName = iterator.next();
        try {
            GenericBeanDefinition beanDef = (GenericBeanDefinition) beanFactory.getBeanDefinition(beanName);
            assertNotNull("Spring Bean (" + beanName + ") of type Initializable does not have the required init-method attribute or the method name is not init!", beanDef.getInitMethodName());
            if (beanDef.getDestroyMethodName() != null) {
                assertTrue("Spring Bean (" + beanName + ") of type Initializable does not have the required init-method attribute or the method name is not init!", beanDef.getInitMethodName().equals("init"));
            }
        } catch (NoSuchBeanDefinitionException e) {
            log.error("testInitMethodCalls: Error while trying to analyze bean with name: " + beanName + " :" + e);
        } catch (Exception e) {
            log.error("testInitMethodCalls: Error while trying to analyze bean with name: " + beanName + " :" + e);
        }
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Initializable(org.olat.core.configuration.Initializable) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 52 with ConfigurableListableBeanFactory

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

the class BeanConfigurerSupport method configureBean.

/**
 * Configure the bean instance.
 * <p>Subclasses can override this to provide custom configuration logic.
 * Typically called by an aspect, for all bean instances matched by a pointcut.
 * @param beanInstance the bean instance to configure (must <b>not</b> be {@code null})
 */
public void configureBean(Object beanInstance) {
    if (this.beanFactory == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("BeanFactory has not been set on " + ClassUtils.getShortName(getClass()) + ": " + "Make sure this configurer runs in a Spring container. Unable to configure bean of type [" + ClassUtils.getDescriptiveType(beanInstance) + "]. Proceeding without injection.");
        }
        return;
    }
    BeanWiringInfoResolver bwiResolver = this.beanWiringInfoResolver;
    Assert.state(bwiResolver != null, "No BeanWiringInfoResolver available");
    BeanWiringInfo bwi = bwiResolver.resolveWiringInfo(beanInstance);
    if (bwi == null) {
        // Skip the bean if no wiring info given.
        return;
    }
    ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    Assert.state(beanFactory != null, "No BeanFactory available");
    try {
        String beanName = bwi.getBeanName();
        if (bwi.indicatesAutowiring() || (bwi.isDefaultBeanName() && beanName != null && !beanFactory.containsBean(beanName))) {
            // Perform autowiring (also applying standard factory / post-processor callbacks).
            beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck());
            beanFactory.initializeBean(beanInstance, (beanName != null ? beanName : ""));
        } else {
            // Perform explicit wiring based on the specified bean definition.
            beanFactory.configureBean(beanInstance, (beanName != null ? beanName : ""));
        }
    } catch (BeanCreationException ex) {
        Throwable rootCause = ex.getMostSpecificCause();
        if (rootCause instanceof BeanCurrentlyInCreationException) {
            BeanCreationException bce = (BeanCreationException) rootCause;
            String bceBeanName = bce.getBeanName();
            if (bceBeanName != null && beanFactory.isCurrentlyInCreation(bceBeanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to create target bean '" + bce.getBeanName() + "' while configuring object of type [" + beanInstance.getClass().getName() + "] - probably due to a circular reference. This is a common startup situation " + "and usually not fatal. Proceeding without injection. Original exception: " + ex);
                }
                return;
            }
        }
        throw ex;
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 53 with ConfigurableListableBeanFactory

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

the class DefaultLifecycleProcessor method getBeanFactory.

private ConfigurableListableBeanFactory getBeanFactory() {
    ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    Assert.state(beanFactory != null, "No BeanFactory available");
    return beanFactory;
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 54 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project webapp by elimu-ai.

the class EnvironmentContextLoaderListener method customizeContext.

@Override
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
    logger.info("customizeContext");
    // Load default settings
    try {
        Resource resourceConfig = new ServletContextResourceLoader(servletContext).getResource("classpath:config.properties");
        PROPERTIES.load(resourceConfig.getInputStream());
        Resource resourceJdbc = new ServletContextResourceLoader(servletContext).getResource("classpath:jdbc.properties");
        PROPERTIES.load(resourceJdbc.getInputStream());
        logger.debug("properties (before overriding): " + PROPERTIES);
    } catch (IOException ex) {
        logger.error(ex);
    }
    if ((env == Environment.TEST) || (env == Environment.PROD)) {
        InputStream inputStream = null;
        try {
            // Override config.properties
            Resource resourceConfig = new ServletContextResourceLoader(servletContext).getResource("classpath:config_" + env + ".properties");
            PROPERTIES.load(resourceConfig.getInputStream());
            // Override jdbc.properties
            Resource resourceJdbc = new ServletContextResourceLoader(servletContext).getResource("classpath:jdbc_" + env + ".properties");
            PROPERTIES.load(resourceJdbc.getInputStream());
            String contentLanguage = (String) servletContext.getAttribute("content_language");
            logger.info("contentLanguage: " + contentLanguage);
            PROPERTIES.put("content.language", contentLanguage);
            String jdbcUrl = (String) servletContext.getAttribute("jdbc_url");
            PROPERTIES.put("jdbc.url", jdbcUrl);
            String jdbcUsername = (String) servletContext.getAttribute("jdbc_username");
            PROPERTIES.put("jdbc.username", jdbcUsername);
            String jdbcPasswordAttr = (String) servletContext.getAttribute("jdbc_password");
            PROPERTIES.put("jdbc.password", jdbcPasswordAttr);
            String googleApiSecret = (String) servletContext.getAttribute("google_api_secret");
            PROPERTIES.put("google.api.secret", googleApiSecret);
            String gitHubApiSecret = (String) servletContext.getAttribute("github_api_secret");
            PROPERTIES.put("github.api.secret", gitHubApiSecret);
            String covalentApiKey = (String) servletContext.getAttribute("covalent_api_key");
            PROPERTIES.put("covalent.api.key", covalentApiKey);
            if (env == Environment.PROD) {
                String discordWebhookUrl = (String) servletContext.getAttribute("discord_webhook_url");
                PROPERTIES.put("discord.webhook.url", discordWebhookUrl);
            }
            logger.debug("properties (after overriding): " + PROPERTIES);
        } catch (FileNotFoundException ex) {
            logger.error(ex);
        } catch (IOException ex) {
            logger.error(ex);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                logger.error(ex);
            }
        }
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setProperties(PROPERTIES);
        applicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
                PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
                propertyPlaceholderConfigurer.setProperties(PROPERTIES);
                propertyPlaceholderConfigurer.postProcessBeanFactory(configurableListableBeanFactory);
            }
        });
    }
    // Add all supported languages
    PROPERTIES.put("supported.languages", Language.values());
    // Add config properties to application scope
    servletContext.setAttribute("configProperties", PROPERTIES);
    servletContext.setAttribute("newLineCharRn", "\r\n");
    servletContext.setAttribute("newLineCharR", "\r");
    servletContext.setAttribute("newLineCharR", "\n");
    super.customizeContext(servletContext, applicationContext);
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 55 with ConfigurableListableBeanFactory

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

the class SpringUtil method unregisterBean.

/**
 * 注销bean
 * <p>
 * 将Spring中的bean注销,请谨慎使用
 *
 * @param beanName bean名称
 * @author shadow
 * @since 5.7.7
 */
public static void unregisterBean(String beanName) {
    final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
    if (factory instanceof DefaultSingletonBeanRegistry) {
        DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory;
        registry.destroySingleton(beanName);
    } else {
        throw new UtilException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!");
    }
}
Also used : DefaultSingletonBeanRegistry(org.springframework.beans.factory.support.DefaultSingletonBeanRegistry) UtilException(cn.hutool.core.exceptions.UtilException) 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