Spring Boot — @ConfigurationProperties | Code Factory

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);
}
}
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 + "]";
}
}
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 + "]";
}
}
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";
}
}
pool-size=5
name=code-factory
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/
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/]]

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Let’s build your personal SEMRush with Elixir!

Confidence vs. Competence

Skip to main contentSkip to toolbar

How to Share Gallery VM images across Azure Tenants

Elastic App Search in a Nutshell

Installing Multiple Alternative Versions of PHP on Ubuntu

GitHub Actions: Passing Boolean input variables to reusable workflow_call

[Part 2] The Best Alternatives to Expensive Bootcamps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Code quality and analysis with SonarQube — Part I

How to: Log4J2 and Graylog in a Spring Boot application

EXPLORING UNIT TESTING WITH JUNIT TEST FRAMEWORK

Spring Boot: Introduction Part — 1