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

网友投稿 242 2022-09-29

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

目录一、druid的资源配置管理二、c3p0资源配置管理三、加载properhttp://ties文件不加载系统属性加载多个properties文件加载所有properties文件加载properties文件标准格式从类路径或jar包中搜索并加载properties文件

spring第三方资源配置管理

DruidDataSourceComboPooledDataSource

一、druid的资源配置管理

导入druid的坐标:

com.alibaba

druid

1.1.16&ltmSauHzE;/version>

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

执行结果:

二、c3p0资源配置管理

maven远程仓库中找:

导入c3p0的坐标:

c3p0

c3p0

0.9.1.2

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

mysql

mysqlmSauHzE-connector-java

5.1.47

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

applicationContext.xml配置:

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

执行结果:

三、加载properties文件

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

2、使用context命名空间,加载指定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自动识别${ }加载的属性值,需要手工点击才可以查阅原始书写格式

不加载系统属性

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

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

加载多个properties文件

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

加载所有properties文件

加载properties文件标准格式

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

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

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

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

2、使用context命名空间,加载指定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自动识别${ }加载的属性值,需要手工点击才可以查阅原始书写格式

不加载系统属性

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

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

加载多个properties文件

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

加载所有properties文件

加载properties文件标准格式

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

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

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:物联网网关如何实现PLC转Sparkplug
下一篇:mailx的使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~