@RefreshScope与不同的*.properties文件

huangapple go评论148阅读模式
英文:

@RefreshScope with different *.properties files

问题

我想在调用刷新端点时重新加载多个配置文件。在 application.properties 文件中的条目中,它可以正常运行。其他任何文件都无法刷新。

这里是一个小例子:

pom.xml

...
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<version>2.3.3.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
		<version>2.3.3.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter</artifactId>
		<version>2.2.4.RELEASE</version>
	</dependency>
</dependencies>
...

application.properties

greeting=Hi
management.endpoints.web.exposure.include=refresh

test.properties

test=Test

ConfigFileHolder1

@Repository
@PropertySource(value="application.properties")
@RefreshScope
public class ConfigFileHolder1 {
	@Value("${greeting}")
	private String greeting;
	
	public String getGreeting() {
		return greeting;
	}
}

ConfigFileHolder2

@Repository
@PropertySource(value="test.properties")
@RefreshScope
public class ConfigFileHolder2 {
	
	@Value("${test}")
	private String test;
	
	public String getTest() {
		return test;
	}
}

ApiController

@Controller
@RefreshScope
public class ApiController implements Api {

	@Autowired
	private ConfigFileHolder1 config1;
	
	@Autowired
	private ConfigFileHolder2 config2;

   // do something with config1 and config2
   ...
}

只有 ConfigFileHolder1 在调用刷新端点后会刷新其值。要刷新 ConfigFileHolder2 的值,应用程序必须重新启动。

我需要更改什么来刷新所有配置文件/ConfigFileHolder 的值呢?

谢谢您的帮助。

英文:

I want to reload multiple config files when the refresh endpoint is called. It works perfectly fine with the entries in the application.properties file. Any other file does not refresh.

Here a small example:

pom.xml

...
&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
		&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
		&lt;artifactId&gt;spring-cloud-starter&lt;/artifactId&gt;
		&lt;version&gt;2.2.4.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;
...

application.properties

greeting=Hi
management.endpoints.web.exposure.include=refresh

test.properties

test=Test

ConfigFileHolder1

@Repository
@PropertySource(value=&quot;application.properties&quot;)
@RefreshScope
public class ConfigFileHolder1 {
	@Value(&quot;${greeting}&quot;)
	private String greeting;
	
	public String getGreeting() {
		return greeting;
	}
}

ConfigFileHolder2

@Repository
@PropertySource(value=&quot;test.properties&quot;)
@RefreshScope
public class ConfigFileHolder2 {
	
	@Value(&quot;${test}&quot;)
	private String test;
	
	public String getTest() {
		return test;
	}
}

ApiController

@Controller
@RefreshScope
public class ApiController implements Api {

	@Autowired
	private ConfigFileHolder1 config1;
	
	@Autowired
	private ConfigFileHolder2 config2;

   // do something with config1 and config2
   ...
}

Only ConfigFileHolder1 will refresh its value after the refresh-endpoint is called. To refresh the value of ConfigFileHolder2 the application has to restart.

What do I have to change to refresh the values of all my config-files/ConfigFileHolder?

Thanks for your help.

答案1

得分: 1

@RefreshScope 仅适用于由 Spring Boot 加载的属性,而不适用于后来在过程中加载的 @PropertySource。因此,您需要告诉 Spring Boot 加载附加的 配置文件

您可以通过添加名称 (spring.config.name) 或位置 spring.config.additional-location 来实现此操作。

在指定附加名称时,请确保同时包括默认的 application,否则该名称将不会再被加载。

--spring.config.name=application,test

将上述内容指定为参数时,所有位置都将被检查,以查找 application.propertiestest.properties,并且还将应用配置文件的横向扩展。

--spring.config.additional-location=classpath:test.properties

这将仅从类路径中加载 test.properties,并且基本上不可能在运行时更改文件,但文件将从该确切位置加载。不会应用配置文件的横向扩展。

英文:

The @RefreshScope will only work with the properties loaded by Spring Boot, not the @PropertySources loaded later in the process. Hence you will need to tell Spring Boot to load the additional configuration files.

You can do this by either adding names (spring.config.name) or locations spring.config.additional-location.

When specifying an additional name make sure to include the default application as well else that won't be loaded anymore.

--spring.config.name=application,test

When specifying the above as a parameter all locations will be checked for both an application.properties and test.properties and also the expansion for profiles will be applied.

--spring.config.additional-location=classpath:test.properties

This will only load the test.properties from the class path and will make it more or less impossible to change the file at runtime but the file will be loaded from that exact location. No profile expansion will be applied.

huangapple
  • 本文由 发表于 2020年8月26日 21:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63599044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定