I have a situation where it would be useful to read a property from classpath:app.properties
or classpath:presentation-api.properties
for use prior to the spring application being run, in order to set a couple of legacy profiles.
So it would be something like this:
@SpringBootApplication
@PropertySource({"classpath:app.properties", "classpath:various_other.properties"})
public class MainApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
boolean legacyPropertyA = // might be set in classpath:app.properties or classpath:various_other.properties or not at all
boolean legacyPropertyB = // might be set in classpath:app.properties or classpath:various_other.properties or not at all
if (legacyPropertyA) {
builder.profiles("legacyProfileA");
}
if (legacyPropertyB) {
builder.profiles("legacyProfileB");
}
return super.configure(builder);
}
}
What's the cleanest way of retrieving legacyPropertyA
and legacyPropertyB
?
I think you can combine @Value and @PostConstruct annotations in order to have a cleaner solution. Please, take a look in the example below:
@SpringBootApplication
@PropertySource({"classpath:application-one.properties", "classpath:application-two.properties"})
public class MyApplication extends SpringBootServletInitializer {
@Value("${property.one}")
private String propertyOne;
@Value("${property.two}")
private String propertyTwo;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
@PostConstruct
public void initProperties() {
System.out.println(propertyOne);
System.out.println(propertyTwo);
}
}
I hope this can help you. Please, let me know you have any questions.
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.