From a7c1f6b730dec82f4d8ae381c283b3ec096b9283 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 10 Nov 2009 04:49:13 +0000 Subject: [PATCH] SPR-5507 Added SmartLifecycle interface with 'isAutoStartup' method and added an 'onRefresh' method to the LifecycleProcessor. --- .../context/LifecycleProcessor.java | 2 ++ .../context/SmartLifecycle.java | 27 +++++++++++++++++ .../support/AbstractApplicationContext.java | 5 +++- .../support/DefaultLifecycleProcessor.java | 30 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java diff --git a/org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java index 8d39eab60aa..aba0cb205b7 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/LifecycleProcessor.java @@ -22,4 +22,6 @@ package org.springframework.context; */ public interface LifecycleProcessor extends Lifecycle { + void onRefresh(); + } diff --git a/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java new file mode 100644 index 00000000000..e0b4fd46867 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java @@ -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(); + +} diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index d0db5de9037..b0f4e6c8e99 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -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}. */ protected void finishRefresh() { + this.lifecycleProcessor.onRefresh(); + // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); } diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java index 5dc249479b9..59cf544f0f3 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java @@ -25,6 +25,7 @@ import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.Lifecycle; import org.springframework.context.LifecycleProcessor; +import org.springframework.context.SmartLifecycle; import org.springframework.util.Assert; /** @@ -68,6 +69,23 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor this.running = false; } + public void onRefresh() { + Map lifecycleBeans = getSmartLifecycleBeans(); + for (String beanName : new LinkedHashSet(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, * making sure that any beans that it depends on are started first. @@ -120,4 +138,16 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor return beans; } + private Map getSmartLifecycleBeans() { + String[] beanNames = beanFactory.getSingletonNames(); + Map beans = new LinkedHashMap(); + for (String beanName : beanNames) { + Object bean = beanFactory.getSingleton(beanName); + if (bean instanceof SmartLifecycle) { + beans.put(beanName, (SmartLifecycle) bean); + } + } + return beans; + } + }