天天看點

IOC/DI配置管理第三方bean

作者:tianlongbabu

案例:資料源對象管理

  1. 實作Druid管理

步驟1:導入druid的依賴

pom.xml中添加依賴

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>           

步驟2:配置第三方bean

在applicationContext.xml配置檔案中添加DruidDataSource的配置

<?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.xsd
            http://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.jdbc.Driver"/>

            <property name="url" value="jdbc:mysql://localhost:3306/world"/>

            <property name="username" value="root"/>

            <property name="password" value="caicai123"/>
            </bean>

</beans>           

說明:

  • driverClassName:資料庫驅動
  • url:資料庫連接配接位址
  • username:資料庫連接配接使用者名
  • password:資料庫連接配接密碼 資料庫連接配接的四要素要和自己使用的資料庫資訊一緻。

步驟3:從IOC容器中擷取對應的bean對象

package com.itheima;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      

    }
}           

2.實作C3P0管理

步驟1:導入C3P0的依賴

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>           

對于新的技術,不知道具體的坐标該如何查找?

直接百度搜尋 從mvn的倉庫https://mvnrepository.com/中進行搜尋

步驟2:配置第三方bean

在applicationContext.xml配置檔案中添加配置

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

    <property name="driverClass" value="com.mysql.jdbc.Driver"/>

    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/world"/>

    <property name="user" value="root"/>

    <property name="password" value="caicai123"/>

    <property name="maxPoolSize" value="1000"/>
</bean>           

注意:

  • ComboPooledDataSource的屬性是通過setter方式進行注入
  • 想注入屬性就需要在ComboPooledDataSource類或其上層類中有提供屬性對應的setter方法
  • C3P0的四個屬性和Druid的四個屬性是不一樣的

注意:

  • 資料連接配接池在配置屬性的時候,除了可以注入資料庫連接配接四要素外還可以配置很多其他的屬性,具 體都有哪些屬性用到的時候再去查,一般配置基礎的四個,其他都有自己的預設值
  • Druid和C3P0在沒有導入mysql驅動包的前提下,一個沒報錯一個報錯,說明Druid在初始化的 時候沒有去加載驅動,而C3P0剛好相反
  • Druid程式運作雖然沒有報錯,但是當調用DruidDataSource的getConnection()方法擷取連 接的時候,也會報找不到驅動類的錯誤

加載properties檔案

第三方bean屬性優化

1.在resources下建立一個jdbc.properties(檔案的名稱可以任意)

2.将資料庫連接配接四要素配置到配置檔案中

3.在Spring的配置檔案中加載properties檔案

4.使用加載到的值實作屬性注入 其中第3,4步驟是需要大家重點關注,具體是如何實作。

實作步驟

步驟1:準備properties配置檔案

resources下建立一個jdbc.properties檔案,并添加對應的屬性鍵值對

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/world
jdbc.username=root
jdbc.password=caicai123           

步驟2:開啟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.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/springcontext.xsd">
</beans>           

步驟3:加載properties配置檔案

<context:property-placeholder location="jdbc.properties"/>           

步驟4:完成屬性注入

使用${key}來讀取properties配置檔案中的内容并完成屬性注入

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    </bean>           

至此,讀取外部properties配置檔案中的内容就已經完成。

讀取單個屬性

對于上面的案例,效果不是很明顯,我們可以換個案例來示範下:

需求:從properties配置檔案中讀取key為name的值,并将其注入到BookDao中并在save方法中 進行列印。

1.在項目中添加BookDao和BookDaoImpl類

2.為BookDaoImpl添加一個name屬性并提供setter方法

3.在jdbc.properties中添加資料注入到bookDao中列印友善查詢結果

4.在applicationContext.xml添加配置完成配置檔案加載、屬性注入(${key})

實作步驟

步驟1:在項目中添對應的類

BookDao和BookDaoImpl類,并在BookDaoImpl類中添加name屬性與setter方法

public interface BookDao {
   public void save();
}


public class BookDaoImpl implements BookDao {
   private String name;

   public void setName(String name) {
       this.name = name;
  }


   public void save() {
       System.out.println("book dao save ..." + name);
  }
}           

步驟2:完成配置檔案的讀取與注入

在applicationContext.xml添加配置,bean的配置管理、讀取外部properties、依賴注入:

<?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.xsd

                   http://www.springframework.org/schema/context

                   http://www.springframework.org/schema/context/springcontext.xsd">


           
           <context:property-placeholder location="jdbc.properties"/>

           
           <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">

       <property name="name" value="${jdbc.driver}"/>

   </bean>
        </beans>
           

注意事項

問題一:鍵值對的key為username引發的問題

出現問題的原因是-placeholder/>标簽會加載系統的環境變量,而且環境 變量的值會被優先加載,如何檢視系統的環境變量?

public static void main(String[] args) throws Exception{

Map<String, String> env = System.getenv();

System.out.println(env);

}

解決方案

location="jdbc.properties" systemproperties-mode="NEVER"/>

system-properties-mode:設定為NEVER,表示不加載系統屬性,就可以解決上述問題。 當然還有一個解決方案就是避免使用username作為屬性的key。

<?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.xsd

                   http://www.springframework.org/schema/context

                   http://www.springframework.org/schema/context/springcontext.xsd">


           
           <context:property-placeholder location="jdbc.properties" systemproperties-mode="NEVER"/>
        </beans>           

問題二:當有多個properties配置檔案需要被加載,該如何配置?

1.調整下配置檔案的内容,在resources下添加jdbc.properties , jdbc2.properties ,内容如 下: jdbc.properties

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db

jdbc.username=root

jdbc.password=root           

jdbc2.properties

username=root666           

2.修改applicationContext.xml

<?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.xsd

                   http://www.springframework.org/schema/context

                   http://www.springframework.org/schema/context/springcontext.xsd">


           <!--方式一 -->
<context:property-placeholder

location="jdbc.properties,jdbc2.properties" system-propertiesmode="NEVER"/>


           <!--方式二-->

           <context:property-placeholder location="*.properties" systemproperties-mode="NEVER"/>


           <!--方式三 -->

           <context:property-placeholder location="classpath:*.properties"

                                         system-properties-mode="NEVER"/>

           <!--方式四-->

           <context:property-placeholder location="classpath*:*.properties"

                                         system-properties-mode="NEVER"/>
        </beans> 
           

說明:

方式一:可以實作,如果配置檔案多的話,每個都需要配置

方式二: *.properties代表所有以properties結尾的檔案都會被加載,可以解決方式一的問 題,但是不标準

方式三:标準的寫法,classpath:代表的是從根路徑下開始查找,但是隻能查詢目前項目的根 路徑

方式四:不僅可以加載目前項目還可以加載目前項目所依賴的所有項目的根路徑下的

properties配置檔案

繼續閱讀