Spring Boot 不替换 Spring Tool Suite 版本中的系统变量 user.home:3.8.4.RELEASE

Posted

技术标签:

【中文标题】Spring Boot 不替换 Spring Tool Suite 版本中的系统变量 user.home:3.8.4.RELEASE【英文标题】:SpringBoot don't replacen System variable user.home in Spring Tool Suite Version: 3.8.4.RELEASESpring Boot 不替换 Spring Tool Suite 版本中的系统变量 user.home:3.8.4.RELEASE 【发布时间】:2017-09-19 15:20:53 【问题描述】:

我使用 Spring Initializr 生成了一个 Spring Boot Web 应用程序,在 macOS Sierra 中使用嵌入式 Tomcat + Thymeleaf 模板引擎。 我想在 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();
    


但是当我启动应用程序时出现此错误

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

【问题讨论】:

【参考方案1】:

@PropertySource 最初是作为 Spring 3.1 的一部分添加的,用于导入资源

在@PropertySource Java doc 中指出,

@PropertySource 资源位置中存在的任何 $... 占位符 将针对已经设置的属性源解决 针对环境注册。

对于您的问题,您错过了添加美元符号($)。希望添加美元符号后,您的问题将得到解决。

您可以通过多种方式添加@PropertySource。基本使用,

@Configuration
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig 

    // more configuration ...

执行时,属性将从位于类路径根目录的 application.properties 文件中导入。 classpath 是默认位置,因此可以省略:

@Configuration
@PropertySource("application.properties")
public class ApplicationConfig 

或者,可以指定一个 file: location 来指定一个位于主机环境其他位置的属性文件:

@PropertySource("file:/path/to/application.properties")

或者你可以使用

@PropertySource("file:$CONF_DIR/application.properties")
$ echo $CONF_DIR
/path/to/directory/with/app/config/files

在你的情况下,你必须使用`

@PropertySource("file:$user.home/application.properties")

`

和 $user.home 将给出以下位置。你将把你的属性文件放在那个位置。

 $ echo $user.home
 /home/yourusername

春季 4:

Spring 4 为 @ProperySource 带来了两个新功能。

第一个新功能:

它处理丢失的文件。默认情况下,如果 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 的新注解允许您声明重复的@PropertySource 注解:

@PropertySources(
    @PropertySource("default.properties"),
    @PropertySource("overriding.properties")
)

N.B:再次强调,属性文件声明的顺序很重要。正如上面示例中的文件名所暗示的,如果稍后声明的文件包含相同的键,则它们将覆盖任何先前的值。

放在一起

总而言之,属性源配置可以这样实现:

@Configuration
@PropertySources(
    @PropertySource("default.properties"),
    @PropertySource(value = "file:$CONF_DIR/optional-override.properties", ignoreResourceNotFound = true)

public class ApplicationConfig 


在 Java 8 中


在 Java 8 中,@PropertySources 注释将是多余的,因为 Java8 引入了repeating annotation。这意味着可以在相同的位置尽可能多地重复相同的注释。

在 Java8 之前,要拥有重复注解,必须将它们分组到容器注解中

@Manufactures(
@Manufacturer(name =”BMW”),
@Manufacturer(name = “Range Rover”)

)
public class Car
//code goes in here

使用 Java8repeating annotations,它使我们可以灵活地编写相同的东西而无需任何容器注释

@Manufacturer(name = “BMW”)
@Manufacturer(name= “Range Rover”)
public class Car
//code goes in here

虽然这里没有使用容器注解,但这次 Java 编译器负责将两个注解包装到一个容器中。

所有功劳归Mattias Severson

资源链接:

    Spring @PropertySource Java 8 Repeating Annotation Explained in 5 minutes

【讨论】:

【参考方案2】:

Spring 使用PropertyPlaceholderConfigurer 来解析所有占位符,即$variable or property name。 所以,你应该使用$user.home。请注意$符号。

【讨论】:

【参考方案3】:

只要系统变量存在,$sys:user.home 就是系统税。

【讨论】:

以上是关于Spring Boot 不替换 Spring Tool Suite 版本中的系统变量 user.home:3.8.4.RELEASE的主要内容,如果未能解决你的问题,请参考以下文章

如何在 spring-boot 中替换最新的 Jetty 9?

如何在 Spring Boot 2.0 上将默认 hikari cp 替换为 tomcat 池

spring-boot 热加载实现替换Jrebel

spring boot 目录浏览功能 How to enable directory listing in Spring Boot

Spring Boot 部分替换自动配置

Maven-Spring 项目不加载 spring-boot-start 档案