SpringBoot 不替换 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.RELEASE

查看:72
本文介绍了SpringBoot 不替换 Spring Tool Suite 版本中的系统变量 {user.home}:3.8.4.RELEASE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Initializr 在 macOS Sierra 中使用嵌入式 Tomcat + Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序.我想在 Mac OS 中使用 系统变量 用户主文件夹名称

我的 Spring Boot 应用程序中有这个 Spring 类配置

@Configuration@Profile("开发")@PropertySource("file:///{user.home}/.devopsbuddy/application-dev.properties")公共类 DevelopmentConfig {@豆公共电子邮件服务电子邮件服务(){返回新的 MockEmailService();}}

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

Caused by: java.io.FileNotFoundException:/{user.home}/.devopsbuddy/application-dev.properties (No such file or directory)在 java.io.FileInputStream.open0(本机方法)在 java.io.FileInputStream.open(FileInputStream.java:195)在 java.io.FileInputStream.(FileInputStream.java:138)在 java.io.FileInputStream.(FileInputStream.java:93)在 sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)在 sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)在 org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:169)在 org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:154)在 org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)在 org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:72)在 org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:58)在 org.springframework.core.io.support.ResourcePropertySource.(ResourcePropertySource.java:65)在 org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:36)在 org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:440)在 org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271)在 org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)在 org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:190)在 org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292)在 org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)在 org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198)在 org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)...省略了 13 个常用帧

解决方案

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

@PropertySource Java doc 据说,

<块引用>

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

对于您的问题,您没有添加美元符号($).希望添加美元符号后,您的问题将得到解决.

您可以通过多种方式添加@PropertySource.对于基本用途,

@Configuration@PropertySource(value = "classpath:application.properties")公共类 ApplicationConfig {//更多配置...}

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

@Configuration@PropertySource("application.properties")公共类 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/您的用户名

<小时><块引用>

春季 4:

Spring 4 为@ProperySource 带来了两个新特性.

第一个新功能:

它处理丢失的文件.默认情况下,如果没有找到已声明的文件,Spring 将抛出异常.

@PropertySource(value = "missing.properties", ignoreResourceNotFound = true)

如果不使用ignoreResourceNotFound = true,会报错如下.

java.lang.IllegalStateException: 加载 ApplicationContext 失败[...]引起:java.io.FileNotFoundException: 类路径资源 [missing.properties] 无法打开,因为它不存在.

第二个新功能:

其次,有一个名为@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)}公共类 ApplicationConfig {}

<小时><块引用>

在 Java 8 中

<小时>

在 Java 8 中,@PropertySources 注解将是多余的,因为 Java8 引入了重复注解.这意味着可以在相同的位置根据需要重复相同的注释.

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

@Manufactures({@制造商(名称=宝马"),@制造商(名称=揽胜")})公共类汽车{//代码在这里}

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

@Manufacturer(name = BMW")@制造商(名称=揽胜")公共类汽车{//代码在这里}

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

所有功劳归于 Mattias Severson

资源链接:

  1. Spring @PropertySource
  2. Java 8 重复注解说明5 分钟后

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat + Thymeleaf template engine in a macOS Sierra. I want to use the System variable User Home Folder Name in Mac OS

I have this Spring class configuration in my Spring Boot application

@Configuration
@Profile("dev")
@PropertySource("file:///{user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {

    @Bean
    public EmailService emailService() {
        return new MockEmailService();
    }

}

But I got this error when I start the application

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

解决方案

@PropertySource was first added as part of Spring 3.1 for importing resources

In the @PropertySource Java doc it is stated that,

Any ${…} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment.

For your issue, you have missed to add the dollar sign($). Hopefully after adding dollar sign, your problem will be solved.

You can add @PropertySource in many ways. For basic use,

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

    // more configuration ...
}

When executed, properties will be imported from the application.properties file, located in the classpath root. classpath is the default location, and can thus be omitted:

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

Alternatively, it is possible to specify a file: location to appoint a properties file that is located elsewhere on your host environment:

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

or you can use

@PropertySource("file:${CONF_DIR}/application.properties")

$ echo $CONF_DIR
/path/to/directory/with/app/config/files

In your case, you have to use `

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

`

and $user.home will give the following location. You will put your properties file on that location.

 $ echo $user.home
 /home/yourusername


In Spring 4:

Spring 4 brings two new features to the @ProperySource.

First new feature:

It deals with missing files. By default, Spring will throw an exception if it does not find the file that has been declared.

@PropertySource(value = "missing.properties", ignoreResourceNotFound = true)

If you don't use ignoreResourceNotFound = true, it will give the following error.

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.

Second new feature:

Secondly, there is a new annotation called @PropertySources that allows you to declare repeated @PropertySource annotations:

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

N.B: Yet again, the order of the property file declarations is important. As suggested by the file names in the example above, files declared later will override any previous value(s) if they contain the same key(s).

Putting It Together

To summarize, a property source configuration can be implemented like:

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


In Java 8


In Java 8, the @PropertySources annotation will be redundant, because Java8 introduces the repeating annotation. This means same annotations can be repeated as much as you want at same locations.

Prior to Java8, to have a repeated annotation, will have to group them in a container annotation

@Manufactures({
@Manufacturer(name ="BMW"),
@Manufacturer(name = "Range Rover")

})
public class Car{
//code goes in here
}

With Java8 repeating annotations, it gives us the flexibility to write the same thing without any container annotation

@Manufacturer(name = "BMW")
@Manufacturer(name= "Range Rover")
public class Car{
//code goes in here
}

Though the container annotation was not used here, the Java compiler this time around takes responsibility for wrapping the two annotations into a container.

All credit goes to Mattias Severson

Resource Link:

  1. Spring @PropertySource
  2. Java 8 Repeating Annotation Explained in 5 minutes

这篇关于SpringBoot 不替换 Spring Tool Suite 版本中的系统变量 {user.home}:3.8.4.RELEASE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆