Spring Boot – @ConfigurationProperties | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Application.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

AppProperties.java

package com.example.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:app.properties")
public class AppProperties {

	@Value("${pool-size}")
	private int poolSize;

	@Value("${name}")
	private String name;

	// getters and setters

	@Override
	public String toString() {
		return "AppProperties [poolSize=" + poolSize + ", name=" + name + "]";
	}

}

ApplicationProperties.java

package com.example.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("app") // prefix app, find app.* values
public class ApplicationProperties {

	private String error;
	private List<Menu> menus = new ArrayList<>();
	private Compiler compiler = new Compiler();

	public static class Menu {
		private String name;
		private String path;
		private String title;

		// getters and setters

		@Override
		public String toString() {
			return "Menu [name=" + name + ", path=" + path + ", title=" + title + "]";
		}

	}

	public static class Compiler {
		private String timeout;
		private String outputFolder;

		// getters and setters

		@Override
		public String toString() {
			return "Compiler [timeout=" + timeout + ", outputFolder=" + outputFolder + "]";
		}

	}

	// getters and setters

	@Override
	public String toString() {
		return "ApplicationProperties [error=" + error + ", menus=" + menus + ", compiler=" + compiler + "]";
	}

}

TestController.java

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.AppProperties;
import com.example.ApplicationProperties;

@RestController
public class TestController {

	@Autowired
	private AppProperties appProperties;
	
	@Autowired
	private ApplicationProperties applicationProperties;
	
	@GetMapping("/hello")
	@ResponseBody
	public String hello() {
		System.out.println(appProperties);
		System.out.println(applicationProperties);
		return "hello";
	}
}

app.properties

pool-size=5
name=code-factory

application.properties

app.menus[0].title=First
app.menus[0].name=First
app.menus[0].path=/
app.menus[1].title=Second
app.menus[1].name=Second
app.menus[1].path=/Second

app.compiler.timeout=5
app.compiler.output-folder=/temp/

app.error=/error/

Hit http://localhost:8080/hello and check console…

AppProperties [poolSize=5, name=code-factory]
ApplicationProperties [error=/error/, menus=[Menu [name=First, path=/, title=First], Menu [name=Second, path=/Second, title=Second]], compiler=Compiler [timeout=5, outputFolder=/temp/]]

Leave a comment