Refresh bean with spring cloud's @RefreshScope support

王宇轩 edited this page Feb 12, 2018 · 3 revisions

Refresh bean with spring boot's @RefreshScope support


  • Spring 4.3.x
  • Spring Boot 1.4.x

Add spring cloud dependency


Mark the bean with @RefreshScope and @EnableAutoConfiguration

@EnableAutoConfiguration // somewhere
public class ExampleBeanWithSpel {

	private String stringProperty;

	private int intProperty;

	private String computedValue;

	private void init() {
		computedValue = stringProperty + intProperty;

	public void someMethod() {
		System.out.println(String.format("My properties: [%s] - [%s] - [%s]", stringProperty, intProperty, computedValue));


Register the bean observed

ExampleBeanWithSpel bean = context.getBean(ExampleBeanWithSpel.class);
GeneralConfigGroup cg1 = (GeneralConfigGroup) context.getBean("propertyGroup1");

cg1.register(new IObserver() {

	public void notified(String data, String value) {

Example code


You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.