gpt4 book ai didi

java - 如果我在配置类上使用 @ActiveProfiles 注释而不是在定义我的 bean 的类上使用它,在 Spring 中会发生什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:26 28 4
gpt4 key购买 nike

我正在学习 Spring Core 认证,我对将 profiles 用于 JUnit 测试 有一些疑问。

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

@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 配置文件的 beans bean 和没有配置文件的 bean。是正确的还是我遗漏了什么?

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

类似的东西:

@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();
}
}

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

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

谢谢

最佳答案

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

ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.

这意味着它只应该用于为测试类 声明 Activity 的 Spring 配置文件。因此,如果将它放在配置类中,它应该没有任何效果。

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

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

Why I have to use the @Profile annotation on a **configuration class* instead annotate directly my beans?

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

关于java - 如果我在配置类上使用 @ActiveProfiles 注释而不是在定义我的 bean 的类上使用它,在 Spring 中会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27216388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com