Spring Configuration Beans

Spring configuration

    <!-- lookup parent from repository -->


The following example is the sample class

@ConfigurationProperties(prefix = "file")
public class FileStorageProperties {
   private String authMethod;


The application.property which resides in the classpath will be read to identify the configuration properties for the above property.


This property will be identified as an autowire candidate.


This will remark the configuration property and prefix will identify the appropriate properties for the class.


Following any form will assign the above authMethod variable.



Now let’s say we have a hierarchy of properties as follows.

#Object properties
The following configuration class can be organized as follows to retrieve the above configurations.
public class ConfigProperties {

    public static class Credentials {
        private String authMethod;
        private String username;
        private String password;

       // standard getters and setters


    private String host;


The following type of configuration will map the properties into the list and map.

#List properties

#Map Properties


We can also use validation properties to ensure the properties meets the requirement.

The following are some samples for validation properties.

String empty check

private String host;

String length check

@Length(max = 4, min = 1)
private String authMethod;

Min-max value check for the integer


private int port;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.