Browse Source
Add a "restart" Spring `Scope` that remains between application restarts. Allows beans such as the livereload server to remain active during restarts and not disconnect clients. See gh-3085pull/3077/merge
7 changed files with 268 additions and 0 deletions
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2012-2015 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.boot.developertools.restart; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import org.springframework.context.annotation.Scope; |
||||
|
||||
/** |
||||
* Restart {@code @Scope} Annotation used to indicate that a bean shoul remain beteen |
||||
* restarts. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.3.0 |
||||
* @see RestartScopeInitializer |
||||
*/ |
||||
@Target({ ElementType.TYPE, ElementType.METHOD }) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
@Scope("restart") |
||||
public @interface RestartScope { |
||||
|
||||
} |
||||
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* Copyright 2012-2015 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.boot.developertools.restart; |
||||
|
||||
import org.springframework.beans.factory.ObjectFactory; |
||||
import org.springframework.beans.factory.config.Scope; |
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
|
||||
/** |
||||
* Support for a 'restart' {@link Scope} that allows beans to remain between restarts. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.3.0 |
||||
*/ |
||||
public class RestartScopeInitializer implements |
||||
ApplicationContextInitializer<ConfigurableApplicationContext> { |
||||
|
||||
@Override |
||||
public void initialize(ConfigurableApplicationContext applicationContext) { |
||||
applicationContext.getBeanFactory().registerScope("restart", new RestartScope()); |
||||
} |
||||
|
||||
/** |
||||
* {@link Scope} that stores beans as {@link Restarter} attributes. |
||||
*/ |
||||
private static class RestartScope implements Scope { |
||||
|
||||
@Override |
||||
public Object get(String name, ObjectFactory<?> objectFactory) { |
||||
return Restarter.getInstance().getOrAddAttribute(name, objectFactory); |
||||
} |
||||
|
||||
@Override |
||||
public Object remove(String name) { |
||||
return Restarter.getInstance().removeAttribute(name); |
||||
} |
||||
|
||||
@Override |
||||
public void registerDestructionCallback(String name, Runnable callback) { |
||||
} |
||||
|
||||
@Override |
||||
public Object resolveContextualObject(String key) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public String getConversationId() { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
/* |
||||
* Copyright 2012-2015 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.boot.developertools.restart; |
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.context.ApplicationListener; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.event.ContextRefreshedEvent; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link RestartScopeInitializer}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class RestartScopeInitializerTests { |
||||
|
||||
private static AtomicInteger createCount; |
||||
|
||||
private static AtomicInteger refreshCount; |
||||
|
||||
@Test |
||||
public void restartScope() throws Exception { |
||||
createCount = new AtomicInteger(); |
||||
refreshCount = new AtomicInteger(); |
||||
ConfigurableApplicationContext context = runApplication(); |
||||
context.close(); |
||||
context = runApplication(); |
||||
context.close(); |
||||
assertThat(createCount.get(), equalTo(1)); |
||||
assertThat(refreshCount.get(), equalTo(2)); |
||||
} |
||||
|
||||
private ConfigurableApplicationContext runApplication() { |
||||
SpringApplication application = new SpringApplication(Config.class); |
||||
application.setWebEnvironment(false); |
||||
return application.run(); |
||||
} |
||||
|
||||
@Configuration |
||||
public static class Config { |
||||
|
||||
@Bean |
||||
@RestartScope |
||||
public ScopeTestBean scopeTestBean() { |
||||
return new ScopeTestBean(); |
||||
} |
||||
|
||||
} |
||||
|
||||
public static class ScopeTestBean implements |
||||
ApplicationListener<ContextRefreshedEvent> { |
||||
|
||||
public ScopeTestBean() { |
||||
createCount.incrementAndGet(); |
||||
} |
||||
|
||||
@Override |
||||
public void onApplicationEvent(ContextRefreshedEvent event) { |
||||
refreshCount.incrementAndGet(); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue