Java SpringBoot不替换Spring工具套件版本3.8.4.RELEASE中的系统变量{user.home}
我已经使用SpringInitializer生成了一个SpringBootWeb应用程序,在macOS中使用嵌入式Tomcat+Thymeleaf模板引擎。 我想在Mac OS中使用系统变量User Home Folder Name 我的Spring启动应用程序中有这个Spring类配置Java SpringBoot不替换Spring工具套件版本3.8.4.RELEASE中的系统变量{user.home},java,spring,spring-mvc,spring-boot,property-placeholder,Java,Spring,Spring Mvc,Spring Boot,Property Placeholder,我已经使用SpringInitializer生成了一个SpringBootWeb应用程序,在macOS中使用嵌入式Tomcat+Thymeleaf模板引擎。 我想在Mac OS中使用系统变量User Home Folder Name 我的Spring启动应用程序中有这个Spring类配置 @Configuration @Profile("dev") @PropertySource("file:///{user.home}/.devopsbuddy/application-dev.properti
@Configuration
@Profile("dev")
@PropertySource("file:///{user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {
@Bean
public EmailService emailService() {
return new MockEmailService();
}
}
但是我在启动应用程序时遇到了这个错误
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
只要系统变量存在${sys:user.home}就是systax 只要系统变量存在${sys:user.home}就是systax Spring使用解析所有占位符的${variable或property name}。 因此,您应该使用${user.home}。请注意$符号。Spring使用它解析所有占位符,即${variable或property name}。
因此,您应该使用${user.home}。请注意$sign。@PropertySource是作为Spring3.1导入资源的一部分首次添加的 在报告中指出 @PropertySource资源位置中存在的任何${…}占位符 将针对已存在的属性源集进行解析 登记反对环境 对于您的问题,您没有添加美元符号$。希望在添加美元符号后,您的问题将得到解决 您可以通过多种方式添加@PropertySource。作为基本用途
@Configuration
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {
// more configuration ...
}
执行时,属性将从位于类路径根中的application.properties文件导入。类路径是默认位置,因此可以省略:
@Configuration
@PropertySource("application.properties")
public class ApplicationConfig {
}
或者,可以指定文件:位置以指定位于主机环境其他位置的属性文件:
@PropertySource("file:/path/to/application.properties")
或者你可以使用
@PropertySource("file:${CONF_DIR}/application.properties")
在您的情况下,您必须使用`
@PropertySourcefile:${user.home}/application.properties
`
$user.home将给出以下位置。您将把属性文件放在该位置
$ echo $user.home
/home/yourusername
在春季4:
Spring4为@PropertySource带来了两个新特性
第一个新功能:
它处理丢失的文件。默认情况下,如果Spring找不到已声明的文件,它将抛出异常
@PropertySource(value = "missing.properties", ignoreResourceNotFound = true)
如果不使用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.
第二个新功能:
其次,有一个名为@PropertySources的新注释,允许您声明重复的@PropertySources注释:
@PropertySources({
@PropertySource("default.properties"),
@PropertySource("overriding.properties")
})
注意:再一次,属性文件声明的顺序很重要。正如上面示例中的文件名所示,如果以后声明的文件包含相同的键,则它们将覆盖以前的任何值
把它放在一起
总之,属性源配置可以实现为:
@Configuration
@PropertySources({
@PropertySource("default.properties"),
@PropertySource(value = "file:${CONF_DIR}/optional-override.properties", ignoreResourceNotFound = true)
}
public class ApplicationConfig {
}
在Java8中
在Java8中,@PropertySources注释是多余的,因为Java8引入了。这意味着可以在相同的位置重复相同的注释
在Java8之前,要有重复的注释,必须将它们分组到容器注释中
@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)
})
public class Car{
//code goes in here
}
@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}
使用Java8,我们可以灵活地编写相同的内容,而无需任何容器注释
@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)
})
public class Car{
//code goes in here
}
@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}
虽然这里没有使用容器注释,但这次Java编译器负责将这两个注释包装到一个容器中
一切功劳归于
资源链接:
@PropertySource最初是作为Spring3.1的一部分添加的,用于导入资源 在报告中指出 @PropertySource资源位置中存在的任何${…}占位符 将针对已存在的属性源集进行解析 登记反对环境 对于您的问题,您没有添加美元符号$。希望在添加美元符号后,您的问题将得到解决 您可以通过多种方式添加@PropertySource。作为基本用途
@Configuration
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {
// more configuration ...
}
执行时,属性将从位于类路径根中的application.properties文件导入。类路径是默认位置,因此可以省略:
@Configuration
@PropertySource("application.properties")
public class ApplicationConfig {
}
或者,可以指定文件:位置以指定位于主机环境其他位置的属性文件:
@PropertySource("file:/path/to/application.properties")
或者你可以使用
@PropertySource("file:${CONF_DIR}/application.properties")
在您的情况下,您必须使用`
@PropertySourcefile:${user.home}/application.properties
`
$user.home将给出以下位置。您将把属性文件放在该位置
$ echo $user.home
/home/yourusername
在春季4:
Spring4为@PropertySource带来了两个新特性
第一个新功能:
它处理丢失的文件。默认情况下,如果Spring找不到已声明的文件,它将抛出异常
@PropertySource(value = "missing.properties", ignoreResourceNotFound = true)
如果不使用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.
第二个新功能:
其次,有一个名为@PropertySources的新注释,允许您声明重复的@PropertySources注释:
@PropertySources({
@PropertySource("default.properties"),
@PropertySource("overriding.properties")
})
注:再一次,秩序
属性文件声明的规则是很重要的。正如上面示例中的文件名所示,如果以后声明的文件包含相同的键,则它们将覆盖以前的任何值
把它放在一起
总之,属性源配置可以实现为:
@Configuration
@PropertySources({
@PropertySource("default.properties"),
@PropertySource(value = "file:${CONF_DIR}/optional-override.properties", ignoreResourceNotFound = true)
}
public class ApplicationConfig {
}
在Java8中
在Java8中,@PropertySources注释是多余的,因为Java8引入了。这意味着可以在相同的位置重复相同的注释
在Java8之前,要有重复的注释,必须将它们分组到容器注释中
@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)
})
public class Car{
//code goes in here
}
@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}
使用Java8,我们可以灵活地编写相同的内容,而无需任何容器注释
@Manufactures({
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)
})
public class Car{
//code goes in here
}
@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car{
//code goes in here
}
虽然这里没有使用容器注释,但这次Java编译器负责将这两个注释包装到一个容器中
一切功劳归于
资源链接: