We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
ozive023 18.47 Properties with Spring and Spring Boot| Baeloung
Properties with Spring and Spring Boot
Vr >
q
Restauration - Expéronce ce
CANASUC Tet of Sug
Y (httos //adsfreestarcom/?
Last updated: September 2,2
\Written by. Eugen Paraschiv (attps// wwwsbacldung.com/author/eugen)
Spring Boot (https://www.baeldung.com/category/spring/spring-boot)
‘Spring Core Basics (https:// www baeldung.com/tag/spring-core-basics)
Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:
>» CHECK OUT THE COURSE (/Is-course-start)
>
isbek.mcn .
=) Comment les territoires participent-
UNTERRITOIRE
‘QUISE LANCE DANS. ils aujourd'hui au développement
UN PROGRAMME fg , F
DE DECARBONATION, d'une économie verte ? Découvrez le...
= a8 ¥
hitpsslvw-baeldung.compropertios-with-spring wntozrve023 18.47 Properties with Spring and Spring Boot | Baeloung
https//ads freestarcom/?
y
tutm_campaign-brandinggutm .medium-banner8utm source-baeldung.com&utm_content-baeldung_incontent
ey)
1. Overview
‘This tutorial will show how to set up and use properties in Spring via Java configuration and @PropertySource.
Well also see how properties work in Spring Boot.
Further reading:
Spring Expression Language Guide (/spring-expression-tanguage)
‘This article explores Spring Exoression Language (S3£L).a powerful expression language thal supports querying and
‘manipulating object graphs at runtime,
Read more (/spring-expression language) —>
Configure a Spring Boot Web Application (/spring-boot-application-configuration)
Some ofthe more useful configs fora Sping Boot application.
Read more (/spring-boot-application-configuration) —+
Guide to @ConfigurationProperties in Spring Boot (/configuration-properties-in-
spring-boot)
‘Aaquick end practical quide to @Contiguration?roperties annotation in Spring Boot
Read more (/configuration-propertiessin-spring-boot) —+
Annotations
2. Register a Properties
‘Spring 31 also introduces the new @PropertySource annotation as a convenient mechanism for adding properly
sources to the environment.
\We can use this annotation in conjunction with the @Configuration annotation:
econfigurat on oO
Proper tySource( "classpath: foo. properties"
public class Propertieswithavatonfig {
We
y
Another very useful way to register a new properties file is using a placeholder, which allows us to dynamically
select the right file at runtime
‘Get mote done: SAS Viya isa Frites-en daantage = SAS Viy
roductve Al and Anais s
Bos ss
(https /ads reestarcom/?
¥
l4ung.comipropertios-with-spring aanozive023 18.47 Properties with Spring and Spring Boot| Baeloung
Property ure “ oO
“elasspath:persistence-s{envTarget:nysql) properties"
»
2.1. Defining Multiple Property Locations
‘The @PropertySource annotation is repeatable according to Java 8 conventions
https./docsoracle.com/javase/tutorial/java/annolalions/repealinghiml. Therefore, if were using Java 8 or
higher. we can use this annotation to define multiple property locations:
erropertySource( "classpath: foo. properties
@PropertySource("ctasspath:bar. properties")
public class Propertieswithsavaconfig {
w
O
y
Of course, we can also use the a@PropertySources annotation and specify an array of @PropertySource. This
‘works in any supported Java version, not justin Java 8 or higher
‘@PropertySource("classpath: foo.properties"), 0
»
public class Propertieswithavatonfig {
w
y
Incither case. its worth noting that in the event of a property name collision, the last source read takes
precedence.
3. Using/Injecting Properties
Injecting a property with the @Value annotation (/spring-value-annotation) is straightforward!
private String jdbcurls O
We can also specify a default value for the property:
Get more done: SAS Viya isa Frites-en daantage = SAS Viy
productive Aland Anais Hiss
Bas
(https /ads reestarcom/?
evaluet "S¢Sabe ur :aeFauteUet"
private string jabeurt;
im)
‘The new PropertySourcesPlacehoidlerConfigurer added in Spring 3 resolve $L.1 placeholders within bean
definition property values and @Value annotations.
Finally. we can obtain the value of a property using the Environment API
¥
htipsifwnww.bacldung,comlproperties-with-spring antozive023 18.47 Properties with Spring and Spring Boot| Baeloung
stowire “ oO
Private Environment envi
datasource. seturl (env. petProperty("jabe.url™));
4. Properties With Spring Boot
Before we go into more advanced configuration options for properties. lets spend some time looking at the new
properties support in Spring Boot.
Generally speaking, this new support involves less configuration compared to standard Spring, which is of
‘course one of the main goals of Boot,
4.1. application properties: the Default Property File
Boot applies its typical convention over configuration approach to property files. This means that we can simply
put an application properties file in our src/main/resources directory, and it will be auto-detected. We can
then inject any loaded properties from it as normal,
So. by using this default fle, we dont have to explicitly register a PropertySource or even provide a path to a
property fle.
We can also configure a different file at runtime if we need to, using an environment property:
[=
‘As of Spring Boot 23 thllos://github com/spring-projects/spring-boot/ wiki/Spring-Boot-23-Release-
Notes#support-of-wildcard-locations-for-configuration-fles), we ean also specify wildcard locations for
configuration files.
For example, we can set the spring config ocation property to config/“/
‘jar app.fer --spring.config. locationsclesspath: /another-Lacat ton. properties oO
| {ava Jar app.jar ~-epring.cotig.locationscontig/+/ oO
This way, Spring Boot will look for configuration fles matching the config// directory pattern outside of our jar fle,
This comes in handy when we have multisle sources of configuration properties.
Since version 240, Spring Boot supports using multi-document properties files, simiariy as YAML does
{nttps:/yamLorg/spec/12/spechtmliit2760396) by design:
baeldung.custonProperty=derautevatue oO
bee lung cus tonPropertymoverriddenvalue
Note that for properties files, the three-dashes notation is preceded by a comment character (#
4.2. Environment-Specific Properties File
v
htipsifwnww.bacldung,comlproperties-with-spring annozive023 18.47 Properties with Spring and Spring Boot| Baeloung
Ife need te target aitferent enyisonments, there's a built-in mechanism for that in Boot
oS
“a
Ps
ees
Fa
sas a
thios/adstestarcom/?
y
‘We can simply define an application-environment properties file in the src/main/resources directory, and,
then set a Spring profile with the same environment name.
For example, if we define a “staging” environment that means well have to define a staging profile and then
application-staging properties.
This env file will be loaded and will take precedence over the default property fle, Note that the default file will
siill be loaded, its just that when there is a property collision, the environment-specific property file takes
precedence.
4.3. Test-Specific Properties File
‘We might also have a requirement to use different property values when our application is under test.
‘Spring Boot handles this for us by Looking in our src/test/resources directory during a test run, Again, default
properties will still be injectable as normal but willbe overridden by these if there is a collision.
4.4. The @TestPropertySource Annotation
lf we need more granular control over test properties, then we can use the @TestPropertySource
(hlte.7/docs spring io/ spring/docs/current/javadoc-
api/org/springframework/test/context/ TestPropertySource html) annotation.
This allows us to set test properties for a specific test context, taking precedence over the default property
sources:
Se an ehSrnceeeelnes«
value "$> CHECK OUT THE COURSE (/1s-course-end)
Learning to build your API
with Spring?
htipsifwnww.bacldung,comlproperties-with-spring sontozive023 18.47 Properties with Spring and Spring Boot| Baeloung
‘owrildad the E-book (/rest-api-spring-guide)
‘Comments are closed on this article!
‘COURSES
SERIES
JAWA BACK TO BASICS" TUTORIAL V/IAVALTUTORAL
‘ABOUT
htipsifwnww.bacldung,comlproperties-with-spring wnt