属性文件——Java&Spring
属性文件
什么是属性文件 ?
定义:一个扩展名为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的更多相关文章
- 属性文件操作之Properties与ResourceBundle
1.Properties与ResourceBundle 两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. 2.Propertie ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件
1,配置数据源 (1)添加驱动 (2)编写spring配置文件 <bean id="dataSource" class="org.springframework.j ...
- Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- Spring读取加密属性文件处理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...
- 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)
一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- spring 使用外部属性文件
一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
随机推荐
- Servlet和Tomcat底层源码分析
Servlet 源码分析 Servlet 结构图 Servlet 和 ServletConfig 都是顶层接口,而 GenericServlet 实现了这两个顶层接口,然后HttpServlet ...
- Codeforce-620C
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the rig ...
- CoderForces-913-C
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of gues ...
- HDU3666-THE MATRIX PROBLEM(差分约束-不等式解得存在性判断 对数转化)
You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...
- 【MobX】391- MobX 入门教程(下)
点击上方"前端自习课"关注,学习起来~ 三.MobX 常用 API 介绍 3. 修改可观察数据 在上一部分内容中,我们了解到,对可观察的数据做出反应的时候,需要我们手动修改可观察数 ...
- Orleans 初接触
简介 这篇随笔主要记录了自己学习Orleans的经过和理解,在学习过程中会一直更新,思路和理解可能有些偏颇,如果有幸有大佬看到这篇文章,希望能给予批评指正. 导航 (一) 入门例子 (二) 测试用例 ...
- ES6,箭头函数 (=>)注意点
函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. 不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误. 不可以使用arguments对象,该对象在函数体内不存 ...
- docker-网络模式
Docker自身的4种网络工作方式,和一些自定义网络模式 安装Docker时,它会自动创建三个网络,bridge(创建容器默认连接到此网络). none .host host:容器将不会虚拟出自己的网 ...
- Rancher 2.3实现K8S一键式升级!再也不用同步升级Rancher啦!
在Rancher 2.3之前,Rancher的新版本总是随着Kubernetes的新版本一起发布,如果你想要使用最新版本的Kubernetes,那么你需要先升级Rancher才能使用.Rancher ...
- [ASP.NET Core 3框架揭秘] 依赖注入[6]:服务注册
通过<利用容器提供服务>我们知道作为依赖注入容器的IServiceProvider对象是通过调用IServiceCollection接口的扩展方法BuildServiceProvider创 ...