JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Spring Boot @ConfigurationProperties Property Validation


In the previous page, we have seen @ConfigurationProperties example. The @ConfigurationProperties support JSR-303 bean validation – javax.validation

Here is a simple property file "application.properties":

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

Now lets introduce some validations to our instance members:

package com.java2novice.springboot.util;

import java.util.List;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

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

	@NotEmpty
	private String resourceUrl;

	private List<Integer> resourcePort;

	@Max(5)
    @Min(0)
	private Integer resourceCount;

	@Override
	public String toString() {

		return "resourceUrl: "+ this.resourceUrl+"\n"
				+ "resourcePort: "+this.resourcePort+"\n"
				+ "resourceCount: "+this.resourceCount+"\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;
	}

	public Integer getResourceCount() {
		return resourceCount;
	}

	public void setResourceCount(Integer resourceCount) {
		this.resourceCount = resourceCount;
	}
}

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

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

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target resourceUrl: http://java2novice.com
resourcePort: [80, 443]
resourceCount: 10
 failed:

    Property: cmdb.resourceCount
    Value: 10
    Reason: must be less than or equal to 5


Action:

Update your application's configuration
<< 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
  22. How to load external property files into Spring Boot application?
  23. How to rename application.properties file in Spring Boot application?
  24. How to configure multiple DataSources (Databases) with Spring Boot and Spring Data?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

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].

Most Visited Pages

Other Interesting Sites

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