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.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
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.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.web.context.WebApplicationContext;
@ -90,7 +90,7 @@ import org.springframework.web.servlet.DispatcherServlet;
ManagementServerPropertiesAutoConfiguration.class })
@EnableConfigurationProperties(HealthMvcEndpointProperties.class)
public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
ApplicationListener<ContextRefreshedEvent> {
SmartInitializingSingleton {
private static Log logger = LogFactory.getLog(EndpointWebMvcAutoConfiguration.class);
@ -122,8 +122,7 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext() == this.applicationContext) {
public void afterSingletonsInstantiated() {
ManagementServerPort managementPort = ManagementServerPort
.get(this.applicationContext);
if (managementPort == ManagementServerPort.DIFFERENT
@ -136,7 +135,6 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
.getEnvironment());
}
}
}
@Bean
@ConditionalOnMissingBean

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

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

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

Loading…
Cancel
Save