日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[Spring5]IOC容器_Bean管理XML方式_外部属性文件

發(fā)布時(shí)間:2023/12/4 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Spring5]IOC容器_Bean管理XML方式_外部属性文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IOC操作Bean管理(外部屬性文件)

1.直接配置數(shù)據(jù)庫信息

(1)配置德魯伊druid連接池

(2)引入德魯伊druid連接池依賴jar包

<!--直接配置連接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value=""></property><property name="url" value=""></property><property name="username" value=""></property><property name="password" value=""></property></bean>

填入數(shù)據(jù)庫信息:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--直接配置連接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/db3"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean></beans>

2.引入外部屬性文件配置數(shù)據(jù)庫連接池

(1)創(chuàng)建外部屬性文件,properties格式文件,寫數(shù)據(jù)庫信息

prop.driverClass=com.mysql.cj.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/db3 prop.userName=root prop.password=8888.216

(2)把外部properties屬性文件引入到spring配置文件中

a.引入context名稱空間

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"></beans>

b.在spring配置文件使用標(biāo)簽引入外部屬性文件

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- &lt;!&ndash;直接配置連接池&ndash;&gt;--> <!-- <bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource">--> <!-- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>--> <!-- <property name="url" value="jdbc:mysql://localhost:3306/db3"></property>--> <!-- <property name="username" value="root"></property>--> <!-- <property name="password" value="8888.216"></property>--> <!-- </bean>--><!--引入外部屬性文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置連接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${prop.driverClass}"></property><property name="url" value="${prop.url}"></property><property name="username" value="${prop.userName}"></property><property name="password" value="${prop.password}"></property></bean></beans>

總結(jié)

以上是生活随笔為你收集整理的[Spring5]IOC容器_Bean管理XML方式_外部属性文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。