I'd like to introduce some methods that are only executed during development.
I thought I might use Spring @Profile
annotation here? But how can I apply this annotation on class level, so that this method is only invoked if the specific profile is configured in properties?
spring.profiles.active=dev
Take the following as pseudocode. How can this be done?
class MyService { void run() { log(); } @Profile("dev") void log() { //only during dev } }
Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.
Spring @Profile allow developers to register beans by condition. For example, register beans based on what operating system (Windows, *nix) your application is running, or load a database properties file based on the application running in development, test, staging or production environment.
For future readers who don't want to have multiple @Beans
annotated with @Profile
, this could also be a solution:
class MyService { @Autowired Environment env; void run() { if (Arrays.asList(env.getActiveProfiles()).contains("dev")) { log(); } } void log() { //only during dev } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With