iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >一文详解Spring加载properties文件的方式
  • 681
分享到

一文详解Spring加载properties文件的方式

2024-04-02 19:04:59 681人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、druid的资源配置管理二、c3p0资源配置管理三、加载properties文件不加载系统属性加载多个properties文件加载所有properties文件加载proper

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>

执行结果:

二、c3p0资源配置管理

Maven远程仓库中找:

导入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>

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

执行结果:

三、加载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"/>

以上就是一文详解Spring加载properties文件的方式的详细内容,更多关于Spring加载properties文件的资料请关注编程网其它相关文章!

--结束END--

本文标题: 一文详解Spring加载properties文件的方式

本文链接: https://www.lsjlt.com/news/151157.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作