Profile案例


在实际开发中,项目即将上线,可能需要不停的在开发环境,生产环境,测试环境…之间进行切换

Java配置实现

创建实体类

package com.sxt.pojo;

public class DataSources {

	private String url;
	private String userName;
	private String password;
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "DataSources [url=" + url + ", userName=" + userName + ", password=" + password + "]";
	}
}

Java配置文件

package com.sxt;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.sxt.pojo.DataSources;
/**
 * 该类相当于application.xml
 * @author Administrator
 *
 */
@Configuration //该配置是必须的
public class JavaConfig {

	/**
	 * @Profile注解相当于一个标记,标记当前的dataSource是开发环境下的dataSource
	 * @return
	 */
	@Bean
	@Profile("dev") //设置开发环境
	public DataSources dataSourcesDev() {
		DataSources ds = new DataSources();
		ds.setUrl("dev:8080");
		ds.setUserName("admin");
		ds.setPassword("1234");
		return ds;
	}
	@Bean
	@Profile("pro") //设置生产环境
	public DataSources dataSourcesPro() {
		DataSources ds = new DataSources();
		ds.setUrl("pro:8080");
		ds.setUserName("root");
		ds.setPassword("1234");
		return ds;
	}
}

测试

package com.sxt.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.sxt.JavaConfig;
import com.sxt.pojo.DataSources;

public class TestDemo {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac =
				new AnnotationConfigApplicationContext();
		//设置环境变量信息
		ac.getEnvironment().setActiveProfiles("pro");
		//注册Java配置文件
		ac.register(JavaConfig.class);
		//刷新 重新加载
		ac.refresh();
		
		DataSources sources = ac.getBean(DataSources.class);
		System.out.println(sources);
	}
}

在这里插入图片描述
在这里插入图片描述

XML配置

package com.sxt.pojo;

public class DataSources {

	private String url;
	private String userName;
	private String password;
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "DataSources [url=" + url + ", userName=" + userName + ", password=" + password + "]";
	}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- Profile配置 -->
	<beans profile="pro">
		<bean class="com.sxt.pojo.DataSources">
			<property name="url" value="http://pro:8080"/>
			<property name="userName" value="admin"/>
			<property name="password" value="1234"/>
		</bean>
	</beans>
	<beans profile="dev">
		<bean class="com.sxt.pojo.DataSources">
			<property name="url" value="http://dev:8080"/>
			<property name="userName" value="root"/>
			<property name="password" value="1234"/>
		</bean>
	</beans>
 </beans>

测试

package com.sxt.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.pojo.DataSources;

public class TestDemo {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ac = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		ac.getEnvironment().setActiveProfiles("dev");
		ac.refresh();
		
		DataSources ds = ac.getBean(DataSources.class);
		System.out.println(ds);
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值