Spring two different application contexts - collision with property placeholder

I created the SDK using the Spring framework, which will be used to integrate with the REST server to use dependency injection.

In this SDK I have MapPropertySourcesto process PropertyPlaceHolders. Basically, I programmatically register some properties there that I want to solve in the annotation SDK @Value.

It works fine in the SDK, but when I create the SDK (using the builder) inside the application, Spring-bootproperties from are MapPropertiesPlaceHolder no longer resolved.

I have this piece of code from the builder class:

    public MyRepository build() {


    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext();

    StandardEnvironment env = new StandardEnvironment();
    context.setEnvironment(env);


    Map<String, Object> propertiesMap = new HashMap<>();
    propertiesMap.put("baseUrl", baseUrl);

    MutablePropertySources mutablePropertySources = context.getEnvironment().getPropertySources();
    mutablePropertySources.addFirst(new MapPropertySource("customPropertiesMap", propertiesMap));


    if(jerseyClient == null){
        jerseyClient = JerseyClientBuilder.createClient();
    }

    context.getBeanFactory().registerSingleton("jerseyClient", jerseyClient);

    context.setParent(null);
    context.register(MySdk.class);
    context.refresh();

    MySdk mySdk = new MySSdk(context);

    return mySdk;
}

So I create an SDK, and I create a new Spring context inside it.

, MapPropertySource , SDK maven Spring-boot. - ? ... ?

, @Value('${baseUrl}), SDK, SDK Spring-boot, . ?

Edit:

MySdk :

@ComponentScan
@Service
@PropertySource("classpath:application.properties")
public class DeviceRepository {

    private ApplicationContext context;

    public MySdk(){
    }

    public MySdk(ApplicationContext context) {
        this.context = context;
    }
    // other methods that calls beans from context like
    // this.context.getBean(MyBean.class).doSomething()

SDK. baseUrl , SDK Spring, , , @Value.

+4
2

PropertySourcesPlaceholderConfigurer bean Spring? , @Value, , .

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

Javadoc:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html

+1
0

All Articles