简体   繁体   中英

How to get values from bootstrap.yml file from static instance

  • I have a classes CosmosConnection and SupplierGetResponseFeed
  • I am calling the method of CosmosConnection from SupplierGetResponseFeed
  • The method of SupplierGetResponseFeed from which I am calling CosmosConnection method is static
  • Example : public static SupplierResponseDataEntity prepareSupplierAzureData(Map<String, Object> row, String[] columnNames) {
  • So , When I create the object of CosmosConnection in SupplierGetResponseFeed I can not use @Autowired as a reason I am not able to pick the value from bootstrap.yml file in CosmosConnection
  • Though I create the object using @Autowired in SupplierGetResponseFeed I am not able to pick the values from bootstrap

    @Autowired static CosmosConnection cosmos;

Below is the code for SupplierGetResponseFeed

public class SupplierGetResponseFeed {
static CosmosConnection cosmos= new CosmosConnection(); //creating object 
public static SupplierResponseDataEntity prepareSupplierAzureData(Map<String, Object> row, String[] columnNames) {
//Some code 
cosmos.connectToDB(); //calling the method of CosmosConnection class
} 

The code for is CosmosConnection

@Configuration
@ComponentScan
public class CosmosConnection {
    @Value("${cosmos.connectionuri}") private String uri;
    @Value("${cosmos.primarykey}") private String primarykey;

public String connectToDB() throws DocumentClientException, IOException, ParseException {
    System.out.println("URI is " + uri); //getting this as null

What changes I need to do for picking the values from bootstrap.yml ??

A annotation called PostConstruct in package javax.annotation with spring framework can be used to solve the problem. As described in the source code:

The PostConstruct annotation is used on a method that needs to be executed
 after dependency injection is done to perform any initialization

And code below is a example:

@Configuration
public class ComosConfig {
  @Value("${cosmos.connectionuri}") private String uri;
  @Value("${cosmos.primarykey}") private String primarykey;

  //get and set methods here
}
public class CosmosConnection {
  private String uri;
  private String primaryKey;

  public CosmosConnection(String uri, String primaryKey) {
    this.uri = uri;
    this.primaryKey = primaryKey;
  } 

  public String connectToDB() {
    //do something here
  }
}
@Component
public class SupplierGetResponseFeed {
  private static CosmosConnection cosmos;
  private CosmosConfig config;

  public SupplierGetResponseFeed(CosmosConfig config) {
    this.config = config;
  }

  @PostConstruct
  public void init() {
    String uri = config.getUri();
    String primaryKey = config.getprimaryKey(); 
    cosmos = new cosmos(uri, primaryKey);
  }

  public static SupplierResponseDataEntity prepareSupplierAzureData() {
    cosmos.connectToDB(); //calling the method of CosmosConnection class
  }
} 

After all, it's not recommended in view of code analysis utils to write to static from instance method, so you may need suppress-warning-annotation used with init method to eliminate warnings.

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