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

The One-Shot Operation with coroutines(the power of livedataBuilder)

How to install the latest stable Nginx version on CentOS 8

Explaining How to Set Up an Apache Spark Cluster to a Five Year Old

Key Takeaways from Continuous Discussions(#c9d9) Takeaways Episode 90: Gene Kim and DOES’18…

Why Functional Programmers Prefer For-Comprehension Over Imperative Code Blocks

It Is Not Too Late To Upgrade To Rails 4

Multipass + Kubernetes + Calico

When you can’t dodge it(or shoot it )Ram it

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

Spring Boot Initializr

Spring Events at VISO

Spring boot error handling with Rollbar

Spring vs Spring Boot