SpringBoot does not replace the system variable {user.home} in Spring Tool Suite Version: 3.8.4.RELEASE

I created the Spring Boot web application using the Spring Initializr using the built-in Tomcat + Thymeleaf template engine on macOS Sierra. I want to use the name System Variable The name of the user's home folder on Mac OS

I have this Spring class configuration in my Spring application to load

@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 starting 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
0
source share
3 answers

@PropertySource was first added as part of Spring 3.1 to import resources

@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 {
}

: location , :

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

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

`

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

`

$user.home . .

 $ echo $user.home
 /home/yourusername

Spring 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

Java8 ,

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

, Java .

:

+1

Spring PropertyPlaceholderConfigurer, , .. ${variable or property name}. , ${user.home}. $.

+1

As long as the system variable exists ${sys:user.home}, there will be systax.

0
source

Source: https://habr.com/ru/post/1017050/


All Articles