简体   繁体   中英

Spring Boot EnvironmentAware not called

I have written a Spring Boot Application where several classes are implementing the EnvironmentAware interface.

The EnvironmentAware.setEnvironment() method is being called on the ApplicationConfiguration class upon loading, but it IS NOT being called when it is implemented in the other classes. (I wrote another app where this works and have configured it the same way I have this app.)

The other class where I have implemented the EnvironmentAware interface looks like:

@Component("merchandisingMasterDataItemsProxy")
@Scope("singleton")
@Configurable
public class MerchandisingMasterDataItemsProxy extends BaseProxy implements EnvironmentAware {

private HttpHeaders httpHeaders;
private String base_url;

@Override
// THIS NEVER GETS CALLED
public void setEnvironment(Environment environment) {
    this.environment = environment;
}

@Autowired
public MerchandisingMasterDataItemsProxy(RestTemplate restTemplateMerchandisingItems) {

    super(restTemplateMerchandisingItems);

    // ENVIRONMENT IS NULL HERE :(
    this.base_url = environment.getProperty(BaseConfig.VCAP_ENVIRONMENT_BASE + "merchandising.items.base_url");

    httpHeaders = new HttpHeaders();
    httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);

}

Is there any special annotation or something I need to do to get this to actually implement the EnvironmentAware interface?

One often gets so wrapped up in "configuration" issues that a blinding glimpse of the obvious escapes one.

M. Deinum gave me that obvious answer. In my code, I am attempting to access a variable in the Constructor that will be set later. When I moved the initialization of those variables into another method, the "setEnvironment()" method was called before I used the environment variable.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM