Customize Spring Boot's auto configurations

In this tutorial, we will customize the spring boot's auto configurations. This can be done in following ways:
  • Through program arguments
  • application.properties
  • application.yml
  • YAML is a serialization format that follows the hierarchical structure, which is in ways similar to JSON. It will contain key value pairs in a hierarchical format. And it is really popular in ruby. Spring dependencies have snake yaml jar which is responsible for parsing yaml.
Spring boot gives developers choices, often there are multiple methods to achieve the same thing in spring boot. This allows developers to pick their favorite approach.

Find this project on Github

https://github.com/javacodenet/springBootApplicationPropertiesDemo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodenet</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

Application.java

package com.javacodenet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
public String homePage(){
return "Welcome to home page";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Through program arguments

application.properties

server.port=8585
server.context-path=/hello
page: home page
title: Welcome to ${page}

application.yml

server:
port: 8585
context-path: /hello
page: home page
title: Welcome to ${page}
view raw application.yml hosted with ❤ by GitHub

Output







Comments

Popular posts from this blog

Generate PDF From XML Or Java Object Using Apache FOP

Generate Random values in Spring boot

Generate/Read an Excel file in Java using Apache POI