属性文件

什么是属性文件 ?

定义:一个扩展名为properties文件,属性文件都是以key-value(键值对)来保存文件的内容,如:log4j.properties,db.properties等。

oracle.driverClassName=oracle.jdbc.driver.OracleDriver
oracle.dburl=jdbc:oracle:thin:@localhost::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工程 WebRoot\WEB-INF\classes目录
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>

属性文件——Java&Spring的更多相关文章

  1. 属性文件操作之Properties与ResourceBundle

    1.Properties与ResourceBundle 两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. 2.Propertie ...

  2. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件

    1,配置数据源 (1)添加驱动 (2)编写spring配置文件 <bean id="dataSource" class="org.springframework.j ...

  4. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  5. Spring读取加密属性文件处理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  6. 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

  7. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  8. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  9. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

随机推荐

  1. docker等文档

    docker strapi koa express

  2. 设计模式GOF23(创建型模式)

    • 创建型模式:  单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式.   • 结构型模式: –适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.   • 行为型模式: 模 ...

  3. [TimLinux] Django 信号

    1. 信号定义 django包含有一个“信号分发器”,在框架内任何时候,在任何地方,有动作发生时,用来帮助解耦应用之间获取通知.简言之,信号允许特定的发送者通知一系列接收者某一特定动作已经发生了.特别 ...

  4. UVA-136Ugly numbers

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  5. 最全的linux系统安装教程和排错方法

    第4章 linux信息和系统安装与连接    260 4.1 linux的发展历史    260 4.2 GPL协议,FSF协议    261 4.3 linux系统的安装    261 4.3.1 ...

  6. 使用ExcelPackage进行Excel报表

    Nuget包名为 epplus.core 命名空间OfficeOpenXml string localFileName = path + Path.DirectorySeparatorChar + f ...

  7. 16个Spring注解,你知道的有几个?

    @Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.   @RestController Spring4之后加入的注解,原来在@Co ...

  8. OS OSTEP (Operating Systems Three Easy pieces 操作系统导论 )

    读<OSTEP>的一点重点记录与感悟 (未完) Chapter-2 第二章 1. 操作系统的设计目标:  抽象.高性能.保护.不间断运行. 抽象:建立一些“抽象”,让操作系统方便和易于使用 ...

  9. Leetcode 42 接雨水 双指针

    地址 https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能 ...

  10. PythonI/O进阶学习笔记_8.python的可迭代对象和迭代器、迭代设计模式

     content: 1.什么是迭代协议 2. 什么是迭代器(Iterator)和可迭代对象(Iterable) 3. 使用迭代器和可迭代对象 4. 创建迭代器和可迭代对象 5. 迭代器设计模式   一 ...