Spring 筆記1:IOC及XML方式&依賴注入

前言

Spring框架的學習路線:php

  1. Spring第一天:Spring的IOC容器之XML的方式,Spring框架與Web項目整合
  2. Spring次日:Spring的IOC容器之註解的方式,Spring的AOP技術
  3. Spring第三天:Spring的事務管理、Spring框架的JDBC模板
  4. Spring第四天:SSH三大框架的整合

這是第一天學習大綱:html

1、 Spring框架的概述

1.1 技術分析之什麼是Spring框架

一、Spring框架的概述:(更詳細參考百度百科 springjava

  • Spring是一個開源框架
  • Spring是於2003 年興起的一個輕量級的Java開發框架,由Rod Johnson在其著做Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。
  • 它是爲了解決企業應用開發的複雜性而建立的。框架的主要優點之一就是其分層架構,分層架構容許使用者選擇使用哪個組件,同時爲 J2EE 應用程序開發提供集成的框架。 Spring使用基本的JavaBean來完成之前只可能由EJB完成的事情。然而,Spring的用途不只限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用均可以 從Spring中受益。
  • Spring的核心是控制反轉(IoC)和面向切面(AOP)。簡單來講,Spring是一個分層的JavaSE/EEfull-stack(一站式) 輕量級開源框架。

引用百度百科上一段介紹:web

Rod Johnson在2002年編著的《Expert one on one J2EE design and development》一書中,對Java EE 系統框架臃腫、低效、脫離現實的種種現狀提出了質疑,並積極尋求探索革新之道。以此書爲指導思想,他編寫了interface21框架,這是一個力圖衝破J2EE傳統開發的困境,從實際需求出發,着眼於輕便、靈巧,易於開發、測試和部署的輕量級開發框架。Spring框架即以interface21框架爲基礎,通過從新設計,並不斷豐富其內涵,於2004年3月24日,發佈了1.0正式版。同年他又推出了一部堪稱經典的力做《Expert one-on-one J2EE Development without EJB》,該書在Java世界掀起了軒然大波,不斷改變着Java開發者程序設計和開發的思考方式。在該書中,做者根據本身多年豐富的實踐經驗,對EJB的各類笨重臃腫的結構進行了逐一的分析和否認,並分別以簡潔實用的方式替換之。至此一戰功成,Rod Johnson成爲一個改變Java世界的大師級人物。spring

解釋:最先作 Java 開發採用的是 sun 公司提供的 EJB 規範作開發,最開始沒那麼多框架。Rod Johnson 對開發過程諸如低效、臃腫、脫離現實種種現狀提出質疑,並積極尋求探索革新之道,後來就有了 spring 框架。數據庫

二、EE開發分紅三層結構apache

  • WEB層 -- Spring MVC 業務層 -- Bean管理:(IOC) 持久層 -- Spring的 JDBC 模板. ORM模板用於整合其餘的持久層框架

三、什麼叫full-stack(一站式)開發?編程

首先來講 Java EE 開發通常分三層,WEB 層、業務層、持久層,每一層要乾的事不同。以下:設計模式

Spring 屬於業務層一個框架,那爲何叫一站式開發呢?這樣來解釋,若是 WEB 層你不想使用 Struts2 框架,Spring 框架提供了一個叫 SpringMVC 模塊(屬於 Spring 的一部分),它就能夠當作 WEB 層小框架,處理 web 層事情;若是持久層不想使用 Hibernate 框架,Spring 框架提供了一個叫 SpringJDBC 模板(它能幫忙操做數據庫),這樣它就能當作持久層解決方案。數組

這樣看來,若是我不使用其餘框架,只用 Spring 框架也是能作 Java EE 開發。而且若是你不想使用 SpringMVC 模塊作 WEB 層也是能夠的,仍然可使用 Struts2 框架,由於 Spring 框架能夠幫忙集成和整合 Struts2 框架;若是持久層你不想使用 SpringJDBC 模板,使用其餘一些持久層框架如 Hibernate、Mybatis 也是能夠,Spring 框架也能夠去整合它們。

另外,若是沒有 Spring 框架,只用 Struts2 開發也是沒問題,但可能效率等方面就不是很好了。但有了 Spring 框架,Struts2 會更牛,它的管理可能更方便、開發更簡單。簡單來講:只用 Struts2 去開發也能夠,可是有了 Spring 框架開發會更好。

再次小結:若是沒有 Spring 框架也能作企業級開發,但有了 Spring,作開發更規範,程序擴展性和維護等方面更好。-----> 它能解決企業級開發的複雜性。

另外關於 Spring 框架,咱們再來了解下它與 Spring boot、Spring Clound 的區別:Spring SpringMVC SpringBoot SpringCloud概念、關係及區別

1.2 技術分析之Spring框架的特色

一、 爲何要學習Spring的框架

  • 方便解耦,簡化開發
    • Spring就是一個大工廠,能夠將全部對象建立和依賴關係維護,交給Spring管理
  • AOP編程的支持
    • Spring提供面向切面編程,能夠方便的實現對程序進行權限攔截、運行監控等功能
  • 聲明式事務的支持
    • 只須要經過配置就能夠完成對事務的管理,而無需手動編程
  • 方便程序的測試
    • Spring對Junit4支持,能夠經過註解方便的測試Spring程序
  • 方便集成各類優秀框架
    • Spring不排斥各類優秀的開源框架,其內部提供了對各類優秀框架(如:Struts二、Hibernate、MyBatis、Quartz等)的直接支持
  • 下降JavaEE API的使用難度
    • Spring 對JavaEE開發中很是難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大下降

二、Spring框架的版本:

  • Sring3.x 和 Spring4.x 的版本

2、SpringIOC的快速入門

一、什麼是 IOC 的功能?

  • IoC(Inverse of Control):控制反轉,將對象的建立權反轉給Spring!!
  • 使用 IOC 能夠解決的程序耦合性高的問題!!

如上圖,解釋一下。

假如要作一個保存客戶信息的功能,那麼咱們在代碼中可能要在 service 層 new 一個類,再到 dao 層 new 一個類(類比上圖即資源),能夠看到,資源的建立權利來自該功能。其實這樣不是不能夠,只是很差,過分耦合了。有了 Spring 框架,那如今能夠交給 Spring 框架了。

IOC 的編寫過程:(先要了解工廠模式,參考 工廠模式——看這一篇就夠了

  • App ---> 工廠 ---> 資源(功能和資源分開了,可是有個問題,資源和工廠還存在耦合)

  • App ---> 工廠 ---> XML文件 ---> 資源

    在 xml 文件中配置,配置什麼,就建立什麼。工廠讀取 xml 文件,工廠負責生產這些對象。程序中用到對象就到工廠拿就能夠。

IOC 的底層實現原理:

編寫步驟:

1. 步驟一:下載Spring框架的開發包
    * 官網:http://spring.io/
    * 下載地址:http://repo.springsource.org/libs-release-local/org/springframework/spring 解壓出來:(Spring目錄結構:)
        * docs      -- API和開發規範
        * libs      -- jar包和源碼
        * schema    -- 約束

2. 步驟二:建立JavaWEB項目,引入Spring的開發包
    * 引入Spring框架IOC核心功能須要的具體的jar包
        * Spring框架的IOC的功能,那麼根據Spring框架的體系結構圖能看到,只須要引入以下的jar包
            * Beans
            * Core
            * Context
            * Expression Language

        * Spring框架也須要引入日誌相關的jar包
            * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
                * com.springsource.org.apache.commons.logging-1.1.1.jar

            * 還須要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15
                * com.springsource.org.apache.log4j-1.2.15.jar

3. 步驟三:建立對應的包結構,編寫Java的類,要注意:之後使用Spring框架作開發,都須要來編寫接口與實現類!!
    * com.itcast.demo1
        * UserService           -- 接口
        * UserServiceImpl       -- 具體的實現類

4. 步驟四:想把UserServiceImpl實現類的建立交給Spring框架來管理,須要建立Spring框架的配置文件,完成配置
    * 在src目錄下建立applicationContext.xml的配置文件,名稱是能夠任意的,可是通常都會使用默認名稱!!

    * 引入spring的約束,須要先找到具體的約束頭信息!!
        * spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
        * 具體的約束以下:      
            <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">
            </beans>

    * 完成UserService的配置
        <!-- Spring的快速入門 -->
        <bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>

5. 步驟五:編寫測試程序,採用Spring框架的工廠方式來獲取到UserService接口的具體實現類!!
    public void demo2(){
        // 使用Spring的工廠:
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 經過工廠得到類:
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHello();
    }
複製代碼

關於第①步: 相關 Spring 文件有以下:

但咱們用到的核心的是 spring-framework-4.2.4.RELEASE-dist.zip 該文件,把其解壓出來能夠看到存在 docs文件夾(文檔) 和 scheme文件夾(約束);另一個文件 spring-framework-3.0.2.RELEASE-dependencies.zip是有關依賴包的文件,如文件上傳、鏈接池、日誌相關包。

關於第②步:

看上圖上半部分 Spring Framework Runtime,能夠看到核心容器下有四個模塊,上面其餘功能都依賴該四個模塊。因此只要導入四個包就能夠。(在這裏,另外導入了spring-framework-3.0.2.RELEASE-dependencies.zip中兩個依賴包,第一個爲日誌規範,第二個爲日誌實現)

關於日誌,其實還少了一個配置文件 log4j.properties,內容:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout
複製代碼

把日誌配置文件粘貼到項目 src 目錄下。

注:log4j.rootLogger=info, stdout 中 info 表示級別,stdout 表示向控制檯輸出。如 info 可改成 off 則不會在輸出日誌信息,還可設置爲 debug。

代碼演示:

public class Demo1 {
    //建立日誌對象
    private Logger log = Logger.getLogger(Demo1.class);

    @Test
    public void run1() {
        // System.out.println("執行了...");
        log.info("執行了...");
    }
}
複製代碼

結果:

關於第③步:

UserService.java:

public interface UserService {
	public void sayHello();
}
複製代碼

UserServiceImpl.java:

public class UserServiceImpl implements UserService {
	public void sayHello() {
		System.out.println("Hello Spring!");
	}
}
複製代碼

使用原來的方式:

//原來的方式
@Test
public void run1() {
    //建立實現類
    //UserServiceImpl us = new UserServiceImpl(); 
    UserService us = new UserServiceImpl();
    us.sayHello();
}
複製代碼

能夠看到打印輸出:Hello Spring!

關於第④⑤步: 在 src 目錄下建立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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
   	<!-- 使用bean標籤 -->
   	<bean id="userService" class="com.strivebo.demo2.UserServiceImpl">
   		<property name="name" value="小鳳"/>
   	</bean>
</beans>
複製代碼

使用 Spring 框架的方式:

//使用 Spring 框架的方式
@Test
public void run2() {
    // 建立工廠,加載核心配置文件
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    // 從工廠中獲取到對象
    UserServiceImpl usi = (UserServiceImpl) ac.getBean("userService");
    //通常使用以下接口的方式
    //UserService usi = (UserService) ac.getBean("userService");
    // 調用對象的方法執行
    usi.sayHello();
}
複製代碼

一樣也是能輸出:Hello Spring!

1.3 入門總結之Spring框架中的工廠(瞭解)

1. ApplicationContext接口
    * 使用ApplicationContext工廠的接口,使用該接口能夠獲取到具體的Bean對象
    * 該接口下有兩個具體的實現類
        * ClassPathXmlApplicationContext            -- 加載類路徑下的Spring配置文件
        * FileSystemXmlApplicationContext           -- 加載本地磁盤下的Spring配置文件

2. BeanFactory工廠(是Spring框架早期的建立Bean對象的工廠接口)
    * 使用BeanFactory接口也能夠獲取到Bean對象
        public void run(){
            BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
            UserService us = (UserService) factory.getBean("us");
            us.sayHello();
        }

    * BeanFactory和ApplicationContext的區別
        * BeanFactory               -- BeanFactory採起延遲加載,第一次getBean時纔會初始化Bean
        * ApplicationContext        -- 在加載applicationContext.xml時候就會建立具體的Bean對象的實例,還提供了一些其餘的功能
            * 事件傳遞
            * Bean自動裝配
            * 各類不一樣應用層的Context實現
複製代碼

3、IoC容器XML的方式

3.1 入門總結之配置Spring框架編寫XML的提示:

1. 步驟一:先複製, http://www.springframework.org/schema/beans/spring-beans.xsd    
2. 步驟二:打開 Eclipse的Windows-->Preference,搜索XML Catalog,點擊Add按鈕
3. 步驟三:先選擇Location的schema的約束地址
    * E:\class\2016\JavaEE28\day35_Spring框架第一天\資料\spring-framework-4.2.4.RELEASE-schema\beans\spring-beans-4.2.xsd
4. 步驟四:注意:Key type要選擇:Schema location
5. 步驟五:Key把http://www.springframework.org/schema/beans/spring-beans.xsd複製上
複製代碼

3.2 技術分析之Spring框架的Bean管理的配置文件方式

3.3.1 Spring框架中標籤的配置

1. id屬性和name屬性的區別
    * id        -- Bean起個名字,在約束中採用ID的約束,惟一
        * 取值要求:必須以字母開始,可使用字母、數字、連字符、下劃線、句話、冒號  id:不能出現特殊字符

    * name      -- Bean起個名字,沒有采用ID的約束(瞭解)
        * 取值要求:name:出現特殊字符.若是<bean>沒有id的話 , name能夠當作id使用
        * Spring框架在整合Struts1的框架的時候,Struts1的框架的訪問路徑是以/開頭的,例如:/bookAction

2. class屬性          -- Bean對象的全路徑
3. scope屬性          -- scope屬性表明Bean的做用範圍
    * singleton         -- 單例(默認值)
    * prototype         -- 多例,在Spring框架整合Struts2框架的時候,Action類也須要交給Spring作管理,配置把Action類配置成多例!!
    * request           -- 應用在Web項目中,每次HTTP請求都會建立一個新的Bean
    * session           -- 應用在Web項目中,同一個HTTP Session 共享一個Bean
    * globalsession     -- 應用在Web項目中,多服務器間的session

4. Bean對象的建立和銷燬的兩個屬性配置(瞭解)
    * 說明:Spring初始化bean或銷燬bean時,有時須要做一些處理工做,所以spring能夠在建立和拆卸bean的時候調用bean的兩個生命週期方法
    * init-method       -- 當bean被載入到容器的時候調用init-method屬性指定的方法
    * destroy-method    -- 當bean從容器中刪除的時候調用destroy-method屬性指定的方法
        * 想查看destroy-method的效果,有以下條件
            * scope= singleton有效
            * web容器中會自動調用,可是main函數或測試用例須要手動調用(須要使用ClassPathXmlApplicationContext的close()方法)
複製代碼

關於 scope 屬性 爲 singleton(單例)和 prototype(多例)的理解:

像之前的寫程序方式,每次要在 service 得 new XxxDao(),能夠看出,每次發送請求,都得 new 一個對象,這就是多例。(每一個請求都有一個實例對象)

那什麼是單例呢?單例至關於整個運行環境中就這一個實例對象。就好比 new XxxDao() 這個 dao 對象已經建立好了,在內存當中就這一個實例對象。

關於 Spring的 bean 的單例和多例做用域更多介紹參考網上資料:【Spring學習17】bean做用域:單例和多例

在 Spring 裏,經過容器建立的對象默認是singleton單例(這裏要注意的是 singleton 做用域和 GOF 設計模式中的單例是不一樣的) ,單例就是在整個容器的生命週期,只會存在一個共享的bean實例。

若是某個 bean 被標記爲多例,則每次請求使用該對象時,都會建立一個新的 bean 實例。好比將這個 bean 注入到另外一個 bean 中,或者在程序中調用getBean("beanid")方法,都會觸發生成一個新的 bean 實例,至關於 new 的操做。

關於 init-method 和 destory-method 屬性,見代碼演示:

<!-- 使用bean標籤 -->
<bean id="userService" class="com.strivebo.demo2.UserServiceImpl" init-method="init" destroy-method="destory">
    <property name="name" value="小鳳"/>
</bean>
複製代碼

UserServiceImpl.java:

public class UserServiceImpl implements UserService {
	public void sayHello() {
		System.out.println("Hello Spring!");
	}	
	public void init() {
		System.out.println("初始化...");
	}
	public void destory() {
		System.out.println("銷燬...");
	}
}
複製代碼

3.3.2 依賴注入(DI)

什麼是依賴注入?

如上圖,咱們想作一個功能,之前都是 service 層 new Dao().xxx ,能夠看到 service 層須要依賴 dao,這就叫依賴。如今有了 Spring ,service 和 dao 均可以交給 Spring 管理。在建立 service 的時候,發現須要 dao,則會注入一個 dao,那 service 就這樣有了 dao,因而即可以調用 dao 對象的方法。代碼來理解:

UserServiceImpl.java:

public class UserServiceImpl implements UserService {
    private String name;
    public void setName(String name) {
        this.name = name;
    }

    public void sayHello() {
        System.out.println("Hello Spring!" + name);
    }
}
複製代碼

之前方法都是:

public void run1() {
    //建立實現類
    UserServiceImpl us = new UserServiceImpl();
    us.setName("小明");
    us.sayHello();
}
複製代碼

能夠看到輸出:Hello Spring!小明

如今方式——依賴注入方式:

applicationContext.xml:

<bean id="userService" class="com.strivebo.demo2.UserServiceImpl">
    <property name="name" value="小鳳"/>
</bean>
複製代碼

執行:

@Test
public void run2() {
    // 建立工廠,加載核心配置文件
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService usi = (UserService) ac.getBean("userService");
    // 調用對象的方法執行
    usi.sayHello();
}
複製代碼

輸出結果:Hello Spring!小鳳

-------------------------------------------代碼完整演示過程---------------------------------------------

①之前的方式:

CustomerDaoImpl.java:

public class CustomerDaoImpl {
    public void save(){
        System.out.println("我是持久層dao....");
    }
}
複製代碼

CustomerServiceImpl.java:

public class CustomerServiceImpl {
    public void save(){
        System.out.println("我是業務層service....");
        new CustomerDaoImpl().save();
    }	
}
複製代碼

執行:

@Test
public void run1(){	//原始方式
    CustomerServiceImpl cs = new CustomerServiceImpl();
    cs.save();
}
複製代碼

結果:

②依賴注入方式:

CustomerServiceImpl.java:

public class CustomerServiceImpl {
    // 提供成員屬性,提供set方法
    private CustomerDaoImpl customerDao;
    public void setCustomerDao(CustomerDaoImpl customerDao) {
        this.customerDao = customerDao;
    }	
    public void save(){
        System.out.println("我是業務層service....");
        // 原來編寫方式
        // new CustomerDaoImpl().save();
        // Spring的方式
        customerDao.save();
    }
}
複製代碼

applicationContext.xml 文件添加以下:(即把 customeDao 注入到 customerService 中)

<!-- 演示的依賴注入 -->
<bean id="customerDao" class="com.strivebo.demo3.CustomerDaoImpl"/>
<bean id="customerService" class="com.strivebo.demo3.CustomerServiceImpl">
    <property name="customerDao" ref="customerDao"/>
</bean> 	
複製代碼

運行:

@Test
public void run2(){	//Spring 方式
    // 建立工廠,加載配置文件,CustomerDaoImpl建立了,CustomerServiceImpl被建立了,
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    CustomerServiceImpl cs = (CustomerServiceImpl) ac.getBean("customerService");
    cs.save();
}
複製代碼

3.3.3 Spring框架的屬性注入

1. 對於類成員變量,經常使用的注入方式有兩種
    * 構造函數注入
    * 屬性setter方法注入

2. 在Spring框架中提供了前兩種的屬性注入的方式
    1. 構造方法的注入方式,兩步
        * 編寫Java的類,提供構造方法
            public class Car {
                private String name;
                private double money;
                public Car(String name, double money) {
                    this.name = name;
                    this.money = money;
                }
                @Override
                public String toString() {
                    return "Car [name=" + name + ", money=" + money + "]";
                }
            }

        * 編寫配置文件
            <bean id="car" class="com.itheima.demo4.Car">
                <constructor-arg name="name" value="大奔"/>
                <constructor-arg name="money" value="100"/>
            </bean>
		
		* 或者
            <bean id="car" class="com.itheima.demo4.Car">
                <constructor-arg index="0" value="長安奔奔"/>
                <constructor-arg index="1" value="45000"/>
            </bean>

    2. 屬性的setter方法的注入方式
        * 編寫Java的類,提供屬性和對應的set方法便可
        * 編寫配置文件

    3. 若是Java類的屬性是另外一個Java的類,那麼須要怎麼來注入值呢?
        * <property name="name" rel="具體的Bean的ID或者name的值"/>
        * 例如:
            <bean id="person" class="com.itheima.demo4.Person">
                <property name="pname" value="美美"/>
                <property name="car2" ref="car2"/>
            </bean>
複製代碼

3.3.4 Spring的2.5版本中提供了一種:p名稱空間的注入(瞭解)

1. 步驟一:須要先引入 p 名稱空間
    * 在schema的名稱空間中加入該行:xmlns:p="http://www.springframework.org/schema/p"

2. 步驟二:使用p名稱空間的語法
    * p:屬性名 = ""
    * p:屬性名-ref = ""

3. 步驟三:測試
    * <bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
複製代碼

3.3.5 Spring的3.0提供了一種:SpEL注入方式(瞭解)

1. SpEL:Spring Expression Language是Spring的表達式語言,有一些本身的語法
2. 語法
    * #{SpEL}

3. 例如以下的代碼
    <!-- SpEL的方式 -->
    <bean id="person" class="com.itheima.demo4.Person">
        <property name="pname" value="#{'小風'}"/>
        <property name="car2" value="#{car2}"/>
    </bean>

4. 還支持調用類中的屬性或者方法
    * 定義類和方法,例如
        public class CarInfo {
            public String getCarname(){
                return "奇瑞QQ";
            }
        }
複製代碼

3.3.6 數組,集合(List,Set,Map),Properties等的注入

1. 若是是數組或者List集合,注入配置文件的方式是同樣的
    <bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
        <property name="arrs">
            <list>
                <value>美美</value>
                <value>小風</value>
            </list>
        </property>
    </bean>

2. 若是是Set集合,注入的配置文件方式以下:
    <property name="sets">
        <set>
            <value>哈哈</value>
            <value>呵呵</value>
        </set>
    </property>

3. 若是是Map集合,注入的配置方式以下:
    <property name="map">
        <map>
            <entry key="老王2" value="38"/>
            <entry key="鳳姐" value="38"/>
            <entry key="如花" value="29"/>
        </map>
    </property>

4. 若是是properties屬性文件的方式,注入的配置以下:
    <property name="pro">
        <props>
            <prop key="uname">root</prop>
            <prop key="pass">123</prop>
        </props>
    </property>
複製代碼

3.3.7 Spring框架的配置文件分開管理(瞭解)

1. 例如:在src的目錄下又多建立了一個配置文件,如今是兩個核心的配置文件,那麼加載這兩個配置文件的方式有兩種!
    * 主配置文件中包含其餘的配置文件:
        <import resource="applicationContext2.xml"/>

    * 工廠建立的時候直接加載多個配置文件:
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    "applicationContext.xml","applicationContext2.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
   	<!-- 使用bean標籤 -->
   	<bean id="userService" class="com.itheima.demo2.UserServiceImpl">
   		<property name="name" value="小鳳"/>
   	</bean>
   	
   	<!-- 演示的依賴注入 -->
   	<bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>
   	<bean id="customerService" class="com.itheima.demo3.CustomerServiceImpl">
   		<property name="customerDao" ref="customerDao"/>
   	</bean>
   	
   	<!-- 演示的構造方法的注入的方式 -->
   	<bean id="car1" class="com.itheima.demo4.Car1">
   		<!-- <constructor-arg name="cname" value="奇瑞QQ"/> <constructor-arg name="price" value="25000"/> -->
        
   		<constructor-arg index="0" value="囚車"/>
   		<constructor-arg index="1" value="545000"/>
   	</bean>
    
   	<!-- 演示的依賴注入 -->
   	<bean id="person" class="com.itheima.demo4.Person">
   		<constructor-arg name="pname" value="美美"/>
   		<constructor-arg name="car1" ref="car1"/>
   	</bean>
   	
   	<!-- 採用set方法注入 <bean id="car2" class="com.itheima.demo4.Car2"> <property name="cname" value="二八自行車"/> <property name="price" value="1000"/> </bean> -->
   	
   	<!-- 採用p名稱空間注入的方式(瞭解) <bean id="car2" class="com.itheima.demo4.Car2" p:cname="保時捷" p:price="1000000"/> -->
	
	<!-- 使用SPEL方式注入 -->
	<bean id="car2" class="com.itheima.demo4.Car2">
		<property name="cname" value="#{'福特野馬'}"/>
		<property name="price" value="#{450000}"/>
	</bean>
	
	<!-- 注入集合 <bean id="user" class="com.itheima.demo4.User"> <property name="arrs"> <list> <value>哈哈</value> <value>呵呵</value> <value>嘿嘿</value> </list> </property> <property name="list"> <list> <value>美美</value> <value>小鳳</value> </list> </property> <property name="map"> <map> <entry key="aaa" value="小蒼"/> <entry key="bbb" value="小澤"/> </map> </property> <property name="pro"> <props> <prop key="username">root</prop> <prop key="password">1234</prop> </props> </property> </bean> -->
	
	<!-- 引入其餘的配置文件 <import resource="applicationContext2.xml"/> -->
</beans>
複製代碼

4、在web項目中集成Spring

CustomerDao.java:

public interface CustomerDao {
	public void save();
}
複製代碼

CustomerDaoImpl.java:

public class CustomerDaoImpl implements CustomerDao {
	
	public void save() {
		System.out.println("持久層:保存客戶...");
	}
}
複製代碼

CustomerService.java:

public interface CustomerService {
	public void save();	
}
複製代碼

CustomerServiceImpl.java:

public class CustomerServiceImpl implements CustomerService {
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
    
	public void save() {
		System.out.println("業務層:保存客戶...");
		cust omerDao.save();
	}
}
複製代碼

配置文件 applicationContext.xml 添加以下:

<!-- 配置客戶的業務層 -->
<bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
    <property name="customerDao" ref="customerDao"/>
</bean>

<!-- 配置持久層 -->
<bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"/>
複製代碼

CustomerAction.java:

/** * 客戶的Action * @author Administrator */
public class CustomerAction extends ActionSupport{
	
	private static final long serialVersionUID = 113695314694166436L;
	
	/** * 保存客戶 * @return */
	public String save(){
		System.out.println("WEB層:保存客戶...");
		// 使用工廠
		/*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); CustomerService cs = (CustomerService) ac.getBean("customerService"); cs.save();*/
		
		ServletContext servletContext = ServletActionContext.getServletContext();
		// 須要使用WEB的工廠的方式
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		CustomerService cs = (CustomerService) context.getBean("customerService");
		cs.save();
		
		return NONE;
	}
}
複製代碼

頁面每次點擊保存客戶,則會調用 Action 層(WEB層) save() 方法,再調用 Service 層(業務層),最後調用 Dao 層(持久層)。

但這裏會出現一個問題,每訪問一次都會加載一次配置文件(即每次請求都會建立一個工廠類,服務器端的資源就浪費了,通常狀況下一個工程只有一個Spring的工廠類就OK了)。須要解決該問題,該怎麼解決呢?整合來了。

第一步:找到spring-framework-4.2.4.RELEASE\libs\spring-web-4.2.4.RELEASE.jar複製到 WEB-INF\lib 目錄(即導入該 jar 包);

第二步:得先溫故 Servlet 監聽器有關知識了,如圖

  • 有這麼一類監聽器,監聽 ServletContext 對象的建立和銷燬的。

  • ServletContext 對象何時建立?當服務器啓動時候建立,當服務器銷燬時候關閉。

當服務器啓動, ServletContext 對象建立,則監聽器對象的方法執行,加載 applicationContext.xml 文件,並且只會加載一次,而且裏面的那些對象已經建立好了。

第三步:配置監聽器(在 web.xml 添加以下)

<!-- 配置WEB整合的監聽器 -->
<listener>
    <!-- 服務器一啓動,監聽器這個類的方法就會執行,能夠關聯查看源碼 --> 
    <!-- 配置該監聽器是固定的,只須要每次粘貼過來就行。-->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加載方式:該監聽器默認加載WEB-INF目錄下的配置文件。-->
<!-- 咱們提供配置方式,加載其餘目錄下配置文件,如src目錄下的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
複製代碼

第四步:編寫 Servlet

public String save(){
    System.out.println("WEB層:保存客戶...");
    ServletContext servletContext = ServletActionContext.getServletContext();
    // 須要使用WEB的工廠的方式
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    CustomerService cs = (CustomerService) context.getBean("customerService");
    cs.save();
    return NONE;
}
複製代碼

步驟小結:Spring框架整合WEB(不是最終的方案)

1. 建立JavaWEB項目,引入Spring的開發包。編寫具體的類和方法。
    * 環境搭建好後,啓動服務器來測試項目,發送每訪問一次都會加載一次配置文件,這樣效率會很是很是慢!!

2. 解決上面的問題
    * 將工廠建立好了之後放入到ServletContext域中.使用工廠的時候,從ServletContext中得到.
        * ServletContextListener:用來監聽ServletContext對象的建立和銷燬的監聽器.
        * 當ServletContext對象建立的時候:建立工廠 , 將工廠存入到ServletContext

3. Spring整合Web項目
    * 引入spring-web-4.2.4.RELEASE.jar包
    * 配置監聽器
         <!-- 配置Spring的核心監聽器: -->
         <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
         </context-param>

4. 修改servlet的代碼
    * 從ServletContext中得到工廠
    * 具體代碼以下
        ServletContext servletContext = ServletActionContext.getServletContext();
        // 須要使用WEB的工廠的方式
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        CustomerService cs = (CustomerService) context.getBean("customerService");
        cs.save();  
複製代碼
相關文章
相關標籤/搜索