I have two methods, implementations differ depending on environment (dev / prod). Is it a good idea to use a variable from application.properties? For example:
production-mode:true
and use it:
@Value("${production-mode}")
private boolean isProd;
...
if (isProd) {
methodForProduction();
} else {
methodForDevelopment();
}
Do you have any suggestions or could you point me in the direction of some useful resources?
Spring boot comes with a profile management system that allow you to active or swap functionnality and behaviour depending on those. Here is a link to the official documentation .
What you could do is create an interface and 2 implementations as such:
public interface MyClass {
void myMethod();
}
public class MyClassForDevelopment implements MyClass {
@Override
public void myMethod() { // your code for developement }
}
public class MyClassForProduction implements MyClass {
@Override
public void myMethod() { // your code for production }
}
And use the configuration of Spring boot as such:
@Configuration
public class MyConfiguration {
@Bean
@Profile("dev")
public MyClass myClassDev() { return new MyClassForDevelopment(); }
@Bean
@Profile("prod")
public MyClass myClassProd() { return new MyClassForProduction(); }
}
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.