JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Spring Boot @ConfigurationProperties example


Spring Boot @ConfigurationProperties is allows developer to map the entire property file into an object easily.

In normal spring framework, we use @Value annotation to inject property values one by one as shown below:

@Value("${cmdb.resource-url}")
private int threadPool;

With the use of @ConfigurationProperties, it is going to be much easier. Here is a simple property file "application.properties":

cmdb.resource-url=http://java2novice.com
cmdb.resourcePort[0]=80
cmdb.resourcePort[1]=443

@ConfigurationProperties annotation helps to load a family of related properties, say in this specific case namespaced by the prefix conveniently named "cmdb".

The approach Spring boot takes is to define a bean that can hold all the families of related properties this way:

package com.java2novice.springboot.util;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("cmdb")
public class CmdbProperties {

	private String resourceUrl;
	private List<Integer> resourcePort;

	@Override
	public String toString() {

		return "resourceUrl: "+ this.resourceUrl+"\n"
				+ "resourcePort: "+this.resourcePort+"\n";
	}

	public String getResourceUrl() {
		return resourceUrl;
	}
	public void setResourceUrl(String resourceUrl) {
		this.resourceUrl = resourceUrl;
	}
	public List<Integer> getResourcePort() {
		return resourcePort;
	}
	public void setResourcePort(List<Integer> resourcePort) {
		this.resourcePort = resourcePort;
	}
}

During runtime, all the properties will be injected to the related bean attributes.

Now lets print the properties after application load:

package com.java2novice.springboot;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.java2novice.springboot.util.CmdbProperties;

@SpringBootApplication
public class SpringBootWebApplication {

	private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);

	@Autowired
	private CmdbProperties cmdbProperties;

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

	@PostConstruct
	public void init() {

		logger.info(cmdbProperties.toString());
	}
}

Console Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)
2017-10-20 17:33:46.786  INFO 49754 --- [           main] c.j.springboot.SpringBootWebApplication  : resourceUrl: http://java2novice.com
resourcePort: [80, 443]
                    
<< Previous Program | Next Program >>

Spring-Boot Examples

  1. Spring-Boot initial setup.
  2. Spring-Boot hello world example
  3. What is spring-boot-starter-parent in Spring-Boot pom.xml file?
  4. What is @SpringBootApplication annotation in spring boot?
  5. What is application.properties in spring boot?
  6. What is @ConfigurationProperties annotation in spring boot?
  7. Spring Boot @ConfigurationProperties example
  8. Spring Boot @ConfigurationProperties Property Validation
  9. Difference between @ConfigurationProperties and @Value
  10. Spring boot web application configurations.
  11. How to run spring boot application through command line?
  12. How to run spring boot as a standalone application (non-web)?
  13. Spring boot property resolution order.
  14. Spring Boot – Profile based properties example.
  15. How to configure logback (SLF4J) logging to spring boot applications?
  16. How to update application context path in spring boot?
  17. How to disable spring logo banner in spring boot?
  18. Spring Data JPA with Spring Boot Applications - Oracle - example
  19. Spring Data JPA with Spring Boot Applications - MySql example
  20. How to configure Spring Boot to show Hibernate SQL Query in logs?
  21. Spring Boot – List all Beans loaded in the ApplicationContext
Knowledge Centre
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] or you can comment on the page.

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.