二手车网站设计,河北建设集团官方网站,做果盘网站,浦口区网站建设质量推荐目录 一、博客引言。 二、基本配置准备。 #xff08;1#xff09;初步分析。 #xff08;2#xff09;初始spring配置文件。 三、spring自定义标签的引入。 #xff08;1#xff09;基本了解。 #xff08;2#xff09;引入新的命名空间#xff1a;xmlns:context。 1初步分析。 2初始spring配置文件。 三、spring自定义标签的引入。 1基本了解。 2引入新的命名空间xmlns:context。 3使用context空间加载properties文件。 1、resources目录下新建properties文件。 2、使用标签。 3、使用属性占位符${}读取properties文件里对应属性值。 四、加载properties文件的测试。 1dao层。 2spring配置文件中配置BookDaoImpl实现类的bean。 3测试类。 4properties文件中的键名注意与系统变量区分。 system-properties-mode。 5加载多个properties文件。 1、测试2个properties文件的读取。 2、使用*.properties加载所有properties文件。 3、使用classpath:*.properties加载当前工程所有properties文件。规范 4、使用classpath*:*.properties加载所有properties文件。包括外部jar包 五、spring配置文件中加载properties文件几种写法的小结。 一、博客引言。 之前的学习——管理第三方资源的两种数据源对象。其中那些配置的属性值一起写在spring配置文件中是不太合适的。不方便统一管理 在本篇博客的学习就是把这些配置的属性值单独抽出来存放到properties文件中。然后再利用spring去加载对应的properties文件的信息。 二、基本配置准备。 1初步分析。 本篇博客的案例是基于上篇博客进行。其中主要的核心就是管理阿里云-druid的数据源对象。spring学习(druid、c3p0的数据源对象管理)(案例学习)-CSDN博客 上次的学习就是直接在配置文件中将数据库驱动、url、用户名与密码的属性值全部写死在spring配置文件中。 2初始spring配置文件。 其中主要的bean是对druid的数据源对象的基本配置。就目前而言属性的值还是写在spring配置文件中待会就进行修改。 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--管理DruidDataSource对象--bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/propertyproperty nameurl valuejdbc:mysql://localhost:3306/test111/propertyproperty nameusername valueroot/propertyproperty namepassword valueroot123/property/bean/beans 三、spring自定义标签的引入。 1基本了解。 spring默认提供了一些标准的XML标签如bean、beans、import等用于常见的配置需求。然而随着项目复杂度的增加这些标准标签可能无法满足一些特定的配置需求。通过自定义标签开发者可以封装复杂的配置逻辑使XML配置文件更加简洁、易读和易于维护。 简单理解就是除了spring-beans提供的标签外的其他标签。那么我们就需要引入其它类似这样写法的命名空间以及相应的地址。 2引入新的命名空间xmlns:context。 引入新的命名空间的写法具体如下。依葫芦画瓢嘛如果找不到可以去对应需要引入的技术的官网寻找对应的写法然后直接复制粘贴。 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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!-- 管理DruidDataSource对象--bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/propertyproperty nameurl valuejdbc:mysql://localhost:3306/test111/propertyproperty nameusername valueroot/propertyproperty namepassword valueroot123/property/bean/beans 3使用context空间加载properties文件。 1、resources目录下新建properties文件。 数据库驱动、url、用户名、密码每一个都独自占一行。写法键名键值。 2、使用标签context:property-placeholder。 当引入新的命名空间后简单的就可以显示大多数可以可以使用的标签。这就是spring自定义标签的扩展性好处。 使用属性location指定properties文件的地址。 context:property-placeholder locationjdbc.properties/ 3、使用属性占位符${}读取properties文件里对应属性值。 到这一步基本上就是完成了。下面采用另外一个案例进行测试。 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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.xsdcontext:property-placeholder locationjdbc.properties/!-- 管理DruidDataSource对象--bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driverClassName}/propertyproperty nameurl value${jdbc.url}/propertyproperty nameusername value${jdbc.username}/propertyproperty namepassword value${jdbc.password}/property/bean/beans 四、加载properties文件的测试。 1dao层。 BookDao接口。 package com.fs.dao;public interface BookDao {/*** dao层save方法*/void save();
}BookDaoImpl实现类。 package com.fs.dao.impl;import com.fs.dao.BookDao;public class BookDaoImpl implements BookDao {private String name;public void setName(String name) {this.name name;}Overridepublic void save() {System.out.println(book dao save...);/*** 测试properties文件中的属性值是否成功注入*/System.out.println(name注入的值name);}
}2spring配置文件中配置BookDaoImpl实现类的bean。 对name属性注入采用setter方式注入。其次注入的属性值采用占位符${}读取properties文件里对应属性值。(测试) bean idbookDao classcom.fs.dao.impl.BookDaoImplproperty namename value${jdbc.driverClassName}/property
/bean3测试类。 获取Ioc容器通过指定id获取对应bean。调用BookDaoImpl的save方法完成测试。 package com.fs.test;import com.fs.dao.impl.BookDaoImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App3 {public static void main(String[] args) {//获取Spring容器读取配置文件ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(springConfig.xml);Object obj context.getBean(bookDao);//强制类型转换BookDaoImpl bookDao (BookDaoImpl) obj;bookDao.save();}
} 测试运行结果如下。 4properties文件中的键名注意与系统变量区分。 如果将jdbc.username改成username。再进行测试。 结果却显示86183这是系统的环境变量的用户。它们之间产生了冲突问题。也就是系统的环境变量的优先级要比当前的程序里的环境要高加载文件后直接替换了当前的属性值而我配置的东西并没有生效 解决方法在spring配置文件中加载properties文件时再设置一个属性。 system-properties-mode。 将该属性值设置成NEVER。表示不加载系统属性。 context:property-placeholder locationjdbc.properties system-properties-modeNEVER/ 现在再测试结果就是成功获取到自己配置的属性值了。 5加载多个properties文件。 1、测试2个properties文件的读取。 jdbc.properties。 jdbc2.properties。 多个properties文件使用逗号相隔。 2、使用*.properties加载所有properties文件。 context:property-placeholder location*.properties system-properties-modeNEVER/ 3、使用classpath:*.properties加载当前工程所有properties文件。规范 注意这种写法只能读取当前工程的配置文件。而对于外部导入的jar包或者某个框架中有properties文件那么这种写法是加载不到的。 context:property-placeholder locationclasspath:*.properties system-properties-modeNEVER/ 4、使用classpath*:*.properties加载所有properties文件。包括外部jar包 这样的写法不仅可以从当前工程读取properties文件还可以读取它所依赖的jar包中读取properties文件。 context:property-placeholder locationclasspath*:*.properties system-properties-modeNEVER/ 五、spring配置文件中加载properties文件几种写法的小结。 最常见且常用的5种写法。