Spring 中的 @ConditionalOnProperty 注解

1、概述

介绍@ConditionalOnProperty注解的主要目的。

2、@ConditionalOnProperty的目的

通常,在开发基于Spring的应用程序时,可能需要根据配置属性的存在和值有条件地创建一些bean。

例如,取决于是否将属性值设置为“ prod”或“ test”,可能想要注册一个DataSource bean来指向生产或测试数据库。

幸运的是,实现这一目标并不像想象的那样困难。 Spring框架正是为此目的提供了@ConditionalOnProperty注解。

简而言之,@ConditionalOnProperty仅在存在环境属性且具有特定值的情况下才启用Bean注册。 默认情况下,必须定义指定的属性,并且不等于false。

现在已经熟悉了@ConditionalOnProperty注解的用途,接下来深入地了解它的工作原理。

3、实践中的@ConditionalOnProperty注解

为了举例说明@ConditionalOnProperty的用法,接下来开发一个基本的通知系统。 为了使事情现在变得简单,假设要发送电子邮件通知。

首先,需要创建一个简单的服务来发送通知消息。 例如,考虑NotificationSender接口:

public interface NotificationSender {
    String send(String message);
}

接下来,提供NotificationSender接口的实现,以发送电子邮件:

public class EmailNotification  implements NotificationSender {
    @Override
    public String send(String message) {
        return "Email Notification: " + message;
    }
}

现在,看看如何使用@ConditionalOnProperty注解。 以这样的方式配置NotificationSender Bean,使其仅在定义了属性notification.service时才加载:

@Bean(name = "emailNotification")
@ConditionalOnProperty(prefix = "notification", name = "service")
public NotificationSender notificationSender() {
    return new EmailNotification();
}

prefix和name属性用于表示应检查的配置属性。

最后,需要在application.properties文件中定义的自定义属性:

notification.service=email

4、高级配置

正如前面已经了解的那样,@ConditionalOnProperty注解允许根据配置属性的存在条件有条件地注册bean。

使用此注释,可以做更多的事情。

假设要添加另一个通知服务,例如,一个允许发送SMS通知的服务。

为此,需要创建另一个NotificationSender实现:

public class SmsNotification implements NotificationSender {
    @Override
    public String send(String message) {
        return "SMS Notification: " + message;
    }
}

由于有两个实现,看看如何使用@ConditionalOnProperty有条件地加载正确的NotificationSender bean。

注解提供了havingValue属性。 非常有趣的是,它定义了一个属性必须具有的值,以便将特定的Bean添加到Spring容器中。

现在,指定要在什么条件下在上下文中注册SmsNotification实现:

@Bean(name = "smsNotification")
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
public NotificationSender notificationSender2() {
    return new SmsNotification();
}
    

借助于hadingValue属性,清楚地表明,仅当notification.service设置为sms时,才希望加载SmsNotification。

@ConditionalOnProperty具有另一个名为matchIfMissing的属性。 此属性指定在属性不可用的情况下条件是否应匹配。

单元测:

public class NotificationUnitTest {

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();

    @Test
    public void sender() {
        this.contextRunner.withPropertyValues("notification.service=email")
                .withUserConfiguration(NotificationConfig.class)
                .run(context -> {
                    assertThat(context).hasBean("emailNotification");
                    NotificationSender notificationSender = context.getBean(EmailNotification.class);
                    assertThat(notificationSender.send("Hello From spring!")).isEqualTo("Email Notification: Hello From spring!");
                    assertThat(context).doesNotHaveBean("smsNotification");
                });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25231683/article/details/131679948