What happens in Spring if I use the @ActiveProfiles annotation in the config class instead of using it in the class that defines my beans?

I am researching Spring Core certificate and I have some doubts about using profiles in JUnit tests .

So, I know that if I annotate the class like this:

@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
    }
}

      

I am declaring a service bean that belongs to the stub profile .

So if my test class looks something like this:

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

      

this means it will be used by beans bean that belongs to stub profile and bean that do not have profile. Am I correct or am I missing something?

What happens if I instead use @ActiveProfiles annotation on the class (which will be an instance of Spring bean), I use it in the Java config class

Something like that:

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

      

What exactly? I think all beans configured in this class will belong to the jdbc-dev profile , but I'm not sure about that. Can you give me more information about this?

Why should I use the @Profile annotation in the ** config class * instead of annotating my beans directly?

Tpx

+2


source to share


1 answer


If you look at the JavaDoc annotation ActiveProfiles

, it contains this text:

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

The value is assumed to be used to declare active Spring profiles for test classes . So if you put it on a config class, it shouldn't have any effect.

And as for annotation @Profile

, it can be used both at the method level and at the class level. If you use it in a method annotated with @Bean

in the config class, then only the bean profile will be owned by the profile. If you use it in the config class it will apply to all beans in the config class, if you use it in the class the @Component

profile will be applied to the bean represented by that class.



@Profile

the JavaDoc annotation
provides a more detailed explanation of these rules.

Why should I use the @Profile annotation in the ** config class * instead of annotating my beans directly?

Well, if all beans in a given config class should only be active for certain profiles, then it makes sense to declare globally in the config class to avoid specifying a profile for all beans individually. But if you were to annotate all of the individualized beans, it would work.

+6


source







All Articles