Spring为应用程序准备了Profile这一概念,用来表示不同的环境。例如,我们分别定义开发、测试和生产这3个环境:

    • native
    • test
    • production

    创建某个Bean时,Spring容器可以根据注解来决定是否创建。例如,以下配置:

    如果当前的Profile设置为test,则Spring容器会调用createZoneIdForTest()创建ZoneId,否则,调用createZoneId()创建ZoneId。注意到@Profile("!test")表示非test环境。

    在运行程序时,加上JVM参数-Dspring.profiles.active=test就可以指定以test环境启动。

    实际上,Spring允许指定多个Profile,例如:

    1. -Dspring.profiles.active=test,master

    可以表示test环境,并使用master分支代码。

    1. @Bean
    2. @Profile({ "test", "master" }) // 同时满足test和master
    3. ...
    4. }

    除了根据@Profile条件来决定是否创建某个Bean外,Spring还可以根据@Conditional决定是否创建某个Bean。

    例如,我们对SmtpMailService添加如下注解:

    它的意思是,如果满足的条件,才会创建SmtpMailService这个Bean。OnSmtpEnvCondition的条件是什么呢?我们看一下代码:

    1. public class OnSmtpEnvCondition implements Condition {
    2. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    3. return "true".equalsIgnoreCase(System.getenv("smtp"));
    4. }
    5. }

    因此,OnSmtpEnvCondition的条件是存在环境变量smtp,值为true。这样,我们就可以通过环境变量来控制是否创建SmtpMailService

    Spring只提供了@Conditional注解,具体判断逻辑还需要我们自己实现。Spring Boot提供了更多使用起来更简单的条件注解,例如,如果配置文件中存在app.smtp=true,则创建MailService

    1. @Component
    2. @ConditionalOnProperty(name="app.smtp", havingValue="true")
    3. public class MailService {
    4. }

    如果当前classpath中存在类,则创建MailService

    1. @Component
    2. @ConditionalOnProperty(name = "app.storage", havingValue = "file", matchIfMissing = true)
    3. public class FileUploader implements Uploader {
    4. ...
    5. }

    在生产环境运行时,我们会把文件存储到类似AWS S3上:

    1. @Component
    2. @ConditionalOnProperty(name = "app.storage", havingValue = "s3")
    3. public class S3Uploader implements Uploader {
    4. ...

    其他需要存储的服务则注入Uploader

    当应用程序检测到配置文件存在app.storage=s3时,自动使用S3Uploader,如果存在配置app.storage=file,或者配置app.storage不存在,则使用FileUploader

    可见,使用条件注解,能更灵活地装配Bean。

    从下载练习:使用@Profile进行条件装配 (推荐使用快速下载)

    Spring允许通过@Profile配置不同的Bean;

    使用条件装配 - 图1