Use SmartInitializingSingleton when possible

Switch implementations of ApplicationListener<ContextRefreshEvent> for
SmartInitializingSingleton when possible.

Fixes gh-1939
pull/1958/head
Phillip Webb 10 years ago
parent 7a783f5a18
commit b583262211

@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
@ -64,7 +65,6 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
@ -90,7 +90,7 @@ import org.springframework.web.servlet.DispatcherServlet;
ManagementServerPropertiesAutoConfiguration.class }) ManagementServerPropertiesAutoConfiguration.class })
@EnableConfigurationProperties(HealthMvcEndpointProperties.class) @EnableConfigurationProperties(HealthMvcEndpointProperties.class)
public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
ApplicationListener<ContextRefreshedEvent> { SmartInitializingSingleton {
private static Log logger = LogFactory.getLog(EndpointWebMvcAutoConfiguration.class); private static Log logger = LogFactory.getLog(EndpointWebMvcAutoConfiguration.class);
@ -122,19 +122,17 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
} }
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void afterSingletonsInstantiated() {
if (event.getApplicationContext() == this.applicationContext) { ManagementServerPort managementPort = ManagementServerPort
ManagementServerPort managementPort = ManagementServerPort .get(this.applicationContext);
.get(this.applicationContext); if (managementPort == ManagementServerPort.DIFFERENT
if (managementPort == ManagementServerPort.DIFFERENT && this.applicationContext instanceof WebApplicationContext) {
&& this.applicationContext instanceof WebApplicationContext) { createChildManagementContext();
createChildManagementContext(); }
} if (managementPort == ManagementServerPort.SAME
if (managementPort == ManagementServerPort.SAME && this.applicationContext.getEnvironment() instanceof ConfigurableEnvironment) {
&& this.applicationContext.getEnvironment() instanceof ConfigurableEnvironment) { addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.applicationContext
addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.applicationContext .getEnvironment());
.getEnvironment());
}
} }
} }

@ -28,12 +28,11 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
@ -193,7 +192,7 @@ abstract class BeanTypeRegistry {
* implementations that allow eager class loading. * implementations that allow eager class loading.
*/ */
static class OptimizedBeanTypeRegistry extends BeanTypeRegistry implements static class OptimizedBeanTypeRegistry extends BeanTypeRegistry implements
ApplicationListener<ContextRefreshedEvent> { SmartInitializingSingleton {
private static final String BEAN_NAME = BeanTypeRegistry.class.getName(); private static final String BEAN_NAME = BeanTypeRegistry.class.getName();
@ -208,7 +207,7 @@ abstract class BeanTypeRegistry {
} }
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void afterSingletonsInstantiated() {
// We're done at this point, free up some memory // We're done at this point, free up some memory
this.beanTypes.clear(); this.beanTypes.clear();
this.lastBeanDefinitionCount = 0; this.lastBeanDefinitionCount = 0;

@ -22,16 +22,15 @@ import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User; import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationEventPublisher; import org.springframework.security.authentication.AuthenticationEventPublisher;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
@ -110,18 +109,21 @@ public class AuthenticationManagerConfiguration extends
@Component @Component
protected static class AuthenticationManagerConfigurationListener implements protected static class AuthenticationManagerConfigurationListener implements
ApplicationListener<ContextRefreshedEvent> { SmartInitializingSingleton {
@Autowired @Autowired
private AuthenticationEventPublisher authenticationEventPublisher; private AuthenticationEventPublisher authenticationEventPublisher;
@Autowired
private ApplicationContext context;
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void afterSingletonsInstantiated() {
ApplicationContext context = event.getApplicationContext(); if (this.context.getBeanNamesForType(AuthenticationManager.class).length == 0) {
if (context.getBeanNamesForType(AuthenticationManager.class).length == 0) {
return; return;
} }
AuthenticationManager manager = context.getBean(AuthenticationManager.class); AuthenticationManager manager = this.context
.getBean(AuthenticationManager.class);
if (manager instanceof ProviderManager) { if (manager instanceof ProviderManager) {
((ProviderManager) manager) ((ProviderManager) manager)
.setAuthenticationEventPublisher(this.authenticationEventPublisher); .setAuthenticationEventPublisher(this.authenticationEventPublisher);

@ -22,13 +22,12 @@ import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -92,7 +91,7 @@ class EntityScanRegistrar implements ImportBeanDefinitionRegistrar {
* on an {@link EntityScan} annotation. * on an {@link EntityScan} annotation.
*/ */
static class EntityScanBeanPostProcessor implements BeanPostProcessor, static class EntityScanBeanPostProcessor implements BeanPostProcessor,
ApplicationListener<ContextRefreshedEvent> { SmartInitializingSingleton {
private final String[] packagesToScan; private final String[] packagesToScan;
@ -120,7 +119,7 @@ class EntityScanRegistrar implements ImportBeanDefinitionRegistrar {
} }
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void afterSingletonsInstantiated() {
Assert.state(this.processed, "Unable to configure " Assert.state(this.processed, "Unable to configure "
+ "LocalContainerEntityManagerFactoryBean from @EntityScan, " + "LocalContainerEntityManagerFactoryBean from @EntityScan, "
+ "ensure an appropriate bean is registered."); + "ensure an appropriate bean is registered.");

Loading…
Cancel
Save