apollo修改配置刷新bean_SpringBoot指定数据源配置 - 小窝蜗

news/2024/7/5 4:46:40

1.结合junit加载数据源

开发、测试、生产都有各自的数据库,这样需要配置的数据源不一样。
每次部署时修改配置过于繁琐,此时,

可以使用Spring的Profile来配置多个数据源,运行前指定需要加载的数据源即可。 采用spring结合junit做演示,使用oracle和mysql做数据源区别演示,

Profile名指定为oracle和mysql,junit测试时,

使用@ActiveProfiles(

"mysql"),@ActiveProfiles("oracle")来指定需要加载的数据源。 6d4affa295be954610ac838471002600.png

 ps:激活指定Profile,junit使用@ActiveProfiles("oracle")

2.了解Profile

(1)Spring中的Profile定义
     Spring中的Profile功能其实早在Spring 3.1的版本就已经出来,它可以理解为我们在Spring容器中所定义的Bean的逻辑组名称,只有当这些Profile被激活的时候,才会将Profile中所对应的Bean注册到Spring容器中。

举个更具体的例子,我们以前所定义的Bean,当Spring容器一启动的时候,
就会一股脑的全部加载这些信息完成对Bean的创建;而使用了Profile之后,
它会将Bean的定义进行更细粒度的划分,将这些定义的Bean划分为几个不同的组,
当Spring容器加载配置信息的时候,首先查找激活的Profile,
然后只会去加载被激活的组中所定义的Bean信息,
而不被激活的Profile中所定义的Bean定义信息是不会加载用于创建Bean的。

(2)使用Profile
由于我们平时在开发中,通常会出现在开发的时候使用一个开发数据库,
测试的时候使用一个测试的数据库,而实际部署的时候需要一个数据库。
以前的做法是将这些信息写在一个配置文件中,当我把代码部署到测试的环境中,
将配置文件改成测试环境;当测试完成,项目需要部署到现网了,
又要将配置信息改成现网的,真的好烦。。。
而使用了Profile之后,我们就可以分别定义3个配置文件,
一个用于开发、一个用户测试、一个用户生产,
其分别对应于3个Profile。当在实际运行的时候,
只需给定一个参数来激活对应的Profile即可,
那么容器就会只加载激活后的配置文件,
这样就可以大大省去我们修改配置信息而带来的烦恼。

(3)配置Spring profile
在介绍完Profile以及为什么要使用它之后,
下面让我们以一个例子来演示一下Profile的使用,
这里还是使用传统的XML的方式来完成Bean的装配。

第一步:Maven依赖
由于只是做一个简单演示,因此无需引入Spring其他模块中的内容,
只需引入核心的4个模块+测试模块即可

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--指定Spring版本,该版本必须等于3.1-->
        <>.4.RELEASE</>
        <!--指定JDK编译环境-->
        <>1.7</>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${}</source>
                    <target>${}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    第二步:例子代码  

package com.panlingxiao.spring.profile.service;

/**
 * 定义接口,在实际中可能是一个数据源
 * 在开发的时候与实际部署的时候分别使用不同的实现
 */publicinterface HelloService {

    public String sayHello();
}

 定义生产环境使用的实现类

package ;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.panlingxiao.spring.profile.service.HelloService;

/**
 * 模拟在生产环境下需要使用的类
 */
@Component
publicclass ProduceHelloService implements HelloService {

    //这个值读取生产环境下的配置注入
    @Value("#{}")
    private String name;

    public String sayHello() {
        return String.format("hello,I'm %s,this is a produce environment!",
                name);
    }
}

 模拟在开发环境下需要使用的类 

package ;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.panlingxiao.spring.profile.service.HelloService;

/**
 * 模拟在开发环境下使用类
 */
@Component
publicclass DevHelloService implements HelloService{

    //这个值是读取开发环境下的配置文件注入
    @Value("#{}")
    private String name;

    public String sayHello() {
        return String.format("hello,I'm %s,this is a development environment!", name);
    }
}

定义配置Spring配置文件

<?xml version="" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    xmlns:util=""
    xsi:schemaLocation=" /spring-beans-.xsd
        http://www.springframework.org/schema/context /spring-context-.xsd
        http://www.springframework.org/schema/util /spring-util-.xsd">

    <!-- 定义开发的profile -->
    <beans profile="development">
        <!-- 只扫描开发环境下使用的类 -->
        <context:component-scan base-package="" />
        <!-- 加载开发使用的配置文件 -->
        <util:properties id="config" location="classpath:dev/"/>
    </beans>

    <!-- 定义生产使用的profile -->
    <beans profile="produce">
        <!-- 只扫描生产环境下使用的类 -->
        <context:component-scan
            base-package="" />
        <!-- 加载生产使用的配置文件 -->    
        <util:properties id="config" location="classpath:produce/"/>
    </beans>
</beans>

说明:

  开发使用的配置文件,dev/

  name=Tomcat

  生产使用的配置文件,produce/

  name=Jetty

编写测试类

package com.panlingxiao.spring.profile.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.panlingxiao.spring.profile.service.HelloService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:")
/*
 * 使用注册来完成对profile的激活,
 * 传入对应的profile名字即可,可以传入produce或者dev
 */
@ActiveProfiles("produce")
publicclass TestActiveProfile {

    @Autowired
    private HelloService hs;

    @Test
    publicvoid testProfile() throws Exception {
        String value = ();
        System.out.println(value);
    }
}

3.部署时指定Profile

第一步:
   配置不同环境的配置文件
   建立对应的环境目录,我这里有三个环境分别是,
dev/test/pro 对应 开发/测试/生产。
建好目录后将相应的配置文件放到对应的环境目录中

  第二步:配置 设置 profile

    这里通过 activeByDefault 将开发环境设置为默认环境。

    如果你是用 idea 开发的话,在右侧 maven projects > Profiles 可以勾选对应的环境。 

<profiles>
    <profile>
        <!-- 本地开发环境 -->
        <id>dev</id>
        <properties>
            <>dev</>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!-- 测试环境 -->
        <id>test</id>
        <properties>
            <>test</>
        </properties>
    </profile>
    <profile>
        <!-- 生产环境 -->
        <id>pro</id>
        <properties>
            <>pro</>
        </properties>
    </profile>
</profiles>

第三步:打包时根据环境选择配置目录

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <warSourceExcludes>
            config/test/**,config/pro/**,config/dev/**
        </warSourceExcludes>
        <webResources>
            <resource>
                <directory>src/main/webapp/config/${}</directory>
                <targetPath>config</targetPath>
                <filtering>true</filtering>
            </resource>
        </webResources>
    </configuration>
</plugin>

第四步:根据环境打包

## 开发环境打包
mvn clean package -P dev

## 测试环境打包
mvn clean package -P test

## 生产环境打包
mvn clean package -P pro

4.配置profile (yml文件)

.全局配置文件
  (1)或者
  (2)固定位置:
        配置文件存放在src/main/resources目录或者类路径/config下;


 (1)Profile定义:
        Profile是Spring对不同环境提供不同配置功能的支持,
        可以通过激活,指定参数等方式快速切换环境;
 (2)多Profile文件
         主配置文件的文件名,可以是application-{profile}.properties;
         默认使用的配置;
         在配置文件中,使用application.=dev激活使用;

(3)yml支持的多文档块方式
server:
  port: 8081
spring:
  profiles:
    active: prod  # 激活生产环境

---
# 测试环境
server:
  port: 8083
spring:
  profiles: dev

---
# 生产环境
server:
  port: 8084
spring:
  profiles: prod

学习来源:

     //测试时加载数据源

     //了解Profile

    //部署时指定Profile

   //配置profile properties文件

    //配置profile yml文件


http://www.niftyadmin.cn/n/4101027.html

相关文章

基于springboot的项目销售_Github标星3.8K,基于SpringBoot框架和SaaS模式ERP软件源码分享...

在今天逛GitHub的时候&#xff0c;无意中发现一个基于SpringBoot框架和SaaS模式好用的ERP软件&#xff0c;觉得蛮不错的&#xff0c;特意分享给头条上的粉丝朋友们&#xff0c;接下来我会给大家介绍这个项目的一些特点和优势&#xff0c;如果您想获取到这个项目的源码进行学习的…

虚幻4地形怎么增加层_虚幻周报20200114 | 新春快乐!

官方知乎号&#xff1a;虚幻引擎 搜集日期&#xff1a;20191230-20200112 整理编辑&#xff1a; 大钊&#xff0c;小辉辉&#xff0c;马古斯&#xff0c;小帅帅 声明&#xff1a;文档搜集来自网上&#xff0c;难免遗漏&#xff0c;请联系我们投稿和添加关注。该文档版权归整理编…

统计机器翻译与神经机器翻译区别_如果每个人都献出一点爱,就会拥有一套超级牛的机器翻译系统...

所谓机器翻译技术就是利用计算机软件技术实现不同语言之间的自动翻译&#xff0c;目的为了帮助解决或缓解人工翻译代价过高和效率过低的问题。特别是针对大规模数据的实时和低成本翻译的应用场景&#xff0c;非人工翻译所为&#xff0c;有效利用机器翻译技术是最佳选择。 早期基…

swagger csrf 404_从0岁用到8岁的404页夫人独家超实用中英文双面闪卡素材包

原标题&#xff1a;从0岁用到8岁的404页夫人独家超实用中英文双面闪卡素材包&#xfeff;Hello~大家好~长假已过&#xff0c;盘子家将继续陪伴各位麻麻们做好家庭陪伴&#xff0c;精彩素材不间断分享&#xff0c;英语、识字、数学、科学、手工、STEM等总有你们想要哒&#xff0…

Access根据出生日期计算年龄_使用EDATE函数根据出生日期计算退休日期!

在企业事单位&#xff0c;从事人力资源工作的朋友可能曾经遇到过计算员工退休日期的问题。比如在下表所示的EXCEL数据中&#xff0c;假如男性的法定退休年龄为60岁&#xff0c;女性的法定年龄为55岁&#xff0c;那么使用EDATE函数结合IF函数计算退休日期就是一个比较巧妙的解决…

如何成为游戏设计师

游戏设计大师Chris Crawford对年轻人的忠告与鼓励序&#xff1a;前几天在搜关于游戏设计大师Chris Crawford的 相关文章时&#xff0c;无意中搜到这么一篇精彩的文章。该篇的主题同样是很多人写过的“大师对年轻人的话”&#xff0c;与其它人不同的是&#xff0c;这篇文章对年轻…

敏捷开发中编写高质量Java代码【转载】

敏捷开发的理念已经流行了很长的时间&#xff0c;在敏捷开发中的开发迭代阶段中&#xff0c;我们可以通过五个步骤&#xff0c;来有效的提高整个项目的代码质量。Java项目开发过程中&#xff0c;由于开发人员的经验、Java代码编写习惯&#xff0c;以及缺乏统一的标准和管理流程…

安卓 文本框怎么贴近边缘_PS教程:怎么用文字组成人脸

效果图&#xff1a;第一步打开Photoshop&#xff0c;创建一个新文档。我使用2560*1440的分辨率大小。导入一张照片&#xff0c;我用的是我一个朋友&#xff0c;一位优秀的摄影师John Arlington拍摄的半身像。这张照片是白色背景&#xff0c;所以抽取这个背景。第二步复制图层&a…