1,Spring使用property文件作为配置源
工程中难免出现一些需要每次部署都需要配置的参数,如数据源连接参数等,测试环境跟实际运行环境是不一样的。
使用spring框架的话,这些参数可能独立分布在不同的springContex配置文件里面。
可以考虑将这些参数独立到一个配置文件并可以让spring方便加载注入。可选的一个方案是使用java的property文件,将所有的配置参数都写到property文件里面,使用${key}来在spring配置文件里面得到这个参数。
例子:
property文件global-config-file.properties:
#FOR dataSource jdbc.dataSource.url=jdbc:postgresql://192.168.1.118:5432/DB_name jdbc.dataSource.username=postgres jdbc.dataSource.password=123
示例配置的是数据源参数。
之后在springContext的配置文件中,加入下面代码,:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="classpath:global-config-file.properties"/> </bean>
即引入PropertyPlaceholderConfigurer来读取property配置文件,spring框架会从其那里获取到需要的配置参数。
之后再用${key}的格式取代你数据源配置参数:
<bean id="myBatisDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.postgresql.Driver"> </property> <property name="url" value="${jdbc.dataSource.url}"> </property> <property name="username" value="${jdbc.dataSource.username}"></property> <property name="password" value="${jdbc.dataSource.password}"></property> </bean>
例如:${jdbc.dataSourcurl}:框架会将global-config-file.properties读到jdbc.dataSource.url的值“jdbc:postgresql://192.168.1.118:5432/DB_name”填入${jdbc.dataSource.url}所在的位置。
2,将property配置文件与工程分离(独立到服务器中)
为什么想到要这样做呢?
这是在实际开发部署中,每次将工程打包部署到服务器,都需要在服务器端修改工程的配置项(如数据源等),感觉很麻烦。
如果工程优先读取放置在服务器上的配置文件,没有此配置文件才从工程目录里面读取,那样就可以将只在服务器存放一份配置文件并修改成与服务器运行环境一致,今后部署即可不用每次都修改工程配置文件了。
2.1 做法:
刚才我们在第1点添加了一个propertyConfigurer,现将其改名为“propertyConfigurer2”,并添加一个“propertyConfigurer1”,两者配置为:
<bean id="propertyConfigurer1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1"/> <property name="ignoreResourceNotFound" value="true"/> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="file:C:/tempSys/config/global-config-file.properties"/> </bean> <bean id="propertyConfigurer2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="2"/> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="location" value="classpath:global-config-file.properties"/> </bean>
order属性表示加载顺序,这种配置表示先加载propertyConfigurer1,在加载propertyConfigurer2,两者配置文件中有同样的参数,后加载的参数不会覆盖先加载的。
propertyConfigurer1需要添加一个 <property name="ignoreResourceNotFound" value="true"/>属性,表示当找不到这个配置文件时,则跳过,这样就不会抛出FileNotFoundException了。
这样配置之后,工程会在启动时,首先检查C:/tempSys/config/global-config-file.properties是否存在,如果存在则加载器配置,随后再加载项目目录下的global-config-file.properties;如果不存在,则直接加载项目目录下的global-config-file.properties。
这样做不单可将配置与开发环境分离出来,避免每次部署时繁琐的配置修改工作。同时这也提高了系统部署时的安全性,比如,实际运行的正式数据库账户密码信息将是十分机密的信息,由运维人员保管维护,可能对开发人员都不能公开(开发人员只知道开发测试数据库的信息),将这些信息直接配置在服务器上,服务器也由运维人员进行管理,这样可以减少机密信息的知晓人群,提高安全性。
相关推荐
spring配置文件实例
Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合
spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件...
使用Spring配置文件实现AOP
spring 配置文件 归类 IOC AOPspring 配置文件 归类 IOC AOP
spring 配置 文件
服务Spring利用依赖注入机制来简化工作同时提高易测性Springbeans及依赖以及beans类所需服务都在配置 文件中进行了详细介绍说明这个配置文件是典型XML格式但是它既冗长又不实用对于需要定义大量 Springbeans大工程来...
spring配置文件详解,Spring的applicationContext.xml文件代码:
Spring5.0自定义配置文件便签[Maven]工程可运行【spring-customize-tag】
spring配置文件详细介绍 通过对xml配置的熟练掌握 相信spring真的太简单
基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...
Spring@PropertySource 配置文件自定义加密、自定义Yaml文件加载
本文档是很专业的 Spring动态加载配置文件,实现热部署方案。
spring配置文件加密实现
初学Spring的小伙伴,还在为找不到主配置文件而着急吗,该配置文件是基于Spring4.10版本的,不用再自己到官网页面上到处找了,分享给大家,也希望大家别再像我一样因为这个坑耽误了大半天的时间,如果要自己配置也...
Spring4.3.7的log4j以及配置文件Spring4.3.7的log4j以及配置文件Spring4.3.7的log4j以及配置文件
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
Spring配置文件:注入各种数据时配置文件的配置格式
主要介绍了Spring Boot自定义配置属性源(PropertySource),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring配置文件详解,交你如何理解spring,熟练运用spring