Spring案例:数据源对象管理及加载properties文件

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Spring案例:数据源对象管理及加载properties文件

spring第三方资源配置管理

DruidDataSource
ComboPooledDataSource

一、druid的资源配置管理

导入druid的坐标:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

App运行输出druid:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

applicationContext.xml配置:


配置数据源对象作为spring管理的bean

<!--    管理DruidDataSource对象-->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
           <property name="username" value="root"/>
           <property name="password" value="root"/>
   </bean>

执行结果:

8820aaebf76d4251bb5cbe43f1c4b60e.png

二、c3p0资源配置管理

maven远程仓库中找:


Maven Repository: Search/Browse/Explore (mvnrepository.com)

https://mvnrepository.com/

导入c3p0的坐标:

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

c3p0还需要mysql的驱动,导入mysql的坐标:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

App运行输出与上面的一样。


applicationContext.xml配置:

  <!--c3p0连接池对象-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
           <property name="driverClass" value="com.mysql.jdbc.Driver"/>
           <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_db"/>
           <property name="user" value="root"/>
           <property name="password" value="root"/>
           <property name="maxPoolSize" value="1000"/>
       </bean>

也可以配置最大连接对象和其他需要配置数据。


执行结果:

2f10935e39fa4d05a03fb6d84c551d3c.png

三、加载properties文件

1、开启context命名空间,总共5处标红的地方需要修改为context。

<?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/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

2、使用context命名空间,加载指定properties文件

<context:property-placeholder location="jdbc.properties"/>

properties配置文件,配置时要加jdbc,不然会和系统环境变量冲突,系统优先级高:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

3、使用${ }读取加载的properties文件中的属性值


说明:idea自动识别${ }加载的属性值,需要手工点击才可以查阅原始书写格式

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

不加载系统属性:

可通过此种方法不加载系统属性,就不会和系统属性冲突:


system-properties-mode属性:是否加载系统属性

<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

加载多个properties文件:

用逗号分隔可加载多个properties文件:

<context:property-placeholder location="jdbc.properties,jdbc2.properties"/>

加载所有properties文件:

<context:property-placeholder location="*.properties"/>

加载properties文件标准格式:

classpath:*.properties:设置加载当前工程类路径中的所有properties文件

<context:property-placeholder location="classpath:*.properties"/>

从类路径或jar包中搜索并加载properties文件:

classpath*:*.properties:设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件

<context:property-placeholder location="classpath*:*.properties"/>
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
Java 关系型数据库 MySQL
Spring5深入浅出篇:Spring中的FactoryBean对象
Spring5深入浅出篇:Spring中的FactoryBean对象
|
3月前
|
Java 程序员 Spring
Spring5深入浅出篇:Spring对象属性注入详解
Spring5深入浅出篇:Spring对象属性注入详解
|
3月前
|
人工智能 监控 安全
spring cloud智慧工地信息平台管理系统源码
智慧工地解决方案依托计算机技术、物联网、云计算、大数据、人工智能、VR&AR等技术相结合,为工程项目管理提供先进技术手段,构建工地现场智能监控和控制体系,弥补传统方法在监管中的缺陷,最终实现项目对人、机、料、法、环的全方位实时监控。智慧工地平台支持项目级、公司级、集团级多级权限划分,可根据企业的组织架构进行项目权限、功能权限、数据权限设定。
27 1
|
3月前
|
前端开发 Java 数据库
洋酒销售系统|基于Spring实现洋酒销售管理平台【论文文档+开题+PPT+讲解视频】
洋酒销售系统|基于Spring实现洋酒销售管理平台【论文文档+开题+PPT+讲解视频】
|
1天前
|
XML Java 数据格式
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
手写spring第八章-定义标记类型Aware接口,实现感知容器对象
4 0
|
1天前
|
XML Java 数据格式
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
6 0
|
15天前
|
Java Spring
玩转对象掌控权:深入Spring,精准控制对象创建次数
玩转对象掌控权:深入Spring,精准控制对象创建次数
18 0
|
15天前
|
Java 关系型数据库 MySQL
高级对象装配:解析Spring创建复杂对象的秘诀
高级对象装配:解析Spring创建复杂对象的秘诀
27 0
高级对象装配:解析Spring创建复杂对象的秘诀
|
24天前
|
XML Java 数据格式
Spring(一)IOC小案例
Spring(一)IOC小案例
|
1月前
|
前端开发 Java 网络安全
ssh(Spring+Spring mvc+hibernate)简单增删改查案例
ssh(Spring+Spring mvc+hibernate)简单增删改查案例
10 0