【问题标题】:What happens in Spring if I use the @ActiveProfiles annotation on a configuration class instead use it on the class that defines my beans?如果我在配置类上使用 @ActiveProfiles 注释而不是在定义我的 bean 的类上使用它,Spring 会发生什么?
【发布时间】:2015-01-28 17:59:47
【问题描述】:

我正在为 Spring Core 认证而学习,我对在 JUnit 测试中使用 profiles 有一些疑问。

所以我知道,如果我用以下方式注释一个类:

@Profile("stub")
@Repository
public class StubAccountRepository implements AccountRepository {

    private Logger logger = Logger.getLogger(StubAccountRepository.class);

    private Map<String, Account> accountsByCreditCard = new HashMap<String, Account>();

    /**
     * Creates a single test account with two beneficiaries. Also logs creation
     * so we know which repository we are using.
     */
    public StubAccountRepository() {
        logger.info("Creating " + getClass().getSimpleName());
        Account account = new Account("123456789", "Keith and Keri Donald");
        account.addBeneficiary("Annabelle", Percentage.valueOf("50%"));
        account.addBeneficiary("Corgan", Percentage.valueOf("50%"));
        accountsByCreditCard.put("1234123412341234", account);
    }

    public Account findByCreditCard(String creditCardNumber) {
        Account account = accountsByCreditCard.get(creditCardNumber);
        if (account == null) {
            throw new EmptyResultDataAccessException(1);
        }
        return account;
    }

    public void updateBeneficiaries(Account account) {
        // nothing to do, everything is in memory
    }
}

我声明了一个属于 stub 配置文件的 服务 bean

所以,如果我的 测试类 是这样的:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestInfrastructureConfig.class)
@ActiveProfiles("stub")
public class RewardNetworkTests {
    .....................................
    .....................................
    .....................................
}

这意味着它将使用属于 stub 配置文件的 bean bean 和没有配置文件的 bean。是对的还是我错过了什么?

如果在一个类(其实例将是 Spring bean)上使用 @ActiveProfiles 注释我在 Java 配置类上使用它会发生什么?

类似的东西:

@Configuration
@Profile("jdbc-dev")
public class TestInfrastructureDevConfig {

    /**
     * Creates an in-memory "rewards" database populated 
     * with test data for fast testing
     */
    @Bean
    public DataSource dataSource(){
        return
            (new EmbeddedDatabaseBuilder())
            .addScript("classpath:rewards/testdb/schema.sql")
            .addScript("classpath:rewards/testdb/test-data.sql")
            .build();
    }   
}

具体是做什么的?我认为在这个类中配置的所有 bean 都将属于 jdbc-dev 配置文件,但我不确定。你能给我更多关于这件事的信息吗?

为什么我必须在 **configuration class* 上使用 @Profile 注释而不是直接注释我的 bean?

Tnx

【问题讨论】:

    标签: java spring annotations spring-annotations spring-profiles


    【解决方案1】:

    如果您查看ActiveProfiles 注释的JavaDoc,它包含以下文本:

    ActiveProfiles 是一个类级别的注解,用于声明在加载用于测试类的 ApplicationContext 时应该使用哪些活动 bean 定义配置文件。

    意味着它只应该用于为测试类声明活动的 Spring 配置文件。所以如果把它放在一个配置类上应该没有效果。

    对于@Profile注解,它可以在方法和类级别上使用。如果您在配置类中用@Bean 注释的方法上使用它,则只有该bean 将属于配置文件。如果你在配置类上使用它,它将应用于配置类中的所有bean,如果你在@Component类上使用它,配置文件将应用于该类所代表的bean。

    @Profile annotation JavaDoc 提供了对这些规则的更详细说明。

    为什么我必须在 **configuration class* 上使用 @Profile 注释而不是直接注释我的 bean?

    如果给定配置类中的所有 bean 应该只对某些配置文件是活动的,那么在配置类上全局声明它是有意义的,以避免必须在所有 bean 上单独指定配置文件。但是,如果您要对所有单个 bean 进行注释,它也会起作用。

    【讨论】:

      猜你喜欢
      • 2017-01-23
      • 1970-01-01
      • 2021-11-08
      • 2015-11-11
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多