属性文件
什么是属性文件 ?
定义:一个扩展名为properties文件,属性文件都是以key-value(键值对)来保存文件的内容,如:log4j.properties,db.properties等。
oracle.driverClassName=oracle.jdbc.driver.OracleDriver oracle.dburl=jdbc:oracle:thin:@localhost:1521:orcl oracle.username=dang oracle.psw=yeshicheng
属性文件的应用场景?
企业主要作用和应用范围举例:
1.日志文件信息,一般取名为log4j.properties ;
2 数据库连接信息,一般取名为db.properties;
3.数据源的配置信息(连接池相关信息)
Java如何读取属性文件?
第一步.读取属性文件第一步,创建一个属性文件对象【Properties properties = new Properties();】
第二步.把Resource属性文件转换为一个流对象【有两种方式:1.通过文件输入字节流;2.通过反射技术。】
第三步.向属性文件对象赋值【有两种方式赋值:setProperty(key, value)直接赋值和load(is)一次性加载赋值。】
第四步.获取属性文件中某一个属性的值【getProperty(key)】
package spring201909; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class TestReadProperties { public static void main(String[] args) { try { // 第一步.读取属性文件第一步 创建一个属性文件对象 //Properties保存数据和读取数据的底层方式和Hashmap Hashtable一样 //都是key-value方式来保存和通过key来读取数据 Properties properties = new Properties(); //第二步.把Resource属性文件转换为一个流对象 //getResourceAsStream中的Resource资源在本例子中指的是属性文件 //当然,在别的例子中还可以是xml,txt等多种资源文件 //BufferedInputStream就是提高读取效率缓冲字节输入流,是输入字节流的包装类 //第一种方法:根据属性文件的路径找到属性文件,并且封装为文件输入字节流 //InputStream is= // new BufferedInputStream(new FileInputStream("config/properties/db.properties")); //第二种方法:通过反射中,类对象的getResourceAsStream把资源转换为流对象 InputStream is = new BufferedInputStream(TestReadProperties.class.getResourceAsStream( "/properties/db.properties")); //第三步.向属性文件对象赋值 // properties.setProperty(key, value)直接赋值异常麻烦而且工作量大 // load等于多次调用setProperty这个方法 把is流对象代表的db.properties文件中的全部key和value一次次的保存到属性文件中 properties.load(is); // 第四步.获取属性文件中某一个属性的值 System.out.println(properties.getProperty("oracle.driverClassName")); } catch (IOException e) { e.printStackTrace(); } } }
如何在Spring容器中使用属性文件?
第一步:加载db.properties属性文件
1.1 首先改变头文件,增加支持context标签的spring-context.xsd文件和xml命名空间【xmlns:context="http://www.springframework.org/schema/context"】【http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd】
1.2 添加context标签,将properties文件加载到上下文环境中【<context:property-placeholder location="classpath:properties/db.properties"/>】
第二步:通过表达式获取属性文件中的值,实现注入【${key}】
<?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"> <!-- 如何让spring容器来帮助baseDao完成属性文件和baseDao中的属性对象properties关联起来 --> <!-- 第一步:加载db.properties属性文件 1.1 首先改变头文件,增加支持context标签的spring-context.xsd文件【xmlns:context="http://www.springframework.org/schema/context"】 1.2 添加context标签,将properties文件加载到上下文环境中【<context:property-placeholder location="classpath:properties/db.properties"/>】 classpath:代表对应编译之后的目录 如果是java工程代表bin目录,如果是web工程 WebRootWEB-INFclasses目录 properties/db.properties:代表准备从bin目录或者classes目录找到properties/db.properties --> <context:property-placeholder location="classpath:properties/db.properties"/> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 支持一次性导入多个属性文件,一般而言就直接一次性多个导入即可 --> <value>classpath:properties/db.properties</value> <value>classpath:properties/log4j.properties</value> </list> </property> </bean> <!-- 第二步:通过这个dao来完成属性对象的注入 --> <bean id="studentJdbcDaoImpl" class="com.yijng.dao.impl.StudentJdbcDaoImpl"> <property name="properties"> <props> <prop key="driverClassName">${oracle.driverClassName}</prop> <prop key="dburl">${oracle.dburl}</prop> <prop key="username">${oracle.username}</prop> <prop key="psw">${oracle.psw}</prop> <prop key="log4j">${log4j.infolevel}</prop> </props> </property> </bean> </beans>