[Spring5]IOC容器_Bean管理XML方式_外部属性文件
生活随笔
收集整理的這篇文章主要介紹了
[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"><!-- <!–直接配置連接池–>--> <!-- <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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cma准考信打印地址在哪cma打印准考证
- 下一篇: xml可以html标签吗,自定义html