1

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.

MissBonbon
  • 141
  • 1
  • 4
  • 16

1 Answers1

1

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.

Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67
  • 1
    This is it! Thanks for that! You made me happy! – MissBonbon Dec 02 '16 at 10:55
  • What is with properties I want to use in the main-method? @Value returns me null and the AutowiredAnnotationBeanPostProcessor can't be used at this place. I'm using SpringApplicationBuilder to create two apis for incomming requests. – MissBonbon Dec 02 '16 at 13:08
  • @MissBonbon do you mean public static void main(String[]) method? – Maciej Dobrowolski Dec 02 '16 at 13:17
  • yes, this is what i mean. Found this question on stack: [link](http://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) - But this not work for me in the first try. – MissBonbon Dec 02 '16 at 13:17
  • @MissBonbon `main` is a static method. Properties can be injected into spring components (beans) which are instances of classes (not static) - don't go that way :) – Maciej Dobrowolski Dec 02 '16 at 13:21
  • What should i rather do? Have u got a link to an good example? – MissBonbon Dec 02 '16 at 13:37
  • @MissBonbon To be honest - I am not sure what you are trying to achieve, and comments are probably not the best place to talk since it's difficult to include code here. I recommend you creating a new question where you can explain the problem (and expectations) more deeply. Feel free to share a link to the question here. – Maciej Dobrowolski Dec 02 '16 at 13:41
  • Hopped you will have an idea. You are right! This need a deeper explanation. Thank for your time to answer. – MissBonbon Dec 05 '16 at 09:53