Thursday 26 September 2019

How to use @ConfigurationProperties annotation on @Bean Method?

app:
  datasource:
    userName: krishna
    password: password123

@Bean
@ConfigurationProperties(prefix = "app.datasource")
@Qualifier("myAppDetails")
public UserDetails appDetails() {
 return new UserDetails();
}

Here the UserDetails class has proeprties userName, password, password. so spring boot maps them to the created object.


Example of the UserDetails class:
public class UserDetails {

 private String userName;
 private String password;

}

Find the below working application.


UserDetails.java
package com.sample.app.model;

public class UserDetails {

 private String userName;
 private String password;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public String toString() {
  return "UserDetails [userName=" + userName + ", password=" + password + "]";
 }

}

AppConfig.java
package com.sample.app.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sample.app.model.UserDetails;

@Configuration
public class AppConfig {

 @Bean
 @ConfigurationProperties(prefix = "app.datasource")
 @Qualifier("myAppDetails")
 public UserDetails appDetails() {
  return new UserDetails();
 }

 @Bean
 @ConfigurationProperties(prefix = "hive.datasource")
 @Qualifier("myHiveDetails")
 public UserDetails hiveDetails() {
  return new UserDetails();
 }

}


application.yml
app:
  datasource:
    userName: krishna
    password: password123
    
hive:
  datasource:
    userName: ram
    password: pk123


App.java
package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.model.UserDetails;

@SpringBootApplication
public class App {
 public static void main(String args[]) {
  SpringApplication.run(App.class, args);
 }

 @Autowired
 @Qualifier("myAppDetails")
 private UserDetails appDetails;
 
 @Autowired
 @Qualifier("myHiveDetails")
 private UserDetails hiveDetails;
 
 @Bean
 public CommandLineRunner demo() {
  return (args) -> {
   System.out.println(appDetails);
   System.out.println(hiveDetails);
  };
 }
}


Total project structure looks like below.

Run App.java, you can see below messages in console.
UserDetails [userName=krishna, password=password123]
UserDetails [userName=ram, password=pk123]


You can even separate the camel case letters by – to improve readability.
app:
  datasource:
    user-name: krishna
    password: password123
    
hive:
  datasource:
    user-name: ram
    password: pk123

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment