前进之路 发表于 2022-6-24 00:13:00

Spring注解@Profile的功能简介说明

转自:
   http://www.java265.com/JavaFramework/Spring/202206/3613.html
下文笔者讲述@Profile注解功能说明,如下所示
@Profile:
   Profile的功能就是配置
    让应用程序来说,不同的环境需要不同的配置

如:
开发环境,应用需要连接一个可供调试的数据库单机进程
生产环境,应用需要使用正式发布的数据库,通常是高可用的集群
测试环境,应用只需要使用内存式的模拟数据库
Spring框架提供了多profile的管理功能,我们可以使用profile功能来区分不同环境的配置例
配置类
package com.java265.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;
/**
* Profile:
*      Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能
*
*开发环境、测试环境、生产环境
*数据源:(/A)、(/B)、(/C)
*
*
* @Profile:指定组件在哪个环境下才能被注册到容器中,不指定,任何环境都会注册
*
*可以写在类上,只有是指定的环境,该类的所有配置才能开始生效
*/
@Configuration
@PropertySource("classpath:db.properties")
public class MainConfigOfProfile implements EmbeddedValueResolverAware {

    @Value("${db.user}")
    private String user;

    private StringValueResolver valueResolver;

    private String driverClass;


    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSource(@Value("${db.password}") String password) throws Exception {
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser(user);
      dataSource.setPassword(password);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
      dataSource.setDriverClass(driverClass);
      return dataSource;
    }

    @Profile("dev")
    @Bean("devDataSource")
    public DataSource dataSourceDev(@Value("${db.password}") String password) throws Exception {
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser("root");
      dataSource.setPassword(password);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
      dataSource.setDriverClass(driverClass);
      return dataSource;
    }

    @Profile("prod")
    @Bean("prodDataSource")
    public DataSource dataSourceProd(@Value("${db.password}") String password) throws Exception {
      ComboPooledDataSource dataSource = new ComboPooledDataSource();
      dataSource.setUser("root");
      dataSource.setPassword(password);
      dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/prod");
      dataSource.setDriverClass(driverClass);
      return dataSource;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
      this.valueResolver = stringValueResolver;
      this.driverClass = valueResolver.resolveStringValue("${db.driverClass}");
    }
}

测试
@Test
    public void test02(){
      //创建一个ApplicationContext
      AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
      //设置需要激活的环境(可以多个)
      ac.getEnvironment().setActiveProfiles("test", "dev");
      //注册主配置类
      ac.register(MainConfigOfProfile.class);
      //启动刷新容器
      ac.refresh();

      String[] names = ac.getBeanNamesForType(DataSource.class);
      for(String name : names){
            System.out.println(name);
      }

      ac.close();
    }

---运行以上代码,将输出以下信息------
testDataSource
devDataSource
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Spring注解@Profile的功能简介说明