top of page

Spring Boot External Configuration

  • taolius
  • Jan 28, 2016
  • 3 min read

Copied from (http://altfatterz.blogspot.com/2014/10/software-configuration-with-spring-boot.html)

Software configuration with Spring Boot

In this blog post I would like to show you the configuration possibilities of a Spring bean's name property in combination with Spring Boot. Let's consider the following simple bean.

@Service

public class GreetingService {

private static final Logger LOGGER = LoggerFactory.getLogger(GreetingService.class);

@Value("${greeting.name:World}")

private String name;

public String greet() {

if (LOGGER.isDebugEnabled()) {

LOGGER.debug("Greeting {}", name);

}

return "Hello " + name;

}

}

The @Value("${greeting.name:World}") means that the name can be configured via the greeting.name property and has the default value of "World". You can quickly try it by cloning my example repository which I have created for this blog post and accessing the http://localhost:8080

git clone https://github.com/altfatterz/configuration-with-spring-boot

cd configuration-with-spring-boot

mvn clean package

java -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war

With the help of Spring Boot Maven Plugin this example is creating a very simple war artifact starting an embedded tomcat instance. Now let's see what configuration options we have. We can configure the name property using a command line argument.

java -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war --greeting.name=Zoltan

We can set it also via a system property.

java -Dgreeting.name="Heisenberg" -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war

Or we can use an OS environment variable.

GREETING_NAME="Dexter" java -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war

Here you can see that I have used underscore since the OS does not allow me to use period-separated key name. But is not a problem for Spring Boot, it can match it. The Spring Boot Actuator module's /env endpoint can be very useful in analysing used configuration. We could also set it via a JNDI attribute. In order to demonstrate it, I will use Wildfly (formerly known as JBoss AS). Just drop the generated war file into your /standalone/deployments and after you have started the server (/bin/standalone.sh) add a JNDI binding via JBoss CLI tool.

$ ./jboss-cli.sh -c "/subsystem=naming/binding=java\:\/greeting.name:add(binding-type=simple,value=Backbase)"

{"outcome" => "success"}

$ ./jboss-cli.sh -c "reload"

view rawjboss-jndi-binding.sh hosted with ❤ by GitHub

And accessing the http://localhost:8080/configuration-with-spring-boot-0.0.1-SNAPSHOT/ you will see the name property was set via JNDI. The interested reader can have a look what modifications I needed to make to able to deploy it to JBoss EAP 6.3.0. Another option is to set it via an external property file. By default it uses the application.properties, however you can easily override it viaspring.config.name as shown below.

java -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war --spring.config.name=backbase

view rawbackbase.properties hosted with ❤ by GitHub

You can group configuration in profiles. With Spring profiles you can achieve that you have one deployable artifact across development, staging and live environments, running on your laptop, application server or on a PaaS provider. Achieving this makes testing very easy. Lastly I would like to show you how Spring Boot can help in setting up logging configuration. Spring Boot already provides a default base configuration for each logging implementation that you can include if you just want to set levels. The base configuration is set up with console output and file output (rotating, 10 Mb file size) which is usually enough.

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<include resource="org/springframework/boot/logging/logback/base.xml"/>

<logger name="com.backbase" level="INFO"/>

</configuration>

view rawlogging.xml hosted with ❤ by GitHub

With the logging.file you can configure the file output location. What you would however mostly do is to setup an externalised logging configuration. For logging I recommend logback. It can automatically reload logging configuration upon modification. The external logging configuration you can set via the logging.config property.

java -jar target/configuration-with-spring-boot-0.0.1-SNAPSHOT.war --logging.config=backbase-logback.xml

view rawbackbase-external-logback.sh hosted with ❤ by GitHub

You should also customise the banner for your app :) I used this tool. Hope you see the great flexibility regarding configuration when using Spring Boot. There are other goodies like using YAML instead of properties files, which is a very convenient format for specifying hierarchical configuration data.


 
 
 

Comments


© 2023 by BI World. Proudly created with Wix.com

  • Facebook Basic Black
  • Twitter Basic Black
  • YouTube Basic Black
bottom of page