0

I have properties like this:

credentials:
   userid: <userid>
   password: <password>

I have a POJO:

@Setter
public class Credentials {
   private String userid;
   private String password;

However, this POJO is in another jar, so I can't add annotations. So I thought I'd try something like this:

@Configuration
@Getter
@Setter
@ConfigurationProperties("credentials")
public class MyCredentials {
   private Credentials credentials = new Credentials();
}

But I can't get my class to load the properties. How can I get it to work in this scenario?

Peter Kronenberg
  • 878
  • 10
  • 32
  • Why do you want to load your credential properties into the Credentials POJO? You dont need the Credentials pojo to access values from your properties files. – Abdullah Khan Feb 01 '22 at 03:39

2 Answers2

-1

Just make a separate configuration bean and access value from that try below code

@Configuration
@Getter
@Setter
@ConfigurationProperties("credentials")
public class MyCredentialSetting{

private String userid;
private String password;

}

Now wherever you want to use just use @Autowired like in controller or service

Here myCredentialSetting has value from propertoes file injected by spring boot automatically

@Autowired
private MyCredentialSetting myCredentialSetting;

String userIdValue=myCredentialSetting.getUserid(); //you will get user id value by this
String password=myCredentialSetting.getPassword();

//Setting value to original pojo for furthur use

private Credentials credentials = new Credentials();
credentials.setUserid(userIdValue);
credentials.setPassword(password);
bhaskarkh
  • 179
  • 2
  • 11
  • 1
    But my point is that the POJO already exists elsewhere, so I don't want to have to re-create it as you've done here with `MyCredentialSetting` – Peter Kronenberg Feb 01 '22 at 14:39
-1

You are mixing things.

@Setter (and @Getter) are most likely lombok project annotations. These annotations at compile time will generate the getX() and setX() methods on a Pojo with a property "x".

If the Credentials POJO is in another jar, it should have a getter and setter (or it is not a POJO). So we don't care about lombok.

On another side you have a @Configuration class where Spring boot will create the different beans of your application.

The class should look something like this:

@Configuration
@ConfigurationProperties("credentials")
public class MyCredentials {

   @Bean("credentials")
   public Credentials credentials(
       @Value("${credentials.userid}") String userid,
       @Value("${credentials.password}") String password) {
         Credentials credentials = new Credentials();
         credentials.setUserid(userid);
         credentials.setPassword(password):
         return credentials;
   }

}

With the @Value annotation Spring boot will inject the properties into the method that will create the bean.

EDIT I

As stated by @M.Deinum, the same can be obtained by:

@Configuration
public class MyCredentials {

   @Bean("credentials")
   @ConfigurationProperties("credentials")
   public Credentials credentials() {
         return new Credentials();
   }
}

@ConfigurationProperties will find the properties prefixed with "credentials" and inject them into the credentials bean.

Thanks for the tip @M.Deinum!

Juan
  • 5,525
  • 2
  • 15
  • 26
  • You can just put the `@ConfigurationProperties` on the `@Bean` method and return just `new Credentials()` from the `@Bean` method. Spring Boot will do the binding afterwards. – M. Deinum Feb 01 '22 at 07:48
  • Not mixing things. The Lombok annotations were just to show that there are Getters and Setters. But you are using the `@Value` annotation. Not sure it makes sense to use that with `@ConfigurationProperties`. `@Value` is for injecting individual properties. `@ConfigurationProperties` is for injecting an entire structure, indicated by the prefix – Peter Kronenberg Feb 01 '22 at 14:42
  • @PeterKronenberg The '@Getter' and '@Setter' will just create a getCredentials() and setCredentials(Credentials credentials) methods inside MyCredentials class wihich is a '@Configuration' file (A more clear name would be MyCredentialsConf). The solution I wrote will create a Credentials bean with the values set. Perhaps you want something different which is not clear in the question. – Juan Feb 01 '22 at 16:17
  • You don’t need `ConfigurationProperties` on the class. – Abhijit Sarkar Feb 02 '22 at 00:57
  • Copy paste issue :) – Juan Feb 02 '22 at 00:58