The @Profile and @ActiveProfile in Spring

Links to the original text: https://www.jianshu.com/p/7ece11832402

1. Use @Profile

Configuration class

@Configuration
public class ProfileConfig {
    @Bean
    @Profile("upper")
    public UpperAction upperAction1(){
        return  new UpperAction("Tom");
    }

    @Bean
    @Profile("upper1")
    public UpperAction upperAction2(){
        return  new UpperAction("Jack");
    }

    @Bean
    @Profile("lower")
    public LowerAction lowerAction1(){
        return new LowerAction();
    }
/*
    @Bean
    @Profile("upper0")
    public Action action(){

        Action action1 =(Action) new UpperAction("Tom");

        return action1;
    }
*/
}

Test function

    @Test
    public void test0(){
        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("upper");
        ctx.register(ProfileConfig.class);
        ctx.refresh();

        UpperAction upperAction=ctx.getBean(UpperAction.class);
        upperAction.execute("hahaha");
    }

Note: It is essential to set the active Profile to Upper before registering the Bean's configuration class, otherwise undefined error ctx.refresh() will be reported, and refresh the container.

 

 

test result

test result

Question 1: What is the role of @Profile?

Figure 1


Function: How to make Spring containers register different beans under different conditions.

 

Question 2: What is the difference between @Profile and @Qualifier?
@ Qualifier: It helps to distinguish between different ways of using the same type of object.
At first I thought @Profile and @Service(... ) /@Bean(... ) The effect is almost the same. They all take a name.

 

But @Profile can be used as well.

@ Profile can also be like this


The effect is this.

image.png


Why use @Profile?
Because we usually use a development database when we develop, a test database when we test, and a database when we deploy. Previous practice is to write this information in a configuration file, when I deploy the code to the test environment, the configuration file will be changed to the test environment; when the test is completed, the project needs to be deployed to now, and the configuration information will be changed to now. Very troublesome.
With Profile, we can define three configuration files, one for development, one for user testing, and one for user production, which correspond to three Profiles respectively. When running in practice, we only need to give a parameter to activate the corresponding Profile, then the container will only load the activated configuration file, which can greatly save us the trouble of modifying the configuration information.

 

2. Use @ActiveProfile

Configuration class

@Configuration
public class ProfileConfig {

    @Bean
    @Profile("upper")
    public UpperAction upperAction1(){
        return  new UpperAction("Tom");
    }

    @Bean
    @Profile("upper1")
    public UpperAction upperAction2(){
        return  new UpperAction("Jack");
    }

    @Bean
    @Profile("lower")
    public LowerAction lowerAction1(){
        return new LowerAction();
    }
/*
    @Bean
    @Profile("upper0")
    public Action action(){
        Action action1 =(Action) new UpperAction("Tom");
        return action1;
    }
*/
}

Test code

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes =ProfileConfig.class)
@ActiveProfiles("upper")
public class Test4_UsingProfile {

    @Autowired
    Action action;

    @Test
    public void test3() {
        action.execute("wushuohanaaaaaa");
    }
}

test result

test result


Question 1: If @ActiveProfile is not used in the test class
LowerAction1() in the configuration class does not set @Profile

 

@Configuration
public class ProfileConfig {

    @Bean
    @Profile("upper")
    public UpperAction upperAction1(){
        return  new UpperAction("Tom");
    }

    @Bean
    @Profile("upper1")
    public UpperAction upperAction2(){
        return  new UpperAction("Jack");
    }

    @Bean
    public LowerAction lowerAction1(){
        return new LowerAction();
    }
}

Test code

   @Autowired
    LowerAction lowerAction;

    @Test
    public void test2() {
        lowerAction.execute("wushuohan");
    }

test result

test result


CONCLUSION: When @ActiveProfile is not used, profile=default and beans without profile are loaded. The PowerAction of Profile is not set here to be loaded.
@ Bean s transform instantiated objects into beans and place them in containers. When the Spring container starts, all beans in the Spring container are scanned, and when the @Autowired annotation is found, the beans matching it (default type matching) are found and injected into the corresponding place.
So whether there is @ActiveProfile or not, you can load beans without @Profile.

Keywords: Spring Database

Added by bran on Thu, 01 Aug 2019 05:11:46 +0300