spring管理数据源和引入外部属性文件~

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: spring管理数据源和引入外部属性文件~

引入外部属性文件:

加入新的依赖:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.30</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.31</version>
    </dependency>

不能直接使用DataSource,因为它是一个接口,而我们开始学习IOC容器时,就说过,IOC是通过我们class所设置的类型来获取这个类型的class对象,再通过调用newInstance()方法调用无参构造创建对象,但接口是没有构造方法的,因此这里我们不能使用接口, 而应该使用该接口的实现类, DataSource的实现类是com.alibaba.druid.pool.DruidDataSource


获取连接对象:

创建spring-datasources.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">
<bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource">
    <!-- 关于数据库的连接-->
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
</beans>


创建测试类获取连接对象:

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class DataSources_text {
    @Test
    public void testDataSources() throws SQLException {
        ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-datasources.xml");
       DruidDataSource druidDataSource= ioc.getBean(DruidDataSource.class);
       System.out.println(druidDataSource.getConnection());
    }
}

输出如下:

获取成功!


关于数据库的连接,我们除了可以像上述这种写法一样将其写在.xml文件中之外,我们还可以新创建个properties文件,如下所示:

创建完成之后,我们只需要将关于数据库连接的代码写入其中即可!

如下所示:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=root

但如果properties文件过多的话,很容易出现重名问题,对此,我们的解决办法是给每个属性+前缀,如下所示;

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root

而对于.xml文件和properties文件来说,他们之间是没有任何关系的,如果我们想使用properties中的数据,那么需要将其引入到.xml文件中去,如何引入?如下所示,新创建一个bean标签,通过类引入,但这个类上面加了一条横线,那么则表示该类是过时的,是不推荐使用的

既然不推荐使用,那么我们就不用了呗,spring为我们提供了更简单的方式,使用property-placeholder标签,它是spring中的一个标签用于在配置文件中引入外部的属性文件,并将属性值注入到Spring的bean中。如下所示:

<!-- context 也是需要引入约束的-->
<!-- location是用来设置当前properties文件的路径-->
<context:property-placeholder   location="jdbc.properties"></context:property-placeholder>

引入之后,该文件中的数据如何获取呢?

方法和在mybatis中学习过的相同,通过${key}的方式,如下所示:

<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>

修改spring-dataSource.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/context
          https://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/beans
          https://www.springframework.org/schema/beans/spring-beans.xsd">
<context:property-placeholder  location="jdbc.properties"></context:property-placeholder>
    <bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 关于数据库的连接-->
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
</beans>

获取成功!

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
人工智能 监控 安全
spring cloud智慧工地信息平台管理系统源码
智慧工地解决方案依托计算机技术、物联网、云计算、大数据、人工智能、VR&AR等技术相结合,为工程项目管理提供先进技术手段,构建工地现场智能监控和控制体系,弥补传统方法在监管中的缺陷,最终实现项目对人、机、料、法、环的全方位实时监控。智慧工地平台支持项目级、公司级、集团级多级权限划分,可根据企业的组织架构进行项目权限、功能权限、数据权限设定。
27 1
|
3月前
|
前端开发 Java 数据库
洋酒销售系统|基于Spring实现洋酒销售管理平台【论文文档+开题+PPT+讲解视频】
洋酒销售系统|基于Spring实现洋酒销售管理平台【论文文档+开题+PPT+讲解视频】
|
2天前
|
Java uml Spring
手写spring第四章-完善bean实例化,自动填充成员属性
手写spring第四章-完善bean实例化,自动填充成员属性
11 0
|
16天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
23 1
|
2月前
|
监控 Java 测试技术
Spring Boot和XXL-Job:高效定时任务管理
Spring Boot和XXL-Job:高效定时任务管理
60 0
|
2月前
|
XML 缓存 Java
解锁Spring Boot AOP的魔力:优雅地管理交叉关注点
解锁Spring Boot AOP的魔力:优雅地管理交叉关注点
149 0
|
3月前
|
Java API Nacos
spring.config.import 是一个 Spring Cloud Config Server 的属性,
spring.config.import 是一个 Spring Cloud Config Server 的属性,【1月更文挑战第25天】【1月更文挑战第123篇】
56 1
|
3月前
|
Dubbo Java 应用服务中间件
如何在 Spring Boot 中制作和管理 Banner:完全指南
在 Spring Boot 应用的初启页面展示独特的ASCII艺术品能够为您的应用增添特色,并为用户或开发者提供温馨的问候。这种ASCII图案常常蕴含着项目的名称、版本信息、以及作者等重要数据。除此之外,这样的欢迎页面还起到加强品牌形象的作用。
|
3月前
|
缓存 Java Spring
Spring5源码(25)-Spring填充bean属性及应用生命周期接口
Spring5源码(25)-Spring填充bean属性及应用生命周期接口
38 0
|
3月前
|
XML Java 数据格式
Spring5源码(6)-Spring注入集合属性
Spring5源码(6)-Spring注入集合属性
14 0