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