Spring 引导:外部 messages.properties 正在添加但未使用
Spring Boot: external messages.properties are being added but not used
我有两个 messages.properties
文件。一个位于 resources
内,另一个位于我的 .jar 文件外的一个名为 etc
.
的目录中
这是我的 PropertiesConfiguration class:
@Configuration
public class PropertiesConfiguration {
@Bean
public PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resourceLst = new ArrayList<Resource>();
resourceLst.add(new FileSystemResource("etc/application.properties"));
resourceLst.add(new FileSystemResource("etc/messages.properties"));
resourceLst.add(new FileSystemResource("etc/messages_et.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[]{}));
return ppc;
}
}
在日志中我看到了这个:
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]
11:18:43.764 WARN [main] PropertyPlaceholderConfigurer - Could not load properties from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]: etc\application.properties (The system cannot find the file specified)
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages.properties]
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages_et.properties]
据我所知,etc
中的 messages.properties
已加载。虽然当应用程序运行时,它的值不会被使用。它们来自我的 resources
项目文件夹中的默认 messages.properties
。我做错了什么吗?
我认为这里有这个问题的答案:
Spring Boot and multiple external configuration files
之前对我有用,所以值得一试!
首先明确SpringBoot.
的配置设置
在POM.XML中,确认一次布局为"ZIP"
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!-- added -->
<layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
</configuration>
</plugin>
</plugins>
</build>
因为,对于 PropertiesLauncher,布局是 "ZIP" 并确保避免使用 @EnableAutoConfiguration。
或对外部属性使用@PropertySource 注释。
(参考:Spring Boot PropertySource)。
Whosebug 已讨论此主题。
参考:
Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?
我的解决方案是:
@Configuration
public class SpringConfiguration {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("file:/path/to/file/messages");
messageSource.setCacheSeconds(10); //reload every 10 sec..
return messageSource;
}
}
它对我来说很完美,记住 省略基本名称的后缀 _et.properties,例如,如果您设置了一个名为 messages_et.properties
的文件基本名称将导致:messageSource.setBasename("file:/path/to/file/messages");
我有两个 messages.properties
文件。一个位于 resources
内,另一个位于我的 .jar 文件外的一个名为 etc
.
这是我的 PropertiesConfiguration class:
@Configuration
public class PropertiesConfiguration {
@Bean
public PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resourceLst = new ArrayList<Resource>();
resourceLst.add(new FileSystemResource("etc/application.properties"));
resourceLst.add(new FileSystemResource("etc/messages.properties"));
resourceLst.add(new FileSystemResource("etc/messages_et.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[]{}));
return ppc;
}
}
在日志中我看到了这个:
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]
11:18:43.764 WARN [main] PropertyPlaceholderConfigurer - Could not load properties from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]: etc\application.properties (The system cannot find the file specified)
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages.properties]
11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages_et.properties]
据我所知,etc
中的 messages.properties
已加载。虽然当应用程序运行时,它的值不会被使用。它们来自我的 resources
项目文件夹中的默认 messages.properties
。我做错了什么吗?
我认为这里有这个问题的答案: Spring Boot and multiple external configuration files
之前对我有用,所以值得一试!
首先明确SpringBoot.
的配置设置在POM.XML中,确认一次布局为"ZIP"
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!-- added -->
<layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
</configuration>
</plugin>
</plugins>
</build>
因为,对于 PropertiesLauncher,布局是 "ZIP" 并确保避免使用 @EnableAutoConfiguration。
或对外部属性使用@PropertySource 注释。 (参考:Spring Boot PropertySource)。
Whosebug 已讨论此主题。
参考:
Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?
我的解决方案是:
@Configuration
public class SpringConfiguration {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("file:/path/to/file/messages");
messageSource.setCacheSeconds(10); //reload every 10 sec..
return messageSource;
}
}
它对我来说很完美,记住 省略基本名称的后缀 _et.properties,例如,如果您设置了一个名为 messages_et.properties
的文件基本名称将导致:messageSource.setBasename("file:/path/to/file/messages");