基于注解的spring profile

pom文件

	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.1.5.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>com.mchange</groupId>
	    <artifactId>c3p0</artifactId>
	    <version>0.9.5.2</version>
	</dependency>
	
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>8.0.15</version>
	</dependency>

主配置类

package cn.jwt.config;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
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 com.mchange.v2.c3p0.ComboPooledDataSource;
//可以根据当前环境,动态的激活和切换一系列组件的功能
//开发环境a,测试环境b,生产环境c
//1 加了环境标识的bean,只有这个环境被激活时才能注册到容器中。默认是default环境
//2 如果写在配置类上,只有指定的环境被激活时,整个配置类里面的所有配置才能生效
//3 没有环境标识的bean,在任何环境下都是加载的	
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class MyconfigProfile {
	@Value("${username}")
	private String user;
	@Value("${password}")
	private String passwrod;
	
	@Profile("test")
	@Bean("dataSourceTest")
	public DataSource dataSourceTest(@Value("${url}") String url)throws PropertyVetoException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(passwrod);
		dataSource.setJdbcUrl(url);
		dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
		return dataSource;
	}
	@Profile("pro")
	@Bean("dataSourceProduction")
	public DataSource dataSourceProduction(@Value("${url}") String url)throws PropertyVetoException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(passwrod);
		dataSource.setJdbcUrl(url);
		dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
		return dataSource;
	}
	@Profile("dev")
	@Bean("dataSourceDevelope")
	public DataSource dataSourceDevelope(@Value("${url}") String url)throws PropertyVetoException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(passwrod);
		dataSource.setJdbcUrl(url);
		dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
		return dataSource;
	}
}

测试类

package cn.jwt.app;

import java.util.Arrays;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import cn.jwt.config.MyconfigProfile;

public class AppTestProfile {
	//两种方式
	//1 使用命令行动态参数,在虚拟机位置加载 -Dspring.profiles.active=test
	//2 代码控制
	public static void main(String[] args) {
		//1 创建IOC容器 appclicationcontext
		AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
		//2 设置需要激活的环境
		applicationContext.getEnvironment().setActiveProfiles("test","dev");
		//3 注册主配置类
		applicationContext.register(MyconfigProfile.class);
		//4 启动刷新容器
		applicationContext.refresh();
		String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
		System.out.println(Arrays.toString(beanNamesForType));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jwt_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值