Spring boot

Spring boot

 

spring-boot-starter-parent

A special starter that provides useful maven defaults. It also provides a dependency-management section so that you can omit version tags for common dependencies. Spring-boot-starter-parent includes a few of maven goal such as repackage and run goal to start application:

mvn spring-boot:run

Other features can be found from here.

 

 

Overview Spring Boot

  • Automatic configuration
  • Starter dependencies
  • Command line interface
  • The actuator

 

Configuration

Spring boot read the properties based on the sequence which overwrite from top down sequence. Eg, default properties can get overwritten by any 1-7 options.

  1. Command line argues
  2. JNDI lookup
  3. JVM system properties
  4. OS system environment variable
  5. external application.properties (subdir /config or same level path as application)
  6. internal application.properties (not the default path, package named “config” or root classpath)
  7. Property sources specific by @PropertySource
  8. default properties

 

Profile and properties file

application.properties file are serve as default properties file. If you’d like to have properties file based on different profile setting. Let’s say production, staging, development. All you need to do is create the properties file with this pattern. application-{profile}.properties. And activate the profile during your application startup.

 

@ConfigurationProperties

With annotated @ConfigurationProperties, your bean class it has injected a value (setter injection) with prefixed define by your choice, Let’s say ‘hello‘. It similar like using @Value inject from properties files.

1) First you’ve need to add the dependencies.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

2) Example, in controller annotated @ConfigurationProperties. We can specific the location file with attribute as well.
(more practically, use it as a component class and inject to your controller)

@Controller
@RequestMapping("/hello")
@ConfigurationProperties(prefix="hello")
public class HelloWorldController {

  private String world;
	
  public void setWorld(String world) {
     this.world = world;
  }
	
  @RequestMapping(method = RequestMethod.GET)
  public String index(Model model) {
    model.addAttribute("world", world);
    return "helloworld";
  }
}

3 Properties file

hello.world=Hello World

4. Your html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Reading List</title>
<link rel="stylesheet" th:href="@{/css/style.css}"></link>
</head>
<body>

  <h2>Hello World</h2>
  <p th:text="${world}">Text to be replace</p>
	
</body>
</html>

 

Web Resource Version Random Hash

# Appended random hash string behind the resources files.
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

resourcehash

Web Resource Version Append

spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/**
spring.resources.chain.strategy.fixed.version=v1

resourceversion

Web Resource Compression GZIP

server.compression.enabled=true
server.compression.min-response-size=0

 

Web Context Path

From the application.properties modified the follow properties. After that you’ll able access with http://web:8080/app

server.address=web
server.port=8081
server.context-path=/app

 

Actuator

Actuator is a feature provided by spring boot to let you inspect in your spring boot application. Actuator provide service such as monitoring and metric to the application. There are several way to gain the data either through the remote endpoint with REST, remote shell and JMX.

 

GET /autoconfig

A report describing about autoconfig condition either pass or failed.

autoconfig

GET /configprops

A report to show how exactly a bean injected with configuration properties

configprops

Get /beans

A report to show all the beans in relationship

beans

Get /dump

Snapshoot dump of thread activity

dump

Get /health

An application health check. It can be configured from application.properties with showing details or without.

endpoints.health.sensitive=[true/false]

healthcheck

Health Indicator

We can create custom health check by implement HealthIndicator interface. It will output to actuator.

public class WebserviceHealIndicator implements HealthIndicator 
{
  public Health health() {
    boolean good = true;
    //implement logic ping your remote
    if (true) {
      return Health.up().withDetail("Webservice up", "Connection alive").build();
    } else {
      return Health.down().withDetail("Webservice down","Connection lost").build();
    }
  }
}

 

Actuator Info

By default, actuator expose nothing on the info. But you can provide the information with the following properties. Alternative, by using @[value]@ to refer the properties set from maven pom.xml

info.appname=Hello
info.framework=Springboot
info.artifact=@project.artifactId@
info.name=@project.name@
info.java.version=@java.version@

 

Output:

{"appname":"Hello","framework":"Springboot","name":"s-secure","artifact":"webapp","java":{"version":"1.8.0_20"}}

 

 

And more others like 

  • Get /env or /env/{name}
  • Get /info
  • Get /mappings
  • Get /metrics or /metrics/{name}
  • Get /shutdown
  • Get /trace

 

Interface upon startup

  • CommandLineRunner
  • ApplicationRunner

 

Create Filter

  • Create filter and annotated with @Component or @WebFilter
  • Annotation with @ServletComponentScan.

Customize actuator path

From the application.properties, modified the value. http://localhost:8080/privat/aconfig

managment.context-path=/private
endpoints.autoconfig.path=/aconfig

 

 

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.