栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转自:

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

下文笔者讲述@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;

@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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号