Skip to content

Commit a7c1f6b

Browse files
committedNov 10, 2009
SPR-5507 Added SmartLifecycle interface with 'isAutoStartup' method and added an 'onRefresh' method to the LifecycleProcessor.
1 parent 91dd752 commit a7c1f6b

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed
 

‎org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
*/
2323
public interface LifecycleProcessor extends Lifecycle {
2424

25+
void onRefresh();
26+
2527
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context;
18+
19+
/**
20+
* @author Mark Fisher
21+
* @since 3.0
22+
*/
23+
public interface SmartLifecycle extends Lifecycle {
24+
25+
boolean isAutoStartup();
26+
27+
}

‎org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -808,10 +808,13 @@ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory b
808808
}
809809

810810
/**
811-
* Finish the refresh of this context, publishing the
811+
* Finish the refresh of this context, invoking the LifecycleProcessor's
812+
* onRefresh() method and publishing the
812813
* {@link org.springframework.context.event.ContextRefreshedEvent}.
813814
*/
814815
protected void finishRefresh() {
816+
this.lifecycleProcessor.onRefresh();
817+
815818
// Publish the final event.
816819
publishEvent(new ContextRefreshedEvent(this));
817820
}

‎org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

+30
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2626
import org.springframework.context.Lifecycle;
2727
import org.springframework.context.LifecycleProcessor;
28+
import org.springframework.context.SmartLifecycle;
2829
import org.springframework.util.Assert;
2930

3031
/**
@@ -68,6 +69,23 @@ public void stop() {
6869
this.running = false;
6970
}
7071

72+
public void onRefresh() {
73+
Map<String, SmartLifecycle> lifecycleBeans = getSmartLifecycleBeans();
74+
for (String beanName : new LinkedHashSet<String>(lifecycleBeans.keySet())) {
75+
SmartLifecycle bean = lifecycleBeans.get(beanName);
76+
if (bean != null && bean.isAutoStartup()) {
77+
String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
78+
for (String dependency : dependenciesForBean) {
79+
doStart(lifecycleBeans, dependency);
80+
}
81+
if (!bean.isRunning()) {
82+
bean.start();
83+
}
84+
lifecycleBeans.remove(beanName);
85+
}
86+
}
87+
}
88+
7189
/**
7290
* Start the specified bean as part of the given set of Lifecycle beans,
7391
* making sure that any beans that it depends on are started first.
@@ -120,4 +138,16 @@ private Map<String, Lifecycle> getLifecycleBeans() {
120138
return beans;
121139
}
122140

141+
private Map<String, SmartLifecycle> getSmartLifecycleBeans() {
142+
String[] beanNames = beanFactory.getSingletonNames();
143+
Map<String, SmartLifecycle> beans = new LinkedHashMap<String, SmartLifecycle>();
144+
for (String beanName : beanNames) {
145+
Object bean = beanFactory.getSingleton(beanName);
146+
if (bean instanceof SmartLifecycle) {
147+
beans.put(beanName, (SmartLifecycle) bean);
148+
}
149+
}
150+
return beans;
151+
}
152+
123153
}

0 commit comments

Comments
 (0)
Please sign in to comment.