当前位置: 首页 > news >正文

【Spring】多环境切换

🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 CSDN🚩🚩🚩
🎈由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!🌠个人主页


目录

  • 🌟 一、Java配置
  • 🌟 二、XML配置
  • 🌟 三、原理分析
  • 🌟 三、自定义Profile


🌟 一、Java配置

首先创建一个 DataSource 类:

public class DataSource {private String username;private String password;private String url;@Overridepublic String toString() {return "DataSource{" +"username='" + username + '\'' +", password='" + password + '\'' +", url='" + url + '\'' +'}';}
}

向 Spring 容器中注册多个 DataSource,并且在注册的时候设置 Profile,只有当条件满足的时候,才向 Spring 容器中注册相应的 Bean,否则不注册

public class JavaConfig {@Bean@Profile("dev")DataSource devDataSource(){DataSource dev = new DataSource();dev.setUsername("dev");dev.setPassword("dev");dev.setUrl("jdbc:mysql:3306/dev");return dev;}@Bean@Profile("prod")DataSource prodDataSource(){DataSource prod = new DataSource();prod.setUsername("prod");prod.setPassword("prod");prod.setUrl("jdbc:mysql:3306/prod");return prod;}}

这里到底向 Spring 容器中注册多少个 DataSource,主要还是看条件是否满足,如果有多个条件满足,那么就注册多个 DataSource 实例到 Spring 容器中,否则就不注册。
最后,启动容器:

	public static void main(String[] args) {//这里注意,不能添加配置类,如果添加了配置类,则 refresh 方法会被调用,而 Spring 容器的初始化则正是从这里开始的//此时,我们还没设置当前系统环境AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();//这里注意,不能添加配置类,如果添加了配置类,则 refresh 方法会被调用,而 Spring 容器的初始化则正是从这里开始的//此时,我们还没设置当前系统环境ConfigurableEnvironment evn = ctx.getEnvironment();evn.setActiveProfiles("prod");//此时再去设置配置类ctx.register(JavaConfig.class);//开始初始化容器ctx.refresh();DataSource dataSource = ctx.getBean(DataSource.class);System.out.println(dataSource);}

启动容器的时候,一开始不要传配置类进去,如果传了,则 refresh 方法会被自动调用,而此时我们还没设置容器的环境!所以,使用无参构造方法创建 AnnotationConfigApplicationContext 对象,然后设置系统环境,再去设置配置类,最后再去初始化容器

🌟 二、XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><beans profile="dev"><bean class="com.dong.demo3.DataSource" ><property name="username" value="dev"/><property name="password" value="dev"/><property name="url" value="jdbc:mysql:3306/dev"/></bean></beans><beans profile="prod"><bean class="com.dong.demo3.DataSource" ><property name="username" value="prod"/><property name="password" value="prod"/><property name="url" value="jdbc:mysql:3306/prod"/></bean></beans>
</beans>

这个是在 父标签 beans 中设置条件,满足对应的条件,父标签中的所有 Bean 才会被初始化

public class XMLDemo1 {public static void main(String[] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();ctx.getEnvironment().addActiveProfile("prod");ctx.setConfigLocation("beans1.xml");ctx.refresh();DataSource bean = ctx.getBean(DataSource.class);System.out.println(bean);}
}

🌟 三、原理分析

@Profile注解的定义就是一个组合注解:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 从这里可以看到,本质上就是一个条件注解,条件则是 ProfileCondition
@Conditional(ProfileCondition.class)
public @interface Profile {/*** The set of profiles for which the annotated component should be registered.*/String[] value();}

那我们来看下 ProfileCondition:

class ProfileCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {// 这句话实际上就是查询到 Profile 注解的所有属性// 对于 Profile 注解来说,实际上就只有 value 属性// value->dev   value->prodMultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());if (attrs != null) {// attrs.get("value") 表示读取属性中的 value 属性,value 属性的值是一个数组,所以这里需要遍历for (Object value : attrs.get("value")) {//判断当前系统环境中是否包含这个 valueif (context.getEnvironment().matchesProfiles((String[]) value)) {return true;}}return false;}return true;}}

🌟 三、自定义Profile

首先,先自定义注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Conditional(MyCondition.class)
public @interface MyProfile {String[] value();
}

然后自定义条件,在自定义条件中解析注解:

public class MyCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(MyProfile.class.getName());if(attrs != null){List<Object> list = attrs.get("value");for (Object value : list) {boolean b = context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value));if(b){return true;}}}return false;}
}

最后使用自定义注解:

public class JavaConfig {@Bean@MyProfile("dev")DataSource devDataSource(){DataSource dev = new DataSource();dev.setUsername("dev");dev.setPassword("dev");dev.setUrl("jdbc:mysql:3306/dev");return dev;}@Bean@MyProfile("prod")DataSource prodDataSource(){DataSource prod = new DataSource();prod.setUsername("prod");prod.setPassword("prod");prod.setUrl("jdbc:mysql:3306/prod");return prod;}}

启动容器:

	public static void main(String[] args) {//这里注意,不能添加配置类,如果添加了配置类,则 refresh 方法会被调用,而 Spring 容器的初始化则正是从这里开始的//此时,我们还没设置当前系统环境AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();//这里注意,不能添加配置类,如果添加了配置类,则 refresh 方法会被调用,而 Spring 容器的初始化则正是从这里开始的//此时,我们还没设置当前系统环境ConfigurableEnvironment evn = ctx.getEnvironment();evn.setActiveProfiles("prod");//此时再去设置配置类ctx.register(JavaConfig.class);//开始初始化容器ctx.refresh();DataSource dataSource = ctx.getBean(DataSource.class);System.out.println(dataSource);}
http://www.lryc.cn/news/175802.html

相关文章:

  • python经典百题之求10000之内的素数
  • ROS2 从头开始:第 5 部分 - 并发、执行器和回调组
  • 笔试强训Day3
  • 软考软件设计师-存储管理-文件管理-计算机网络(中
  • Vue3的学习
  • 什么是Peppol ID?如何创建?
  • Spring注解大揭秘:@Component、@Service、@Repository详解
  • Innodb底层原理与Mysql日志机制
  • 浅谈大数据背景下用户侧用电数据在电力系统的应用与发展分析
  • 20230919在WIN10下使用python3将PDF文档转为DOCX格式的WORD文档
  • PCR检测试剂——博迈伦
  • spring一个项目多个模块聚合打包问题解决方案
  • Linux设备树(Device Tree)何时被解析
  • 【Elasticsearch】数据简单操作(二)
  • 4 vCPU 实例达成 100 万 JSON API 请求/秒的优化实践
  • 呼叫中心系统有什么优势
  • 如何在linux操作系统下安装nvm
  • Linux 入门:基本指令
  • IP转地理位置:探讨技术与应用
  • 关于埋点上报
  • 最新博客园图片上传接口,模拟实现图片上传
  • ROS2 从头开始:第 08/8回 - 使用 ROS2 生命周期节点简化机器人软件组件管理
  • Vue组件库Element
  • broadcast自动扩展
  • 【Pm4py第七讲】关于visualization
  • 通过 BigQuery 中的 11 个新链增强 Google Cloud 的区块链数据服务
  • C++笔记之文档术语——将可调用对象作为函数参数
  • 【Android知识笔记】FrameWork中的设计模式
  • 机器学习西瓜书+南瓜书吃瓜教程第三章学习笔记
  • JUnit5单元测试提示“Not tests were found”错误