Only eagerly initialize refresh scope beans that are not lazy

This commit is contained in:
Dave Syer
2016-01-29 13:11:44 +00:00
parent 2112bb23ec
commit f550de7559

View File

@@ -24,6 +24,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
@@ -70,11 +71,12 @@ import org.springframework.jmx.export.annotation.ManagedResource;
*/
@ManagedResource
public class RefreshScope extends GenericScope
implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor {
implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor, Ordered {
private ApplicationContext context;
private BeanDefinitionRegistry registry;
private boolean eager = true;
private int order = Ordered.LOWEST_PRECEDENCE - 100;
/**
* Create a scope instance and give it the default name: "refresh".
@@ -83,6 +85,15 @@ public class RefreshScope extends GenericScope
super.setName("refresh");
}
@Override
public int getOrder() {
return this.order;
}
public void setOrder(int order) {
this.order = order;
}
/**
* Flag to determine whether all beans in refresh scope should be instantiated eagerly
* on startup. Default true.
@@ -105,7 +116,8 @@ public class RefreshScope extends GenericScope
if (this.eager && this.registry != null) {
for (String name : this.context.getBeanDefinitionNames()) {
BeanDefinition definition = this.registry.getBeanDefinition(name);
if (this.getName().equals(definition.getScope())) {
if (this.getName().equals(definition.getScope())
&& !definition.isLazyInit()) {
this.context.getBean(name);
}
}