EaterOfFromage
EaterOfFromage

Reputation: 319

Getting a SpringBoot property in a non-Spring class

I've got a number of properties defined in my application.properties file. These are loaded into a number of different configuration files across the system via @Configuration, @PropertySource, and @ConfigurationProperties annotations.

In addition, I have a library, separate from this system, that has no dependence on Spring (and ideally will stay that way). At some point in the execution of the system, it initializes an instance of a class from this library via reflection and a no args constructor. However, in that initialization, I want to get a Spring property and assign it to a local field - however, as I mentioned before, this library class is not spring configured, and is in fact in an entirely different project. How can this be done?

My current solution is that when the property is initialized in the config class, the setter for the property also sets a system property (via System.setProperty("someProp", propValue)), and then in the no args constructor of the library class I call System.getProperty("someProp"). However, this feels really hacky, particularly the part where I set the variable. Perhaps there is some way to configure SpringBoot to automatically propagate that particular property up to become a System property as well?

My code atm

ServiceConfig.class

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "service")
public class ServiceConfig {

  private String serviceUrl;

  public String getServiceUrl() {
    return serviceUrl;
  }

  public void setServiceUrl(String serviceUrl) {
    this.serviceUrl = serviceUrl;
    System.setProperty("SERVICE_URL", serviceUrl);
  }
}

My application.properties

service.serviceUrl=http://localhost:8000

LibraryClass.class

public class LibraryClass {
    private final String serviceUrl;

    public LibraryClass() {
        this.serviceUrl = getProperty("OAUTH_SERVICE_URL");
    }

    ...
}

Upvotes: 1

Views: 1300

Answers (1)

Tommy Brettschneider
Tommy Brettschneider

Reputation: 1490

if your LibraryClass has a setter method - in the example below named setServiceUrl(...) - to set this configuration property you could add this to your existing ServiceConfig configuration class:

@Bean
public LibraryClass getLibraryClass(@Value(${"OAUTH_SERVICE_URL"}) String serviceUrl) {
    LibraryClass libraryClass = new LibraryClass();
    libraryClass.setServiceUrl(serviceUrl);
    return libraryClass;
}

Other than that - if you cannot modify LibraryClass because it's parts of a 3rd party library or so... you could use Spring's Environment instance, to read all needed properties that - later - will be accessed inside of the constructor of LibraryClass and set them just the way you did as System properties. Also add this to your config class:

@Autowired
public void setSystemPropsNeededForLibraryClassConstruction(Environment environment) {
    System.setProperty("serviceUrl", environent.getProperty("serviceUrl"));
}

Upvotes: 2

Related Questions