Spring @Profile annotation

I. Introduction

It is so simple to use multi-environment development in springboot, do you want to know how it is implemented in spring? Let's learn together! ! Why don't you study the basics and the interview has been diss? After all, it is not what you think it will be. In fact, you will not. !

Inheriting the spirit of open source, Spreading technology knowledge;

Two @profile to achieve multi-environment configuration

2.1 @profile configuration

The purpose of using @profile annotation is to develop multiple environments, such as development environment using dev, production environment using prod, you can use @Profile annotation to achieve different development environments using different data sources;

@profile annotation instructions:

  1. Before Spring 3.2 @Profile annotation was used on the class
  2. After spring3.2 @Profile annotation is used in the method
/**
 * @Author lsc
 * <p>spring3.2之前 @Profile注解用在类上
 * spring3.2 之后 @Profile注解用在 方法上
 * </p>
 */
@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        System.out.println(" dev DataSource !!");
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        basicDataSource.setUrl("jdbc:mysql://localhost:3308/zszxz");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("1234");
        return basicDataSource;
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        System.out.println(" prod DataSource !!");
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/zszxz");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("1234");
        return basicDataSource;
    }
}

If you are configuring in xml, the example is as follows

<beans profile="dev">
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
              p:driverClassName="com.mysql.jdbc.Driver"
              p:url="jdbc:mysql://localhost:3306/zszxzb"
              p:username="root"
              p:password="1234"/>
</beans>
<beans profile="prod">
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
              p:driverClassName="com.mysql.jdbc.Driver"
              p:url="jdbc:mysql://localhost:3306/zszxzb"
              p:username="root"
              p:password="1234"/>
</beans>

2.2 Activation method

Activation method one

  1. spring.profiles.active activation method
  2. If spring.profiles.active is not configured, use spring.profiles.default activation method
  3. If the previous configuration is all, it will load the beans that are not defined in the profile;

Activation method 2

Configure in web.xml

<context-param>
	<param-name>spring.profiles.active</param-name>
    <param-value>dev</param-value>
</context-param>
<servlet>
        <servlet-name>zszxzServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>dev</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>zszxzServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Activation method three

Use @ActiveProfilesannotations on classes

The test is as follows

@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes= DataSourceConfig.class)//加载配置类
@ActiveProfiles("dev")
public class ProfileTest {

    @Autowired
    private DataSource dataSource;

    @Test
    public void sheetTest(){
        JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        List<String> query = jdbc.query("select * from customer", new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getLong("id") + ":" + rs.getString("customer_name");
            }
        });
        // [19:知识追寻者, 20:知识追寻者, 21:知识追寻者, 22:知识追寻者, 23:知识追寻者]
        System.out.println(query);
    }
}

Source address: public account summary article location

Guess you like

Origin www.cnblogs.com/zszxz/p/12700309.html