@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 {
}
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{
}
, Java .