SmartLifecycleカスタムライフサイクル
24176 ワード
@Component
public class MyLifeCycle implements SmartLifecycle {
private boolean isRunning = false;
@Override
public void start() {
System.out.println("MyLifeCycle:start");
isRunning = true;
}
@Override
public void stop() {
System.out.println("MyLifeCycle:stop");
isRunning = false;
}
// true,stop(Runnable callback) stop() , false,start
@Override
public boolean isRunning() {
return isRunning;
}
// true start() , false
@Override
public boolean isAutoStartup() {
return true;
}
//SmartLifecycle , isRunning() true
@Override
public void stop(Runnable callback) {
callback.run();
isRunning = false;
}
// ,
@Override
public int getPhase() {
return 0;
}
}
ソースの追跡1.AbstractAplication Comptext
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
clearResourceCaches();
// Initialize lifecycle processor for this context.
// DefaultLifecycleProcessor
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
// DefaultLifecycleProcessor#onRefresh()
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
2.Default Lifecycle Processor1.onRefsh方法を実行する
2.startBens方法を実行する
private void startBeans(boolean autoStartupOnly) {
// Lifecycle
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new HashMap<>();
lifecycleBeans.forEach((beanName, bean) -> {
// Lifecycle , SmartLifecycle isAutoStartup() true
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
int phase = getPhase(bean);
LifecycleGroup group = phases.get(phase);
if (group == null) {
group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
// getPhase() key
phases.put(phase, group);
}
// getPhase() LifecycleGroup
group.add(beanName, bean);
}
});
if (!phases.isEmpty()) {
List<Integer> keys = new ArrayList<>(phases.keySet());
Collections.sort(keys);
for (Integer key : keys) {
// start()
phases.get(key).start();
}
}
}
Default Lifecycle Processor葃LifecycleGrouppublic void start() {
if (this.members.isEmpty()) {
return;
}
if (logger.isDebugEnabled()) {
logger.debug("Starting beans in phase " + this.phase);
}
Collections.sort(this.members);
//this.members getPhase() , start()
for (LifecycleGroupMember member : this.members) {
doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
}
}
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
// LifecycleGroup lifecycleBeans
Lifecycle bean = lifecycleBeans.remove(beanName);
if (bean != null && bean != this) {
String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
for (String dependency : dependenciesForBean) {
doStart(lifecycleBeans, dependency, autoStartupOnly);
}
//SmartLifecycle isRunning() isAutoStartup()
if (!bean.isRunning() &&
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
if (logger.isTraceEnabled()) {
logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
}
try {
bean.start();
}
catch (Throwable ex) {
throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
}
if (logger.isDebugEnabled()) {
logger.debug("Successfully started bean '" + beanName + "'");
}
}
}
}