Profile案例

本文介绍如何使用Java配置实现不同环境(如开发、生产)的数据源配置切换,通过@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);
	}
}

在这里插入图片描述

### 关于Profile的概念及其在编程和软件开发中的应用 #### 定义与作用 在编程和软件开发领域,“profile”通常指代个人资料或配置文件。它用于存储用户的个人信息、偏好设置或其他元数据,以便系统能够更好地识别用户并提供个性化服务[^1]。 对于开发者而言,profile还可以表示性能分析工具的结果报告。通过这些报告,程序员可以评估程序运行期间的各项指标(如CPU占用率、内存消耗等),从而优化代码效率[^4]。 #### 实际案例中的体现 以引用材料为,在描述团队成员信息时提到"优快云 Profile"[^1] ,这里指的是该平台上展示的技术博客和个人简介页面;而在另一个场景下讨论项目版本控制记录时,则涉及到了提交日志作为历史追踪依据之一 [^2] 。这表明无论是人员管理还是工程实践环节里,"profile"都扮演着重要角色. 另外值得注意的是,当涉及到多媒体处理框架设计时(如Intel® Media SDK),其文档也可能提及某些特定参数集合被命名为profiles来定义支持的功能选项范围以及质量等级等方面的内容.[^3] 以下是创建简单用户档案类的一个Python实现子: ```python class UserProfile: def __init__(self, user_id, name, email): self.user_id = user_id self.name = name self.email = email def display_profile(self): return f"User ID: {self.user_id}\nName: {self.name}\nEmail: {self.email}" # 创建实对象 user = UserProfile('832102214', 'Zhenggang Chen', 'example@example.com') print(user.display_profile()) ``` 此脚本展示了如何构建基本的数据结构用来保存有关个体的关键属性值,并可通过方法调用轻松获取完整的显示字符串形式呈现出来。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值