当前位置:   article > 正文

【spring】@Profile注解学习

【spring】@Profile注解学习

@Profile介绍

在Spring框架中,@Profile注解用于根据特定的配置文件来有条件地激活或禁用Bean的定义。这在开发和测试过程中非常有用,因为它允许你为不同的环境(如开发、测试、生产)定义不同的配置。

@Profile不仅可以标注在方法上,也可以标注在配置类上。如果标注在配置类上,只有在指定的环境时,整个配置类里面的所有配置才会生效。如果一个Bean上没有使用@Profile注解进行标注,那么这个Bean在任何环境下都会被注册到IOC容器中。

@Profile源码

  1. @Target({ElementType.TYPE, ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Conditional(ProfileCondition.class)
  5. public @interface Profile {
  6. String[] value();
  7. }
源代码截图

@Profile属性介绍

  • value:指定环境的标识。

@Profile注解使用场景

1. 多环境配置

在实际开发过程中,通常会有开发、测试和生产等多个不同的运行环境。每个环境都有其特定的配置需求,如数据库连接信息、服务器地址等。通过使用@Profile注解,可以为不同的环境定义专属的配置类,从而实现多环境的灵活切换和管理。

2. 条件化Bean创建

有时候,某些Bean可能只在特定的条件下需要被创建。例如,你可能需要在测试环境中创建一些用于测试的辅助Bean,而在生产环境中则不需要。利用@Profile注解,可以仅在满足特定条件时创建这些Bean。

3. 功能开关

在某些情况下,可能需要根据环境或条件启用或禁用某些功能。通过@Profile注解,可以在配置类或者Bean定义的方法上设置条件,从而实现功能的动态开关。

4. 环境特定的安全性配置

安全性配置往往根据环境的不同而有所差异。例如,在开发环境中,可能需要关闭某些安全检查以便于调试;而在生产环境中,则需要开启所有的安全措施。使用@Profile注解可以对安全性相关的Bean进行条件化配置。

5. 集成测试环境配置

在进行集成测试时,可能需要一些专门为测试设计的配置,如模拟的外部服务、测试数据库等。通过为测试环境定义特定的Profile,并在其中包含相关的测试配置和Bean,可以方便地进行集成测试。

6. 动态配置

在运行时,可能需要根据外部条件(如系统属性、环境变量、命令行参数等)动态地改变应用程序的行为。@Profile注解可以结合这些动态输入来激活或禁用特定的配置。

7. 灰度发布

在进行灰度发布时,可能需要为一部分用户或服务器启用新的功能或配置,而其他用户或服务器则继续使用旧的配置。通过定义不同的Profile并根据用户或服务器的特征来激活相应的Profile,可以实现灰度发布的配置管理。

8. 性能监控与日志配置

在开发和测试环境中,通常需要开启详细的日志记录和性能监控,以便于发现和调试问题。而在生产环境中,则可能只需要记录关键的错误信息和性能指标。通过@Profile注解,可以根据环境来配置不同的日志级别和性能监控策略。

@Profile测试示例代码

示例代码 一
ProfileDemo类
  1. package com.yang.SpringTest.annotation.profileLearn;
  2. import lombok.Data;
  3. import lombok.extern.slf4j.Slf4j;
  4. /**
  5. * <p>ProfileDemo类</p>
  6. *
  7. * @author By: chengxuyuanshitang
  8. * Package com.yang.SpringTest.annotation.profileLearn
  9. * Ceate Time 2024-04-09 17:37
  10. */
  11. @Data
  12. @Slf4j
  13. public class ProfileDemo {
  14. private String environment;
  15. public ProfileDemo (String environment) {
  16. this.environment = environment;
  17. }
  18. }
ProfileDemoConfig配置类
  1. package com.yang.SpringTest.annotation.profileLearn;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Profile;
  6. /**
  7. * <p>ProfileDemoConfig配置类</p>
  8. *
  9. * @author By: chengxuyuanshitang
  10. * Package com.yang.SpringTest.annotation.profileLearn
  11. * Ceate Time 2024-04-09 17:38
  12. */
  13. //@Profile("development")
  14. @Configuration
  15. @ComponentScan(value = {"com.yang.SpringTest.annotation.profileLearn"})
  16. public class ProfileDemoConfig {
  17. @Profile("development")
  18. @Bean("profileBeanDev")
  19. public ProfileDemo profileBeanDev(){
  20. return new ProfileDemo("开发环境");
  21. }
  22. @Profile("test")
  23. @Bean("profileBeanTest")
  24. public ProfileDemo profileBeanTest(){
  25. return new ProfileDemo("测试环境");
  26. }
  27. @Profile("production")
  28. @Bean("profileBeanProd")
  29. public ProfileDemo profileBeanProd(){
  30. return new ProfileDemo("生产环境");
  31. }
  32. }
ProfileDemoTest测试类
  1. package com.yang.SpringTest.annotation.profileLearn;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. /**
  5. * <p>ProfileDemoTest测试类</p>
  6. *
  7. * @author By: chengxuyuanshitang
  8. * Package com.yang.SpringTest.annotation.profileLearn
  9. * Ceate Time 2024-04-09 17:40
  10. */
  11. @Slf4j
  12. public class ProfileDemoTest {
  13. public static void main (String[] args) {
  14. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
  15. context.getEnvironment ().setActiveProfiles ("test");
  16. context.register (ProfileDemoConfig.class);
  17. context.refresh ();
  18. ProfileDemo demo = context.getBean (ProfileDemo.class);
  19. log.info ("ProfileDemo is : [ {} ]", demo);
  20. }
  21. }
运行结果

示例代码 二

上面一样,在ProfileDemoConfig配置类填写@Profile("development")

ProfileDemoConfig配置类
  1. package com.yang.SpringTest.annotation.profileLearn;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Profile;
  6. /**
  7. * <p>ProfileDemoConfig配置类</p>
  8. *
  9. * @author By: chengxuyuanshitang
  10. * Package com.yang.SpringTest.annotation.profileLearn
  11. * Ceate Time 2024-04-09 17:38
  12. */
  13. @Profile("development")
  14. @Configuration
  15. @ComponentScan(value = {"com.yang.SpringTest.annotation.profileLearn"})
  16. public class ProfileDemoConfig {
  17. @Profile("development")
  18. @Bean("profileBeanDev")
  19. public ProfileDemo profileBeanDev(){
  20. return new ProfileDemo("开发环境");
  21. }
  22. @Profile("test")
  23. @Bean("profileBeanTest")
  24. public ProfileDemo profileBeanTest(){
  25. return new ProfileDemo("测试环境");
  26. }
  27. @Profile("production")
  28. @Bean("profileBeanProd")
  29. public ProfileDemo profileBeanProd(){
  30. return new ProfileDemo("生产环境");
  31. }
  32. }
运行结果

出现错误No qualifying bean of type 'com.yang.SpringTest.annotation.profileLearn.ProfileDemo' available

修改ProfileDemoTest测试类

把setActiveProfiles ("test")

  1. package com.yang.SpringTest.annotation.profileLearn;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. /**
  5. * <p>ProfileDemoTest测试类</p>
  6. *
  7. * @author By: chengxuyuanshitang
  8. * Package com.yang.SpringTest.annotation.profileLearn
  9. * Ceate Time 2024-04-09 17:40
  10. */
  11. @Slf4j
  12. public class ProfileDemoTest {
  13. public static void main (String[] args) {
  14. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
  15. context.getEnvironment ().setActiveProfiles ("development");
  16. context.register (ProfileDemoConfig.class);
  17. context.refresh ();
  18. ProfileDemo demo = context.getBean (ProfileDemo.class);
  19. log.info ("ProfileDemo is : [ {} ]", demo);
  20. }
  21. }
运行结果

当@Profile注解标注到类上时,虽然类中的方法上也标注了@Profile注解,但是整体上会以类上标注的@Profile注解为准。如果设置的环境标识与类上标注的@Profile注解中的环境标识不匹配,则整个类中的配置都不会生效。否则,类中没有使用@Profile注解标识的Bean和环境标识与方法上使用@Profile注解指定的环境标识匹配的Bean才会生效。




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/427588
推荐阅读
相关标签
  

闽ICP备14008679号