Spring_IOC容器:xml方式-外部属性文件(Bean管理)

1.直接配置数据库信息

(1)配置德鲁伊链接池

(2)引入德鲁伊连接处依赖的jar包 druid-1.2.9.jar    mysql-connector-java-8.0.28.jar

      <!--直接配置链接池-->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql://localhost/shooldb"></property>
       <property name="username" value="root"></property>
       <property name="password" value="root"></property>
   </bean>

 

2.引用外部属性文件配置数据库连接池

(1)创建外部属性文件,properties格式文件,写数据库信息

(2)把外部properties属性文件引入Spring配置文件中

-----引入context名称空间

 xmlns:context="http://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/beans/spring-context.xsd">

-----在Spring配置文件使用标签引入外部属性文件

    <!--引入外部属性文件-->
    <context:property-placeholder location="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>

 

阅读剩余
THE END