MissBonbon
MissBonbon

Reputation: 161

Spring Boot Custom Properties - How to include externalize properties when class is not in the application context

It is hard to understand but for my application a required format. I have some custom libraries which are included at runtime and so they are not in the spring application context. To get apis from spring boot application I catched required apis and overhand this to my external classes.

To show an example:

HashValueService hashValueService 
   = (HashValueService) appContext.getBean("hashValueServiceImpl");

ServiceList srvList = new ServiceList();
    srvList.setHashValueService(hashValueService);

In this way I'm able to get access to my database, which is in my application context.

I have a lot of properties distributed in the whole application. So I want to use the default application.properties to centralized often used properties in my application, like the keystore.

For that I edited application.properties with this line:

application.keystore=server.jks

But of course the usage of the Spring's @Value does show me a null for that attribute, because this class is not in my application context:

@Value("${application.keystore}")
private String keystore;

Do you have an idea to overhand this properties to this customer libraries? Maybe the creation of a new property file whould help? Thank u a lot.

Upvotes: 1

Views: 970

Answers (1)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12122

Majority of Spring magic is done by BeanPostProcessors. Take a good look at them - link.

@Value wiring (and much more) is performed by AutowiredAnnotationBeanPostProcessor, you can use it for your purpose:

AutowiredAnnotationBeanPostProcessor beanPostProcessor = 
          appContext.getBean(AutowiredAnnotationBeanPostProcessor.class);
ServiceList srvList = new ServiceList();

beanPostProcessor.processInjection(srvList);

After that, your ServiceList should have String keystore field initialized.

Upvotes: 1

Related Questions