属性文件

什么是属性文件 ?

定义:一个扩展名为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. LightOJ1355 Game Of CS(green 博弈)

    Jolly and Emily are two bees studying in Computer Science. Unlike other bees they are fond of playin ...

  2. 【广州.NET社区推荐】【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性

    原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...

  3. 【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性

    原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...

  4. Linux环境(服务器)下非root用户安装Python3.6

    Linux环境(服务器)下非root用户安装Python3.6 在管理实验室集群时候,遇到的问题--非root用户在搭建自己环境时候,如何搭建. 注意: root用户的根目录是root,非root用户 ...

  5. JavaEE基础(05):过滤器、监听器、拦截器,应用详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.Listener监听器 1.概念简介 JavaWeb三大组件:Servlet,Listener,Filter.监听器就是指在应用程序中监听 ...

  6. docker初体验:Docker部署SpringCloud项目eureka-server

    Docker部署SpringCloud项目eureka-server 1 创建eureka-server工程 创建父工程cloud-demo,其pom.xml如下: <?xml version= ...

  7. 【Eureka】集群搭建

    [Eureka]集群搭建 转载============================================== ====================================== ...

  8. 一线大厂Java面试必问的2大类Tomcat调优

    一.前言 最近整理了 Tomcat 调优这块,基本上面试必问,于是就花了点时间去搜集一下 Tomcat 调优都调了些什么,先记录一下调优手段,更多详细的原理和实现以后用到时候再来补充记录,下面就来介绍 ...

  9. vue中子组件直接修改父组件prop属性bug

    在有些时候,子组件直接修改父组件传来的 prop 对象的属性会出现不同步的问题. 比如,父组件传过来的一个对象 checkBoxObj: checkBoxObj:{ checked: false } ...

  10. Python面向对象-定制方法

    Python中的class可以定义许多定制方法,可以让我们方便的生成特定的类. 我们之前介绍了__slots__.__len__(),python中还有许多这样的特殊函数: __str__ >& ...