Spring @Condition注解实例讲解

本文介绍Spring框架中的@Conditional注解,通过示例演示如何根据不同的条件注册特定的bean。使用@Conditional可以实现在满足一定条件下才将bean注入到Spring容器中。

@Condition是Spring4新增的注解,作用是在满足条件时将bean注入到Spring容器,在Spring Boot中大量用到了@Condition注解,例如@ConditionOnBean等为Spring Boot自动化配置的核心。源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {

    /**
     * All {@link Condition}s that must {@linkplain Condition#matches match}
     * in order for the component to be registered.
     */
    Class<? extends Condition>[] value();

}

注解的value是一个Condition接口的实现,Condition接口源码:

public interface Condition {

    /**
     * Determine if the condition matches.
     * @param context the condition context
     * @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
     * or {@link org.springframework.core.type.MethodMetadata method} being checked.
     * @return {@code true} if the condition matches and the component can be registered
     * or {@code false} to veto registration.
     */
    boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

}

我们做一个简单的实例,根据设置的profile不同,在Spring容器中注册的bean也不同,首先定义一个Service接口:

public interface ProfileService {

    void show();

}

再定义两个实现类:

public class ProfileDevService implements ProfileService {

    @Override
    public void show() {
        System.out.println("this is dev");
    }

}
public class ProfilePrdService implements ProfileService {

    @Override
    public void show() {
        System.out.println("this is prd");
    }

}

这两个类只能在Spring窗口中注册一个,方法就是判断profile,两个判断类:

public class OnDevProfile implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getActiveProfiles()[0].equals("dev");
    }

}
public class OnPrdProfile implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getActiveProfiles()[0].equals("prd");
    }

}

这两个类都实现了Condition接口,在注解@Condition需要用到这两个类:

@Configuration
public class ConditionConfiguration {

    @Bean
    @Conditional(OnDevProfile.class)
    public ProfileService devProfileService() {
        return new ProfileDevService();
    }

    @Bean
    @Conditional(OnPrdProfile.class)
    public ProfileService prdProfileService() {
        return new ProfilePrdService();
    }

}

在注入bean时加上了@Condition注解,所以能否注册成功,取决于@Condition注解的value类的matchs方法返回值。

启动一个容器:

public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getEnvironment().setActiveProfiles("prd");
        context.register(ConditionConfiguration.class);
        context.refresh();

        ProfileService profileService = context.getBean(ProfileService.class);
        profileService.show();

        context.close();
    }

这里设置active profiles为"prd",因此控制台打印的是this is prd,将active profiles改为"dev"打印的是this is dev

转载于:https://www.cnblogs.com/c04s31602/p/11210892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值