<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luke 10X</title>
    <description>The latest articles on DEV Community by Luke 10X (@luke10x).</description>
    <link>https://dev.to/luke10x</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F81200%2F0332d375-e2c0-40fa-be9d-dd63b6734535.png</url>
      <title>DEV Community: Luke 10X</title>
      <link>https://dev.to/luke10x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luke10x"/>
    <language>en</language>
    <item>
      <title>Config Management in Spring Boot applications</title>
      <dc:creator>Luke 10X</dc:creator>
      <pubDate>Wed, 12 Oct 2022 06:01:15 +0000</pubDate>
      <link>https://dev.to/luke10x/config-management-in-spring-boot-applications-27ga</link>
      <guid>https://dev.to/luke10x/config-management-in-spring-boot-applications-27ga</guid>
      <description>&lt;p&gt;Although you may find many different ways to work with configuration in Spring Boot applications, across the industry&lt;br&gt;
there is one most common simple practice that results in reasonably flexible and maintainable solutions. I will try to outline it below without going too deep into exploring all possible options, just to make it short and quick to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Inject environment variables to your components with @Value
&lt;/h2&gt;

&lt;p&gt;The configuration will be injected by Spring components, but it has to be annotated with @Value:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component
class MyComponent {

    @Value("${myappname.weatherapi.url}")
    private weatherApiUrl

    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also &lt;code&gt;myappname.weatherapi.url&lt;/code&gt; have to be defined in &lt;code&gt;application.properties&lt;/code&gt; (or &lt;code&gt;application.yaml&lt;/code&gt;):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myappname.weatherapi.url=http://weather.com/api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  1.1 Override it in tests
&lt;/h3&gt;

&lt;p&gt;You can override it in tests easily&lt;br&gt;
by re-specifying this @Value within the test:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SpringBootTest(properties = {
    "myappname.weatherapi.url=http://localhost:8888"
})
class MyComponentTest {

    @Autowired
    private MyComponent myComponent;

    // ..
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  2. Inject environment variables into application.properties
&lt;/h2&gt;

&lt;p&gt;If you have variables defined in your environment with&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export WEATHER_URL=http://wiremock:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;when you run Spring Boot application,&lt;br&gt;
it can be injected into &lt;code&gt;application.properties&lt;/code&gt; using this syntax:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.port=${PORT:8020}
myappname.wetherapi.url=${WEATHER_URL:http://localhost:8888}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now when you run your Spring Boot application,&lt;br&gt;
and if you have your PORT and WEATHER_APPLICATION set in your environment,&lt;br&gt;
then the environment variables will be in use.&lt;/p&gt;

&lt;p&gt;But if you don't have them in the environment where you run your application,&lt;br&gt;
then it will fall back to default values (8020 and localhost).&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Alternative locations for application.properties file
&lt;/h2&gt;

&lt;p&gt;By default &lt;code&gt;application.properties&lt;/code&gt; is used. But if Spring Boot profile is set then it will use a filename of this pattern: &lt;code&gt;application-{profilename}.properties&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;to run your Spring Boot app with another profile make sure you pass "-D" param like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java -jar -Dspring-boot.profile=local my-spring-boot-app.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;this will use &lt;code&gt;application-local.properties&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.1 Override it just for tests
&lt;/h2&gt;

&lt;p&gt;To use file &lt;code&gt;aplication-test.properties&lt;/code&gt; in on test class use:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SpringBootTest&lt;br&gt;
@ActiveProfiles("test")&lt;br&gt;
class MyComponentTest {&lt;br&gt;
    // ...&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  

&lt;ol&gt;
&lt;li&gt;Bypassing application (Bad Practice)
&lt;/li&gt;
&lt;/ol&gt;
&lt;/h2&gt;


&lt;p&gt;Your components could use the &lt;code&gt;SystemProperties&lt;/code&gt; class to access environment variables directly.&lt;br&gt;
In this way, you don't even need to have anything in &lt;code&gt;application.properties&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But the best practice is to use properties files,&lt;br&gt;
as this way it is clear what parameters could be tuned for this service. And given that your properties files are injecting environment variables, then your application is truly portable&lt;br&gt;
because Java code does not care really how the values get injected. So this allows us to even migrate away from Spring, or from using environment if that becomes a necessity.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Adhere 12 Factor App methodology
&lt;/h2&gt;

&lt;p&gt;It is worth mentioning that using multiple properties files&lt;br&gt;
for each deployment environment, while it may seem tempting to do, may not necessarily be the best idea. The reasoning is&lt;br&gt;
quite clearly defined in &lt;a href="https://12factor.net/config"&gt;12 Factor&lt;/a&gt; methodology. This way you make sure that your application is platform-agnostic and can be easily deployed in many different environments (including containers) without changes in the code.&lt;/p&gt;

&lt;p&gt;Although there are thousands of different ways to manage the configuration in Spring Boot applications, all you need to know&lt;br&gt;
is to follow these simple 5 steps to make sure your applications&lt;br&gt;
are flexible and scaleable and cloud-ready.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
