Spring条件注解@Conditoinal+Profile环境切换应用@Profile
- 手机
- 2025-08-12 17:12:01

Spring条件注解
一、创建一个maven项目
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> </dependencies>二、接口以及实现类
public interface Food { String showName(); } public class Rice implements Food { public String showName() { return"米饭"; } } public class Noodles implements Food { public String showName() { return"面条"; } }三、配置类
两个 Bean 的名字都为 food,这不是巧合,而是有意取的。两个 Bean 的返回值都为其父类对象 Food 每个 Bean 上都多了 @Conditional 注解,当 @Conditional 注解中配置的条件类的 matches 方法返回值为 true 时,对应的 Bean 就会生效。
@Configuration public class JavaConfig{ @Bean("food") @Conditoinal(RiceConditionl.class) Food rice(){ return new Rice(); } @Bean("food") @Conditional(NoodlesCondition.class) Food noddles(){ return new Noddles(); } }四、条件Condition接口的实现类,用来做出判断
在 matches 方法中做条件属性判断,当系统属性中的 people 属性值为 ‘北方人’ 的时候,NoodlesCondition 的条件得到满足,当系统中 people 属性值为 ‘南方人’ 的时候,RiceCondition 的条件得到满足,换句话说,哪个条件得到满足,一会就会创建哪个 Bean
public class NoodlesCondition implements Condition{ @Override public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata){ return context.getEnvironment().getProperty("people").equals("北方人"); } } public class RiceCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("people").equals("南方人"); } }六、测试
public class Main { public static void main(String[] args) { // AnnotationConfigApplicationContext 实例用来加载 Java 配置类 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); //添加一个 property 到 environment 中,添加完成后,再去注册我们的配置类 ctx.getEnvironment().getSystemProperties().put("people", "南方人"); ctx.register(JavaConfig.class); //刷新容器 ctx.refresh(); //从容器中去获取 food 的实例了,这个实例会根据 people 属性的不同,而创建出来不同的 Food 实例 Food food = (Food) ctx.getBean("food"); System.out.println(food.showName()); } } Profile环境切换更改配置类 这次不需要条件注解了,取而代之的是 @Profile
@Configuration public class JavaConfig{ @Bean("food") @Profile("南方人") Food rice(){ return new Rice(); } @Bean("food") @Profile("北方人") Food noddles(){ return new Noddles(); } }测试
public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("南方人"); ctx.register(JavaConfig.class); ctx.refresh(); Food food = (Food) ctx.getBean("food"); System.out.println(food.showName()); } }@Profile注解定义
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(ProfileCondition.class) //条件类 public @interface Profile { String[] value(); } class ProfileCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); if (attrs != null) { for (Object value : attrs.get("value")) { if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) { returntrue; } } returnfalse; } returntrue; } }Spring条件注解@Conditoinal+Profile环境切换应用@Profile由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Spring条件注解@Conditoinal+Profile环境切换应用@Profile”