图书类网站建设策划书,烟台龙口网站建设,网站开发技术主管工作职责,wordpress如何在地址栏中加网站logo需求:将数据库连接四要素提取到properties配置文件#xff0c;spring来加载配置信息并使用这些信息来完成属性注入。第三方bean属性优化的思路如下#xff1a;
1.在resources下创建一个jdbc.properties(文件的名称可以任意)
2.将数据库连接四要素配置到配置文件中
3.在Spr…需求:将数据库连接四要素提取到properties配置文件spring来加载配置信息并使用这些信息来完成属性注入。第三方bean属性优化的思路如下
1.在resources下创建一个jdbc.properties(文件的名称可以任意)
2.将数据库连接四要素配置到配置文件中
3.在Spring的配置文件中加载properties文件
4.使用加载到的值实现属性注入
其中第34步骤是需要重点关注具体是如何实现。
实现步骤
步骤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/beans
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xmlns:contexthttp://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/springcontext.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/beans
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xmlns:contexthttp://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/springcontext.xsd
context:property-placeholder locationjdbc.properties/
bean iddataSource classcom.alibaba.druid.pool.DruidDataSource
property namedriverClassName value${jdbc.driver}/
property nameurl value${jdbc.url}/
property nameusername value${jdbc.username}/
property namepassword value${jdbc.password}/
/bean
/beans至此读取外部properties配置文件中的内容就已经完成。