SPR-5507 Added SmartLifecycle interface with 'isAutoStartup' method and added an 'onRefresh' method to the LifecycleProcessor.
This commit is contained in:
parent
91dd752e71
commit
a7c1f6b730
|
|
@ -22,4 +22,6 @@ package org.springframework.context;
|
||||||
*/
|
*/
|
||||||
public interface LifecycleProcessor extends Lifecycle {
|
public interface LifecycleProcessor extends Lifecycle {
|
||||||
|
|
||||||
|
void onRefresh();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Mark Fisher
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public interface SmartLifecycle extends Lifecycle {
|
||||||
|
|
||||||
|
boolean isAutoStartup();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -808,10 +808,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finish the refresh of this context, publishing the
|
* Finish the refresh of this context, invoking the LifecycleProcessor's
|
||||||
|
* onRefresh() method and publishing the
|
||||||
* {@link org.springframework.context.event.ContextRefreshedEvent}.
|
* {@link org.springframework.context.event.ContextRefreshedEvent}.
|
||||||
*/
|
*/
|
||||||
protected void finishRefresh() {
|
protected void finishRefresh() {
|
||||||
|
this.lifecycleProcessor.onRefresh();
|
||||||
|
|
||||||
// Publish the final event.
|
// Publish the final event.
|
||||||
publishEvent(new ContextRefreshedEvent(this));
|
publishEvent(new ContextRefreshedEvent(this));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.context.Lifecycle;
|
import org.springframework.context.Lifecycle;
|
||||||
import org.springframework.context.LifecycleProcessor;
|
import org.springframework.context.LifecycleProcessor;
|
||||||
|
import org.springframework.context.SmartLifecycle;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -68,6 +69,23 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
||||||
this.running = false;
|
this.running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onRefresh() {
|
||||||
|
Map<String, SmartLifecycle> lifecycleBeans = getSmartLifecycleBeans();
|
||||||
|
for (String beanName : new LinkedHashSet<String>(lifecycleBeans.keySet())) {
|
||||||
|
SmartLifecycle bean = lifecycleBeans.get(beanName);
|
||||||
|
if (bean != null && bean.isAutoStartup()) {
|
||||||
|
String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
|
||||||
|
for (String dependency : dependenciesForBean) {
|
||||||
|
doStart(lifecycleBeans, dependency);
|
||||||
|
}
|
||||||
|
if (!bean.isRunning()) {
|
||||||
|
bean.start();
|
||||||
|
}
|
||||||
|
lifecycleBeans.remove(beanName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the specified bean as part of the given set of Lifecycle beans,
|
* Start the specified bean as part of the given set of Lifecycle beans,
|
||||||
* making sure that any beans that it depends on are started first.
|
* making sure that any beans that it depends on are started first.
|
||||||
|
|
@ -120,4 +138,16 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
|
||||||
return beans;
|
return beans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, SmartLifecycle> getSmartLifecycleBeans() {
|
||||||
|
String[] beanNames = beanFactory.getSingletonNames();
|
||||||
|
Map<String, SmartLifecycle> beans = new LinkedHashMap<String, SmartLifecycle>();
|
||||||
|
for (String beanName : beanNames) {
|
||||||
|
Object bean = beanFactory.getSingleton(beanName);
|
||||||
|
if (bean instanceof SmartLifecycle) {
|
||||||
|
beans.put(beanName, (SmartLifecycle) bean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return beans;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue