Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Updating Spring Placeholder Values #972

Merged
merged 2 commits into from
Feb 24, 2018

Conversation

nobodyiam
Copy link
Member

Auto Updating Spring Placeholder Values

1. Features

Spring placeholder values will now be auto updated when new configs are released in Apollo.

1.1 @Value Placeholders

Full support is provided for:

  1. @Value placeholders annotated on fields
  2. @Value placeholders annotated on methods
  3. @Value placeholders with Spring Expression Language

For example, the following 3 fields can all be auto updated when the corresponding config is changed:

public class TestJavaConfigBean {

  @Value("${timeout:100}")
  private int timeout;
  private int batch;
  @Value("#{new java.text.SimpleDateFormat('${dateFormat}').parse('${dateProperty}')}")
  private Date someDate;

  @Value("${batch:200}")
  public void setBatch(int batch) {
    this.batch = batch;
  }

  public int getTimeout() {
    return timeout;
  }

  public int getBatch() {
    return batch;
  }

  public Date getSomeDate() {
    return someDate;
  }
}

@Configuration
@EnableApolloConfig
public class AppConfig {
  @Bean
  TestJavaConfigBean testJavaConfigBean() {
    return new TestJavaConfigBean();
  }
}

Sample Configurations:

timeout=1000
batch=200
dateFormat=yyyy-MM-dd HH:mm:ss.SSS
dateProperty=2018-02-23 20:01:02.123

We also provide limited support for nested placeholders.

For example, with the following configurations and program:

  1. batch equals to normalBatch at first, so the initial nestedProperty equals to 1000
  2. When batch is changed to specialBatch, then the nestedProperty field will be changed to 5000
  3. However, if we don't change batch config and change normalBatch to 5000 instead, then the nestedProperty won't be auto updated
batch=normalBatch
normalBatch=1000
specialBatch=5000
public class TestNestedPropertyBean {

  @Value("${${batch}}")
  private int nestedProperty;

  public int getNestedProperty() {
    return nestedProperty;
  }
}

1.2 XML Placeholders

Full support is provided for placeholders for property field.

For example, the following 3 fields can all be auto updated when the corresponding config is changed:

public class TestXmlBean {
  private int timeout;
  private int batch;
  private Date someDate;

  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }

  public int getTimeout() {
    return timeout;
  }

  public void setBatch(int batch) {
    this.batch = batch;
  }

  public int getBatch() {
    return batch;
  }

  public void setSomeDate(Date someDate) {
    this.someDate = someDate;
  }

  public Date getSomeDate() {
    return someDate;
  }
}

Xml Config:

<apollo:config/>

<bean class="com.ctrip.framework.apollo.spring.demo.TestXmlBean">
    <property name="timeout" value="${timeout:100}"/>
    <property name="batch" value="${batch:200}"/>
    <property name="someDate" value="#{new java.text.SimpleDateFormat('${dateFormat}').parse('${dateProperty}')}"/>
</bean>

Sample Configurations:

timeout=1000
batch=200
dateFormat=yyyy-MM-dd HH:mm:ss.SSS
dateProperty=2018-02-23 20:01:02.123

We also provide limited support for nested placeholders, the same as @Value placeholders.

2. Configuration

This auto update feature is turned on by default, you could turn off this feature by:

  1. Specify apollo.autoUpdateInjectedSpringProperties=false in System Properties
    For example, you may pass -Dapollo.autoUpdateInjectedSpringProperties=false when starting the Java program

  2. Specify apollo.autoUpdateInjectedSpringProperties=false in META-INF/app.properties
    For example,

    app.id=SampleApp
    apollo.autoUpdateInjectedSpringProperties=false

If apollo.autoUpdateInjectedSpringProperties is specified in both System Properties and META-INF/app.properties, then the one specified in System Properties wins.

zhegexiaohuozi and others added 2 commits February 20, 2018 17:24
2. Support XML config placeholders
3. Add more tests to cover all kinds of cases
4. Update apollo demo to adapt for auto update functionality
@coveralls
Copy link

Coverage Status

Coverage increased (+1.4%) to 51.829% when pulling 3efd4a3 on nobodyiam:auto-update-value-merge into 7edfb1a on ctripcorp:master.

@codecov-io
Copy link

Codecov Report

Merging #972 into master will increase coverage by 1.36%.
The diff coverage is 90.61%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #972      +/-   ##
============================================
+ Coverage     46.91%   48.28%   +1.36%     
- Complexity     1554     1650      +96     
============================================
  Files           354      360       +6     
  Lines          9755    10062     +307     
  Branches        968     1012      +44     
============================================
+ Hits           4577     4858     +281     
- Misses         4835     4847      +12     
- Partials        343      357      +14
Impacted Files Coverage Δ Complexity Δ
.../apollo/spring/property/SpringValueDefinition.java 100% <100%> (ø) 4 <4> (?)
...ork/apollo/spring/config/ConfigPropertySource.java 100% <100%> (ø) 6 <1> (+1) ⬆️
...pring/boot/ApolloSpringApplicationRunListener.java 86.66% <100%> (+0.95%) 9 <1> (ø) ⬇️
...apollo/spring/config/PropertySourcesProcessor.java 100% <100%> (ø) 12 <0> (ø) ⬇️
...va/com/ctrip/framework/apollo/util/ConfigUtil.java 82.92% <100%> (+1.34%) 38 <4> (+4) ⬆️
...pollo/spring/annotation/ApolloConfigRegistrar.java 100% <100%> (ø) 2 <0> (ø) ⬇️
...llo/spring/config/ConfigPropertySourceFactory.java 100% <100%> (ø) 3 <3> (?)
.../framework/apollo/spring/property/SpringValue.java 100% <100%> (ø) 15 <15> (?)
.../spring/config/ConfigPropertySourcesProcessor.java 100% <100%> (ø) 3 <1> (+1) ⬆️
...ip/framework/apollo/internals/DefaultInjector.java 69.23% <100%> (+2.56%) 3 <0> (ø) ⬇️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7edfb1a...3efd4a3. Read the comment docs.

@zollty
Copy link

zollty commented Mar 9, 2018

Great job!

@seven7ma
Copy link

Perfect!

@caoyang5858
Copy link

nice

@beareverse
Copy link

嗯!!

@caoyang5858
Copy link

caoyang5858 commented Mar 13, 2022 via email

@nobodyiam nobodyiam deleted the auto-update-value-merge branch March 14, 2022 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants