Nuñ*_*ada 1 java spring spring-mvc spring-boot property-placeholder
我已经使用 Spring Initializr 在 macOS Sierra 中使用嵌入式 Tomcat + Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序。我想在 Mac OS 中使用系统变量用户主文件夹名称
我的 Spring Boot 应用程序中有这个 Spring 类配置
@Configuration
@Profile("dev")
@PropertySource("file:///{user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {
@Bean
public EmailService emailService() {
return new MockEmailService();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我启动应用程序时出现此错误
Caused by: java.io.FileNotFoundException: /{user.home}/.devopsbuddy/application-dev.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:169)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58)
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:65)
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:36)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:440)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:190)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)
... 13 common frames omitted
Run Code Online (Sandbox Code Playgroud)
@PropertySource 最初是作为 Spring 3.1 的一部分添加的,用于导入资源
@PropertySource 资源位置中存在的任何 ${...} 占位符都将根据已针对环境注册的一组属性源进行解析。
对于您的问题,您错过了添加美元符号 ($)。希望在添加美元符号后,您的问题将得到解决。
您可以通过多种方式添加 @PropertySource。对于基本用途,
@Configuration
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {
// more configuration ...
}
Run Code Online (Sandbox Code Playgroud)
执行时,属性将从位于类路径根目录的 application.properties 文件中导入。classpath 是默认位置,因此可以省略:
@Configuration
@PropertySource("application.properties")
public class ApplicationConfig {
}
Run Code Online (Sandbox Code Playgroud)
或者,可以指定 file: location 来指定位于主机环境中其他位置的属性文件:
@PropertySource("file:/path/to/application.properties")
Run Code Online (Sandbox Code Playgroud)
或者你可以使用
@PropertySource("file:${CONF_DIR}/application.properties")
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)$ echo $CONF_DIR /path/to/directory/with/app/config/files
在您的情况下,您必须使用`
@PropertySource("file:${user.home}/application.properties")
`
和 $user.home 将给出以下位置。您将把您的属性文件放在该位置。
$ echo $user.home
/home/yourusername
Run Code Online (Sandbox Code Playgroud)
在春季 4:
Spring 4 为@ProperySource 带来了两个新特性。
第一个新功能:
它处理丢失的文件。默认情况下,如果没有找到已声明的文件,Spring 将抛出异常。
@PropertySource(value = "missing.properties", ignoreResourceNotFound = true)
Run Code Online (Sandbox Code Playgroud)
如果不使用ignoreResourceNotFound = true,则会出现以下错误。
java.lang.IllegalStateException: Failed to load ApplicationContext
[...]
Caused by: java.io.FileNotFoundException: class path resource [missing.properties] cannot be opened because it does not exist.
Run Code Online (Sandbox Code Playgroud)
第二个新功能:
其次,有一个名为 @PropertySources 的新注释,它允许您声明重复的 @PropertySource 注释:
@PropertySources({
@PropertySource("default.properties"),
@PropertySource("overriding.properties")
})
Run Code Online (Sandbox Code Playgroud)
注意:同样,属性文件声明的顺序很重要。正如上面示例中的文件名所建议的那样,如果稍后声明的文件包含相同的键,则它们将覆盖任何先前的值。
把它放在一起
总而言之,属性源配置可以实现如下:
@Configuration
@PropertySources({
@PropertySource("default.properties"),
@PropertySource(value = "file:${CONF_DIR}/optional-override.properties", ignoreResourceNotFound = true)
}
public class ApplicationConfig {
}
Run Code Online (Sandbox Code Playgroud)
在 Java 8 中
在 Java 8 中,@PropertySources 注解将是多余的,因为 Java8 引入了重复注解。这意味着可以在相同的位置根据需要重复相同的注释。
在 Java8 之前,要重复注解,必须将它们分组在一个容器注解中
@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)
})
public class Car{
//code goes in here
}
Run Code Online (Sandbox Code Playgroud)
使用 Java8 重复注解,它使我们可以灵活地编写相同的内容而无需任何容器注解。
@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}
Run Code Online (Sandbox Code Playgroud)
虽然这里没有使用容器注解,但这次 Java 编译器负责将两个注解包装到一个容器中。
所有功劳归功于马蒂亚斯·塞弗森
| 归档时间: |
|
| 查看次数: |
3467 次 |
| 最近记录: |