The Magical Use of @Profile in Spring

       Recently, a scheduled task needs to be developed in the project. The spring cloud microservice framework has been implemented in the project. The schedule task server is deployed as an independent service, but the business layer logic code is also included in a module of the main service, which starts too many during the development phase. Service and development debugging are inconvenient, so I thought about integrating the scheduled task into the main service to facilitate debugging (similar to Unit Test), but in the production environment, it is necessary to ensure that this scheduled task will not be started in the main service, at this time @Profile is shining debut.

 

@Profile("dev")
@Component
public class TestTask {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Scheduled(cron = "*/10 * * * * ?")
    public void testJob() {
        log.debug("****************start*****************");
        // do something......
        log.debug("*****************end *****************");
    }
}

  

 

       Friends who have used SpringBoot should know that spring boot can automatically select different profile environment configurations for us at runtime, for example, the dev environment, test test environment, and prd production environment have different database usernames, passwords, etc., spring boot When the service starts, the corresponding configuration will be selected according to the profile we specify, saving us the steps of manually changing or executing script changes. Here @Profile({"dev") means that when the specified profile=dev, the @Component annotation will take effect, the TestTask object will be registered in Spring's bean factory, and the scheduled task will be executed. On the contrary, if If the specified profile is not dev, the @Component annotation will be invalid, and the registration action will not occur.

 

Next look at the definition of @Profile:

 

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({ProfileCondition.class})
public @interface Profile {
    String[] value();
}

 

    Friends who understand annotations will notice that String[] value(); value is an array? Yes, @Profile can specify profiles of multiple environments. For example,  @Profile({"dev", "test"})表示此任务将会在profile为dev或test环境中被执行。另外,value支持取反 "!" 运算符操作,@Profile("!dev") means that the code will take effect in environments where the profile is not dev.

 

    I believe that readers have been able to experience the magic of @Profile. Finally, I would like to remind everyone that when multiple profiles are specified, the "or" condition between each profile takes effect, for example @Profile({"dev", " !test"}) means that the code will take effect when profile=dev or profile!=test. In addition, the AND operation between multiple profiles is not currently supported. Friends who have doubts about this can google the "AndProfilesCondition" class, many people are concerned about " "Multiple profiles are not supported and operated" is a problem, and other netizens can also see the code implementation of this class on Stack Overflow.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107617&siteId=291194637