Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Profiles on method level?

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     } } 
like image 570
membersound Avatar asked Mar 12 '14 10:03

membersound


People also ask

Can you use @component together with profile?

Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.

Which of these are valid usages of spring profiles?

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.


1 Answers

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    } } 
like image 152
Niels Masdorp Avatar answered Sep 25 '22 02:09

Niels Masdorp