微网站价格,湖南省建设厅气源适配性目录2022,摄影师网站html5,图标不显示wordpressSpring框架可以通过Spring的配置文件完成两个数据源druid和C3P0的配置#xff08;Spring#xff1a;IOC/DI配置管理第三方bean#xff09;#xff0c;但是其中包含了一些问题#xff0c;我们来分析下:
这两个数据源中都使用到了一些固定的常量如数据库连接四要素#xf…Spring框架可以通过Spring的配置文件完成两个数据源druid和C3P0的配置SpringIOC/DI配置管理第三方bean但是其中包含了一些问题我们来分析下:
这两个数据源中都使用到了一些固定的常量如数据库连接四要素把这些值写在Spring的配置文件中不利于后期维护需要将这些值提取到一个外部的properties配置文件中
Spring框架如何从配置文件中读取属性值来配置呢
实现思路 需求:将数据库连接四要素提取到properties配置文件spring来加载配置信息并使用这些信息来完成属性注入。 1.在resources下创建一个jdbc.properties(文件的名称可以任意) 2.将数据库连接四要素配置到配置文件中 3.在Spring的配置文件中加载properties文件 4.使用加载到的值实现属性注入 步骤1:准备properties配置文件
resources下创建一个jdbc.properties文件,并添加对应的属性键值对
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://127.0.0.1:3306/spring_db
jdbc.usernameroot
jdbc.passwordroot步骤2:开启context命名空间
在applicationContext.xml中开context命名空间
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
/beans步骤3:加载properties配置文件
在配置文件中使用context命名空间下的标签来加载properties配置文件
context:property-placeholder locationjdbc.properties/步骤4:完成属性注入
使用${key}来读取properties配置文件中的内容并完成属性注入
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
!-- 1.开启context命名空间--
!-- 2.使用context空间加载properties文件--!--方式1 context:property-placeholder locationjdbc.properties system-properties-modeNEVER/--
!--方式2 context:property-placeholder locationjdbc.properties,jdbc2.properties system-properties-modeNEVER/--
!-- classpath:*.properties 设置加载当前工程类路径中的所有properties文件--
!-- system-properties-mode属性是否加载系统属性--!--方式3 context:property-placeholder location*.properties system-properties-modeNEVER/--!--方式4 classpath*:*.properties 设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件--context:property-placeholder locationclasspath*:*.properties system-properties-modeNEVER/!-- 3.使用属性占位符${}读取properties文件中的属性--
!-- 说明idea自动识别${}加载的属性值需要手工点击才可以查阅原始书写格式--bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//beanbean idbookDao classcom.itheima.dao.impl.BookDaoImplproperty namename value${jdbc.url}//bean/beans说明:
方式一:可以实现如果配置文件多的话每个都需要配置方式二:*.properties代表所有以properties结尾的文件都会被加载可以解决方式一的问题但是不标准方式三:标准的写法classpath:代表的是从根路径下开始查找但是只能查询当前项目的根路径方式四:不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的properties配置文件context:property-placeholder/标签会加载系统的环境变量而且环境变量的值会被优先加载,因此增加system-properties-mode:设置为NEVER,表示不加载系统属性
至此读取外部properties配置文件中的内容就已经完成进行测试
public class App {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext4.xml);DataSource dataSource (DataSource) ctx.getBean(dataSource);System.out.println(dataSource);}
}运行结果
加载properties文件小结
本节主要讲解的是properties配置文件的加载需要掌握的内容有: 如何开启context命名空间 如何加载properties配置文件 context:property-placeholder location system-properties-modeNEVER/如何在applicationContext.xml引入properties配置文件中的值 ${key}[说明]内容主要来源黑马程序员网上资源学习