Home » Java » java – Need properties in Configuration class-Exceptionshub

java – Need properties in Configuration class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have spring boot application and application.properties file in resources folder.
I need some properties in a configuration class. I cant use getClass().getClassLoader().getResourceAsStream() because null pointer. How do I get the properties in configuration class?
Property class:

@Configuration
@PropertySource("classpath:application.properties")
public class AppProperties {
    @Value("${server.ssl.key-store}")
    private String serverSSLKeyStore;

    @Value("${server.ssl.trust-store}")
    private String serverSSLTrustStore;

    @Value("${server.ssl.key-store-password}")
    private String serverSSLKeyStorePwd;

    @Value("${server.ssl.trust-store-password}")
    private String serverSSLTrustStorePwd;

    @Value("${rabo.api.url}")
    private String raboApiUrl;

    @Value("${tpp.certificate}")
    private String TPPCertificate;

    @Value("${rabo.client.id}")
    private String raboClientId;

    @Value("${rabo.key.id}")
    private String raboKeyId;

    @Value("${rabo.private.key}")
    private String raboPrivateKey;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("application.properties"));
        return propertySourcesPlaceholderConfigurer;

    }
// Getters

}

Configuration class:

@Configuration
public class RetrofitConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(RetrofitConfiguration.class);

    @Bean
    SSLTrustManagerHelper provideSSLTrustManagerHelper(AppProperties properties) {
        try {
            LOG.error(properties.getServerSSLKeyStore()); // PRINTS NULL
            InputStream keystore = new FileInputStream(properties.getServerSSLKeyStore());
            InputStream truststore = new FileInputStream(properties.getServerSSLTrustStore());
            String keystorePwd = properties.getServerSSLKeyStorePwd();
            String truststorePwd = properties.getServerSSLTrustStorePwd();
            SSLTrustManagerHelper ssl = null;
            try {
                ssl = new SSLTrustManagerHelper(keystore, keystorePwd, truststore, truststorePwd);
            } catch (Exception e) {
                LOG.error("error occured while creating ssl trust manager helper!", e);
            }
            return ssl;
        } catch (FileNotFoundException e) {
            LOG.error("file not found ", e);
            return null;
        }
    }

// Other config
}

The AppProperties class is created but properties return null.

How to&Answers:

What you want to do is setup a @ConfigurationProperties rather than a property source. This feature of Spring Boot allows you to read properties into a POJO that can be used elsewhere in code.

So the AppProperties would be something like:

@Configuration
@ConfigurationProperties
class AppProperties {
   private String serverSSLKeyStore;
   // the other properties

   public void setServerSslKeyStore(String value) {
     this.serverSSLKeyStore = value;
   }

   public String getServerSslKeyStore() {
     return this.serverSSLKeyStore;
   }
}

Every field must have a corresponding property with the same name in the application.properties file. Note that each field should have getter and setter methods for this to work.

You can then initialize it in your Spring boot application using:

@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class) // not needed in later spring version
class MyApplication {
   public static void main(String[] args) {
      SpringApplication.run(MyApplication.class, args);
   }
}

Answer:

There are few changes can be done

  1. Remove bean propertyConfigInDev
  2. If there is not overriden path (of property file)
    defined then you do not require
    @PropertySource("classpath:application.properties"), you may skip this line of code as well
  3. You may use @Value directly in RetrofitConfiguration class as shown below

@Configuration
public class RetrofitConfiguration {
   private static final Logger LOG = LoggerFactory.getLogger(RetrofitConfiguration.class);

@Value("${server.ssl.trust-store}")
private String serverSSLTrustStore;

@Bean
SSLTrustManagerHelper provideSSLTrustManagerHelper() {
   LOG.error(serverSSLTrustStore); // **SHOULDN'T PRINTS NULL**
   }
}