Spring如何加载properties文件(properties,spring,开发技术)

时间:2024-05-04 20:34:03 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

spring第三方资源配置管理

  • DruidDataSource

  • ComboPooledDataSource

一、druid的资源配置管理

导入druid的坐标:

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

App运行输出druid:

importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjavax.sql.DataSource;publicclassApp{publicstaticvoidmain(String[]args){ApplicationContextctx=newClassPathXmlApplicationContext("applicationContext.xml");DataSourcedataSource=(DataSource)ctx.getBean("dataSource");System.out.println(dataSource);}}

applicationContext.xml配置:

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

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

执行结果:

Spring如何加载properties文件

二、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连接池对象--><beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="com.mysql.jdbc.Driver"/><propertyname="jdbcUrl"value="jdbc:mysql://localhost:3306/spring_db"/><propertyname="user"value="root"/><propertyname="password"value="root"/><propertyname="maxPoolSize"value="1000"/></bean>

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

执行结果:

Spring如何加载properties文件

三、加载properties文件

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

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

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

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

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

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/spring_dbjdbc.username=rootjdbc.password=root

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

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

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

不加载系统属性

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

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

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

加载多个properties文件

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

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

加载所有properties文件

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

加载properties文件标准格式

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

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

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

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

<context:property-placeholderlocation="classpath*:*.properties"/>
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Spring如何加载properties文件的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python中文分词和词频统计如何实现下一篇:

14 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18