Java匹馬行天下之J2EE框架開發——Spring—>用IDEA開發Spring程序(01)

1、心動不如行動

1、建立項目

*注:在IDEA中我建立的Maven項目,不瞭解Maven的朋友能夠看我以前的博客「咱們一塊兒走進Maven——知己知彼」,瞭解Maven後能夠看我以前的博客「Maven的安裝與配置」,自行安裝,行動起來吧。html

 

2、加載依賴

 在pom.xml文件中添加Spring依賴和日誌相關依賴java

<dependencies>
  <!--測試相關-->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <!--Spring核心基礎依賴-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <!--日誌相關-->
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
</dependencies>

  

3、項目結構

在main目錄下面建立java和resources目錄web

        

   

 

      

 

 

4、基礎代碼實現(練手)

    • 建立TestDao接口和實現類
    • 得到TestDao實現類的實例
      • 在以前開發中,咱們是直接new一個對象便可。即:`private TestDao dao = new TestDaoImpl();`
      • 在學習Spring以後,將由Spring來建立對象的實例 --> 即:`IoC 控制反轉(Inverse of Control)`
        以後須要實例對象時,從Spring工廠(容器)中得到便可,須要將實現類的全限定名稱配置到xml文件中。

建立dao包,在dao包下建立TestDao接口和TestDao接口的實現類,結構以下圖:正則表達式

TestDao接口代碼示例:spring

package dao;

public interface TestDao {
    public void sayHello();
}

TestDaoImpl實現類代碼示例:express

package dao;

public class TestDaoImpl implements TestDao{
    @Override
    public void sayHello() {
        System.out.println("Hello,Spring!");
    }
}

  

在resources資源目錄點擊右鍵,依次選擇New-->XML Configuration File-->Spring Config,建立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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--把testDao對象的建立權交給Spring-->
    <!-- <bean> 配置須要建立的對象
id :用於以後從Spring容器中得到實例時使用的
class :須要建立實例的全限定類名-->
<bean id="testDao" class="dao.TestDaoImpl"></bean> </beans>

建立test包,在test包下建立測試類SpringTest  api

 

 

package test;
import dao.TestDao;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
@Test
public void demo1(){
// 以前開發,本身手寫new出對象
TestDao dao= new TestDaoImpl();
dao.sayHello();
}

@Test
public void demo2() {
// 如今從spring容器中得到對象實例
// 1 、得到容器
//初始化Spring容器ApplicationContext,加載配置文件
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
// 二、得到內容 ,注意此時不須要本身new出對象了,都是從spring容器中得到
//經過容器獲取testDao實例
TestDao testDao = (TestDao) application.getBean("testDao");
testDao.sayHello();
}
}

  

  • IoC(Inverse of Control)反轉控制的概念,就是將本來在程序中手動建立TestDaoImpl對象的控制權,交由Spring框架管理。
  • 簡單說,就是建立TestDaoImpl對象的控制權被反轉到了Spring框架。


點擊測試方法左側的運行按鈕,選擇Run,測試代碼數組

 

 

運行後控制檯顯示結果    app

 

 

項目運行成功!!!!!!!

   

5、Spring入門案例:DI(掌握)   

  • DI :Dependency Injection :依賴注入
  • is a :是一個,繼承。
  • has a:有一個,成員變量,依賴。
 class B {
       private A a;   // B類依賴A類,B類使用A類。
    }

    依賴:一個對象須要使用另外一個對象。
    注入:經過setter方法進行另外一個對象實例設置。

  例如:

class BookServiceImpl {
        // 以前開發:接口 = 實現類(service和dao耦合了,寫死了,知道具體的實現類是誰,那麼個人具體實現類變化,那麼這行代碼也得跟着變)
        // private BookDao bookDao = new BookDaoImpl();

         // spring以後(解耦:service實現類使用了dao的接口,這樣就不知道具體的實現類是誰了)
        private BookDao bookDao;
        setter方法
   }

模擬spring執行過程
    建立service實例:BookService bookService = new BookServiceImpl();   => IoC <bean>
    建立dao實例:BookDao bookDao = new BookDaoImple();                  => IoC
    將dao設置給service:bookService.setBookDao(bookDao);                => DI <property>

  

具體代碼實現:

實現步驟:

  • 建立BookDao接口和實現類
  • 建立BookService接口和實現類
  • 將dao和service配置到 applicationContext.xml文件中
  • 使用api測試

項目結構:

 加載依賴:

 在pom.xml文件中添加Spring依賴和日誌相關依賴

 

<dependencies>
  <!--測試相關-->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <!--Spring核心基礎依賴-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <!--日誌相關-->
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
</dependencies>

 

  

 

 

BookDao接口和實現類

 

package dao;

public interface BookDao {
    void save();
}

 

  

package dao;

public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("實現添加功能");
    }
}

  

BookService接口和實現類

package Service;

public interface BookService {
    void addBook();
}

  

package Service;

import dao.BookDao;
import dao.BookDaoImpl;

public class BookServiceImpl implements BookService {

    //方式一:以前,接口=實現類
    //private BookDao bookDao= new BookDaoImpl();

    //方式二:如今,接口+setter方法
    private BookDao bookDao;
    public void setBookDao(BookDao bookDao){
        this.bookDao=bookDao;
    }

    @Override
    public void addBook() {
    this.bookDao.save();

    }
}

  

 

將dao和service配置到 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- <bean> 配置須要建立的對象
            id :用於以後從Spring容器中得到實例時使用的
            class :須要建立實例的全限定類名-->
    <!--
   模擬spring執行過程
       建立service實例:BookService bookService = new BookServiceImpl();   => IoC <bean>
       建立dao實例:BookDao bookDao = new BookDaoImple();                  => IoC
       將dao實例設置給service實例:bookService.setBookDao(bookDao);         => DI <property>

       <property> 用於進行屬性注入
           name : Bean的屬性名稱,經過setter方法得到
               setBookDao  =>  BookDao =>  bookDao
           ref :另外一個Bean的id值的引用
   -->
    <!-- 建立service實例 -->
    <bean id="bookServiceId" class="Service.BookServiceImpl">
        <!-- 將dao實例設置給service實例 -->
        <property name="bookDao" ref="bookDaoId"></property> <!-- 用於進行屬性注入 -->
    </bean>
    <!-- 建立dao實例 -->
    <bean id="bookDaoId" class="dao.BookDaoImpl"></bean>
</beans>

  

 

 

使用api測試

建立test包,在test包下建立測試類SpringTest

package test;

import Service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    public void Test1(){
        // 以前開發,本身手寫new出對象
        // BookService bookService = new BookServiceImpl();
        // bookService.addBook();
    }

    @Test
    public void Test2(){
        // 如今從spring容器中得到對象實例
        // 1 、得到容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 二、得到內容 ,注意此時不須要本身new出對象了,都是從spring容器中得到
        BookService bookService = (BookService) applicationContext.getBean("bookServiceId");

        bookService.addBook();
    }
}

  

  • DI:Dependency Injection 依賴注入,在Spring框架負責建立Bean對象時,動態的將依賴對象注入到Bean組件。
  • getBean("bookServiceId"); 從spring容器中得到指定名稱對象的實例時,會先判斷本實例對象是否須要使用其餘實例化對象,因爲設置了< property name="bookDao" ref="bookDaoId">< /property>,說明須要使用其餘實例化對象,因此就根據其餘Bean的id值的引用,去spring容器中得到指定名稱對象的實例,至關於將dao實例設置給service實例。

運行後控制檯顯示結果

 

 這就成功了,開心一下,經過這兩個案例,多Spring有了初步的理解,加油!

 

6、Spring的核心API(瞭解)

 

api總體瞭解便可,以後不使用,在學習過程須要。

 

 

  • BeanFactory :這是一個工廠,用於生成任意Bean。
    採起延遲加載,第一次調用getBean(); 時纔會初始化Bean。(即實例化對象)
  • ApplicationContext :是BeanFactory的子接口,功能更強大。(國際化處理、事件傳遞、Bean自動裝配、各類不一樣應用層的Context實現)。 採起非延時加載,當配置文件被加載時,就進行對象的實例化。
    • ClassPathXmlApplicationContext 用於加載classpath(類路徑/src)下的xml
      • 加載xml運行時位置 --> /WEB-INF/classes/xxx.xml
    • FileSystemXmlApplicationContext 用於加載指定盤符下的xml
      • 加載xml運行時位置 --> /WEB-INF/xxx.xml
      • 經過java web學習過的 ServletContext.getRealPath(); 得到具體盤符

示例代碼以下:

package test;

import Service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class SpringTest {
    @Test
    public void Test1(){
        // 以前開發,本身手寫new出對象
        // BookService bookService = new BookServiceImpl();
        // bookService.addBook();
    }

    @Test
    public void Test2(){
        // 如今從spring容器中得到對象實例
        // 1 、得到容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 採起非延時加載,當配置文件被加載時,就進行對象的實例化。
        // 二、得到內容 ,注意此時不須要本身new出對象了,都是從spring容器中得到
        BookService bookService = (BookService) applicationContext.getBean("bookServiceId");

        bookService.addBook();
    }

    @Test
    public void demo3() {
        // 如今從spring容器中得到對象實例,使用的是BeanFactory,裏面須要一個Resource,該Resource又是一個接口,須要找它的實現類ClassPathResource
        // 1 、得到容器
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        // 二、得到內容 ,注意此時不須要本身new出對象了,都是從spring容器中得到
        BookService  bookService = (BookService) beanFactory.getBean("bookServiceId"); // 採起延遲加載,第一次調用getBean(); 時纔會初始化Bean(即實例化對象)。

        bookService.addBook();
    }
}

  

7、裝配Bean:基於XML

 

3種bean實例化方式:

  • 使用`默認構造方法`實例化
  • 使用`靜態工廠方法`實例化
  • 使用`實例工廠方法`實例化

使用默認構造方法實例化

格式:

<bean id="從Spring容器中得到實例時使用的" class="須要建立實例的全限定類名"></bean>
例如:<bean id="userServiceId" class="Service.UserServiceImpl"></bean>

  

示例中用到的 UserService.java 和 UserServiceImpl.java 代碼同上面實例中的代碼,這裏再也不贅述!
在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <!--第一種實例化Bean 的方式 :使用默認構造方法實例化,即要實例化的Bean必需要提供默認構造方法  -->
    <bean id="userServiceId" class="Service.UserServiceImpl"></bean>

</beans>

  測試代碼: 

public class TestIoC {
    @Test
    public void demo01() {
        // 以前開發,本身手寫new出對象
        UserService userService = new UserServiceImpl(); // 直接手動建立實例

        userService.addUser();
    }

    @Test
    public void demo02() {
        // 如今從spring容器中得到對象實例
        // 1 、得到容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 二、得到內容 ,注意此時不須要本身new出對象了,都是從spring容器中得到
        UserService userService = (UserService) applicationContext.getBean("userServiceId");

        userService.addUser();
    }
}

  

使用`靜態工廠方法`實例化

  • 靜態工廠:經常使用與spring整合其餘框架(工具)時。
  • 靜態工廠:用於生成實例對象,全部的方法必須是static。

示例中用到的 UserService.java 和 UserServiceImpl.java 代碼同上面實例中的代碼,這裏再也不贅述!
格式:

<bean id=""  class="工廠全限定類名"  factory-method="靜態方法名稱">

  

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <!--第二種實例化Bean 的方式 :使用靜態工廠方法實例化
        將自定義的靜態工廠建立的實例交給spring管理 
        class 自定義靜態工廠全限定類名
        factory-method 靜態方法名
    -->
    <bean id="userServiceId" class="Service.MyBeanFactory" factory-method="createService"></bean>
</beans>

  

 靜態工廠類代碼:
public class MyBeanFactory {

    /**
     * 建立實例的靜態工廠,全部的方法必須是靜態的(static)。
     * 
     * @return
     */
    public static UserService createService() {
        return new UserServiceImpl();
    }

    // 還有建立其餘實例的靜態工廠
    // ......
}
 

  測試代碼:
TestStaticFactory.java

 
/**
 * 第二種實例化Bean 的方式 :使用靜態工廠方法實例化
 *
 */
public class TestStaticFactory {

    @Test
    public void demo01() {
        // 之前:使用自定義的靜態實例工廠
        UserService userService = MyBeanFactory.createService();

        userService.addUser();
    }

    @Test
    public void demo02() {
        // 如今:使用spring 工廠:將自定義的靜態工廠建立的實例交給spring管理
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userServiceId", UserService.class); // 這種方式底層會自動轉換 
// UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
}

  

注意:當使用JDK版本爲1.8時,運行上面的測試代碼會出現一個問題: java.lang.IllegalArgumentException,
問題解決連接:使用Junit測試一個 spring靜態工廠實例化bean 的例子,全部代碼都沒有問題,可是出現java.lang.IllegalArgumentException異常
小結:在之後的開發中,工廠類不須要咱們去手寫,由於別人已經寫好了,咱們經過編寫配置文件,把別人寫好的工廠類拿來,寫上要用的方法名,而後把它生產後的實例給Spring存起來,之後咱們要用什麼實例,跟Spring說一下,去拿就能夠了。

 

使用`實例工廠方法`實例化

 

實例工廠:必須先有工廠的實例對象,而後經過實例對象去建立對象。特色:提供全部的方法都是「非靜態」的。

示例中用到的 UserService.java 和 UserServiceImpl.java 代碼同上面實例中的代碼,這裏再也不贅述!

  

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--第三種實例化Bean 的方式 :使用實例工廠方法實例化 -->
    <!--建立工廠實例 -->
    <bean id="myBeanFactoryId" class="com.itheima.c_inject.c_factory.MyBeanFactory" ></bean>
    <!--經過工廠實例,得到對象      factory-bean 工廠實例名稱        factory-method 普通方法名稱 -->
    <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>
</beans>

  靜態工廠類代碼:

 
public class MyBeanFactory {

    /**
     * 建立實例的工廠,全部方法非靜態
     * 
     * @return
     */
    public UserService createService() {
        return new UserServiceImpl();
    }

    // 還有建立其餘實例的工廠
    // ......
}
 

  測試代碼:
TestFactory.java

 

package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 第三種實例化Bean 的方式 :使用實例工廠方法實例化
 *  */
public class TestFactory {

    @Test
    public void demo01() {
        // 之前:使用自定義的實例工廠
        // 一、建立工廠實例
        MyBeanFactory myBeanFactory = new MyBeanFactory();
        // 二、經過工廠實例,得到對象
        UserService userService = myBeanFactory.createService();

        userService.addUser();
    }

    @Test
    public void demo02() {
        // 如今:使用spring 工廠:將自定義的實例工廠建立的實例交給spring管理
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userServiceId", UserService.class); // 這種方式底層會自動轉換
// UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
}

 

  

  • 1、Bean的種類

    • 普通bean :以前操做的都是普通bean。例如:< bean id="xxx" class="A" >,這句代碼的意思是:Spring直接建立A的實例,並返回。

    • FactoryBean :是一個特殊的bean,具備工廠生成對象能力,可是隻能生成特定的對象。
      想要生產對象的bean 必須實現FactoryBean 接口,此接口提供方法getObject(); 用於得到特定bean。

      • 示例:< bean id="xxx" class="FB">,這句代碼的意思是:Spring會先建立FB實例,而後調用getObject(); 方法,並返回方法的返回值。
        即至關於以下兩行代碼:
        FB fb = new FB();
        return fb.getObject();
    • BeanFactory 和 FactoryBean 對比?

      • BeanFactory :是一個生產bean的工廠,用於生成任意的bean。
      • FactoryBean :是一個特殊的bean,用於生成另外一個特定的bean。
        • 例如:ProxyFactoryBean ,此工廠bean用於生產代理對象的實例。< bean id="xxx" class="….ProxyFactoryBean">,這句代碼的意思是:得到代理對象的實例。即AOP使用。
    • spring容器中bean元素id和name屬性的區別?

      • 在spring容器中添加如下配置:
        示例:< bean id="userServiceId" class="com.itheima.a_ioc.UserServiceImpl">

      • bean節點中id和name的區別:

      • 區別一:

        • id : 指定惟一實例引用
        • name : 能夠指定多個實例引用,例如name="名稱1, 名稱2"
      • 區別二:

        • id :id的命名要知足XML對ID屬性命名規範
          例如:必須以字母開始,可使用字母、數字、連字符、下劃線、句話、冒號

        • name : 若是Bean的名稱中含有特殊字符,就須要使用name屬性
          例如 : < bean name="# boy" class="cn.itheima.ioc.Boy"/>

        • 由於name屬性能夠相同,因此後出現Bean會覆蓋以前出現的同名的Bean。

      總結:項目開發的時候,強烈要求用id,由於id能夠表示惟一引用。

 

  • 2、Bean的做用域

Bean 的做用域:用於肯定spring所建立bean 的實例個數。

 

 

    • 取值:
      • singleton 單例,默認值。
      • prototype 多例,每執行一次getBean() 將得到一個實例。例如:在struts整合spring時,須要配置action爲多例。
    • 配置示例:默認狀況下會在容器啓動時初始化bean,但咱們能夠指定Bean節點的 lazy-init="true" 來延遲初始化bean,這時候,只有第一次獲取bean會才初始化bean。
      • 例如:`< bean id="xxx" class="xxx" scope="xxx">`

    • 例如:< bean id="xxx" class="service.UserServiceImpl" lazy-init="true">
    • 若是想對全部bean都應用延遲初始化,能夠在根節點beans設置 default-lazy-init="true"
      例如:< beans default-lazy-init="true「>
    • Portlet是基於java的web組件,由portlet容器管理,並由容器處理請求,生產動態內容。
      • Portals使用portlets做爲可插拔用戶接口組件,提供信息系統的表示層。
      • 做爲利用servlets進行web應用編程的下一步,portlets實現了web應用的模塊化和用戶中心化。

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userServiceId" class="com.itheima.d_scope.UserServiceImpl" scope="prototype"></bean>
</beans>

  測試代碼:
TestScope.java

public class TestScope {

    @Test
    public void demo01() {
        // 如今:使用spring 工廠
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService1 = applicationContext.getBean("userServiceId", UserService.class);
// 這種方式底層會自動轉換
UserService userService2 = applicationContext.getBean("userServiceId", UserService.class);
// 這種方式底層會自動轉換
// 默認Bean的做用域是單例,因此打印的對象的地址是同樣的
// System.out.println(userService1); // service.UserServiceImpl@2ac273d3
// System.out.println(userService2); // service.UserServiceImpl@2ac273d3
// 如今在配置文件中添加scope屬性,值爲prototype,此時Bean的做用域變爲多例了,再次打印,輸出地址不同了
System.out.println(userService1);
//service.UserServiceImpl@66480dd7
System.out.println(userService2);
//service.UserServiceImpl@52a86356
}
}

  

 

  • 3、Bean的生命週期

Bean 的生命週期詳情

 

  1. instantiate bean 對象實例化。
  2. populate properties 封裝屬性。
  3. 若是Bean實現 BeanNameAware,則表示執行 setBeanName
  4. 若是Bean實現 BeanFactoryAware 或者 ApplicationContextAware,則表示設置實例工廠(setBeanFactory)或者上下文對象(setApplicationContext)。
  5. 若是存在類實現 BeanPostProcessor(後處理Bean),則表示執行 postProcessBeforeInitialization
  6. 若是Bean實現 InitializingBean,則表示執行 afterPropertiesSet
  7. 調用,則表示指定初始化方法 init
  8. 若是存在類實現 BeanPostProcessor(處理Bean),則表示執行 postProcessAfterInitialization
  9. 執行業務處理
  10. 若是Bean實現 DisposableBean,則表示執行 destroy
  11. 調用 ,則表示指定銷燬方法 customerDestroy

 

 

  • 4、Bean 的初始化和銷燬

 

目標方法執行前和執行後,將進行Bean的初始化或銷燬。
  示例:<bean id="xxx" class="xxx" init-method="初始化的方法名稱" destroy-method="銷燬的方法名稱"></bean>
示例代碼以下:

 

編寫目標類代碼:
UserService.java

public interface UserService {
    void addUser();
}

  

UserServiceImpl.java

public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("e_lifecycle add user");
    }

    public void myInit() {
        System.out.println("個人初始化方法");
    }

    public void myDestory() {
        System.out.println("個人銷燬方法");
    }
}

  

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--  
        init-method 用於配置初始化方法,用於準備數據等使用場景
        destroy-method 用於配置銷燬方法,用於清理資源等使用場景
    -->                        
    <bean id="userServiceId" class="service.UserServiceImpl" 
        init-method="myInit" destroy-method="myDestory"></bean>
</beans>

  編寫測試代碼:

 
public class TestLifecycle {

    @Test
    public void demo01() throws Exception {
        // 如今:使用spring 工廠(spring 容器)
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 UserService userService = (UserService) applicationContext.getBean("userServiceId"); 
userService.addUser(); // 要想使個人銷燬方法也執行,必需要求:
// 1.容器必須先close,個人銷燬方法纔會執行;
// 2.必須是單例的(spring所建立該bean的實例個數只有一個)即bean中的scope配置成默認便可。
// 由於此close方法在接口 ApplicationContext 中沒有定義,而在實現類中提供了該方法,咱們可使用反射,由於反射最後執行的就是實現類中的方法。
applicationContext.getClass().getMethod("close").invoke(applicationContext); } }
 

  

  • 5、BeanPostProcessor 後處理Bean

  • 是由spring提供的一種機制,只要實現類實現此接口BeanPostProcessor,並將該實現類提供給spring容器,spring容器將自動執行兩個方法:在初始化方法前執行before()方法,在初始化方法後執行after()方法。配置格式: 這句代碼的意思就是:把實現類提供給了spring容器。
  • Factory hook(勾子) that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies.
  • spring提供工廠勾子,用於修改實例對象,能夠生成代理對象。(是AOP底層)
    谷歌翻譯:Factory hook(勾子),容許自定義修改新的bean實例,例如:檢查標記接口或用代理包裝它們。

咱們來模擬這句話的意思:

before()    => postProcessAfterInitialization(Object bean, String beanName) 
after()     => postProcessBeforeInitialization(Object bean, String beanName) 

A a = new A();

a = B.before(a); // 將a的實例對象傳遞給後處理bean,能夠什麼都沒作,也能夠作一些事情,好比:生成jdk代理對象並返回給a,這樣a就從實例對象變成代理對象了,此時的a就具備了AOP功能;再好比,若是把null返回給a,再用a去調用方法,就會出現空指針異常。
a.init();
a = B.after(a);

// 如下是AOP演示:
// 咱們如今在後處理Bean 代碼執行完以後,把jdk代理對象返回給a。讓a在調用addUser()以前先作一些事情

// 以前要作的事情
a.addUser(); // 在目標方法的先後能夠作一些事情,例如:開啓事務、提交事務、性能監控(先後時間)等等
// 以後要作的事情

a.destroy();

  目標類示例代碼以下:
       UserService.java

 

public interface UserService {
    void addUser();
}

 

  UserServiceImpl.java

 

public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("e_lifecycle add user");
    }

    public void myInit() {
        System.out.println("個人初始化方法");
    }

    public void myDestory() {
        System.out.println("個人銷燬方法");
    }
}

 

  實現類示例代碼以下:
MyBeanPostProcessor.java

 

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("執行了前方法:" + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
        System.out.println("執行了後方法:" + beanName);
        // 傳入的參數bean是咱們的目標對象,此時咱們的目標對象只有一個接口,那麼咱們的代理對象也只有一個接口
        // 生成jdk代理對象
        return Proxy.newProxyInstance(
                MyBeanPostProcessor.class.getClassLoader(), // 代理對象
                bean.getClass().getInterfaces(), // 目標對象
                new InvocationHandler() {

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("---開啓事務---");
                        Object obj = method.invoke(bean, args); // 執行目標方法,本例中的目標方法是addUser
                        System.out.println("---關閉事務---");
                        return obj;
                    }
                }); // 代理的處理程序
    }
}

 

  

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--  
        init-method 用於配置初始化方法,用於準備數據等使用場景
        destroy-method 用於配置銷燬方法,用於清理資源等使用場景
    -->                        
    <bean id="userServiceId" class="service.UserServiceImpl" 
        init-method="myInit" destroy-method="myDestory"></bean>

    <!-- 將後處理的實現類註冊給spring -->
    <bean class="com.itheima.e_lifecycle.MyBeanPostProcessor"></bean>
</beans>

  測試示例代碼以下:
      TestLifecycle.java

 

public class TestLifecycle {

    @Test
    public void demo01() throws Exception {
        // 如今:使用spring 工廠(spring 容器)
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
// 要想使個人銷燬方法也執行,必需要求:
// 1.容器必須先close,個人銷燬方法纔會執行;
// 2.必須是單例的(spring所建立該bean的實例個數只有一個)即bean中的scope配置成默認便可。
// 由於此close方法在接口 ApplicationContext 中沒有定義,而在實現類中提供了該方法,咱們可使用反射,由於反射最後執行的就是實現類中的方法。
applicationContext.getClass().getMethod("close").invoke(applicationContext);
}
}

 

  

 

    • 運行結果截圖:
    • 問題1:後處理bean做用某一個目標類,仍是全部目標類?
      答:全部。
    • 問題2:如何只做用一個?
      答:經過獲取"參數2"beanName進行控制。例如:"xxx".equals(method.getName());

 

  • 6、依賴注入Bean 的屬性(xml)

 

  • 注入依賴對象能夠採用:手工裝配自動裝配。在實際應用中建議使用手工裝配,由於自動裝配會產生未知狀況,開發人員沒法預見最終的裝配結果。
    • 手動裝配:通常進行配置信息都採用手動裝配。
      • 基於xml裝配基於註解裝配 => 以後講解
        • 構造方法注入
        • 屬性setter方法注入
        • 接口注入(spring不支持)
    • 自動裝配:在struts 和spring 整合的時候使用自動裝配。
      • byType:按類型裝配
      • byName:按名稱裝配
      • constructor:按構造裝配
      • autodetect:不肯定裝配(即自動裝配)

 

 

構造方法

Bean對象類:

public class User {

    private Integer uid;
    private String username;
    private Integer age;

    public User(Integer uid, String username) { // 構造方法一
        super();
        this.uid = uid;
        this.username = username;
    }

    public User(String username, Integer age) { // 構造方法二
        super();
        this.username = username;
        this.age = age;
    }
    // 省略getter 和 setter 方法
    // ......

  

在spring容器中進行配置:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--6.5.一、構造方法注入 
        <constructor-arg> 用於配置構造方法的一個參數argument
            name :參數的名稱
            value :設置普通數據
            ref :設置引用數據,通常是另外一個bean 的id值

            index :參數的索引號,從0開始 。若是隻有索引,匹配到了多個構造方法時,默認使用第一個。
            type :肯定參數類型

        例如1:name屬性開發中不經常使用,由於使用該屬性須要關聯要實例化對象的源碼,不然name的值你就不知道。而通常開發中咱們咱們不會獲得源碼。
            <constructor-arg name="username" value="李曉藝"></constructor-arg>
            <constructor-arg name="age" value="26"></constructor-arg>
        例如2:類型type 和  索引index (這二者結合使用)
            <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
            <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>

            注意:在開發中爲了指定執行的是哪一個構造方法,通常使用index屬性和type屬性結合的方式。
    -->            
    <bean id="userId" class="entity.User">
        <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
        <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
    </bean>
</beans>
 

  

setter方法

 

在spring容器中進行配置:

 
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--6.5.二、setter方法注入 
        * 注入的是普通數據 時
            <property name="" value="值"></property>
            等效
            <property name="">
                <value>值</value>
            </property>

        * 注入的是引用數據時
            <property name="" ref="另外一個bean的id"></property>
            等效
            <property name="">
                <ref bean="另外一個bean的id"></ref>
            </property>
    -->
    <bean id="personId" class="entity.Person">
        <property name="pname" value="曉藝"></property>
        <property name="age">
            <value>26</value>
        </property>

        <property name="homeAddr" ref="homeAddrId"></property>
        <property name="companyAddr">
            <ref bean="companyAddrId"></ref>
        </property>
    </bean>

    <bean id="homeAddrId" class="entity.Address">
        <property name="addr" value="山西運城"></property>
        <property name="tel" value="911"></property>
    </bean>

    <bean id="companyAddrId" class="entity.Address">
        <property name="addr" value="百子灣"></property>
        <property name="tel" value="120"></property>
    </bean>
</beans>
 

  

  • 7、P命名空間 [瞭解]

    • 是對"setter方法注入"進行簡化,替換

      而是在

    • p命名空間使用前提:必須添加命名空間。以下圖所示:

 

在spring容器中進行配置:

 
applicationContext.xml
 <!--6.5.三、P命名空間[瞭解]
            是對"setter方法注入"進行簡化,替換`<property name="屬性名稱">`,
            而是在`<bean p:屬性名稱="普通值" 和 p:屬性名稱-ref="引用值">`
            p命名空間使用前提:必須添加命名空間。

            注意:開發中通常不這麼用,通常用於裝逼用。
    -->
    <bean id="personId" class="com.itheima.f_xml.c_p.Person" 
        p:pname="明軍" 
        p:age="26" 
        p:homeAddr-ref="homeAddrId" 
        p:companyAddr-ref="companyAddrId">
    </bean>

    <bean id="homeAddrId" class="entity.Address"
        p:addr="河南信陽"
        p:tel="119">
    </bean>

    <bean id="companyAddrId" class="entity.Address"
        p:addr="青年路"
        p:tel="110">
    </bean>

  

 
  • 8、SpEL [瞭解]

 
  • 進行統一編程,全部的內容都使用value。
    格式:
    #{123}、#{'bruce'}、#{2e5} :數字、字符串、科學計數法(常量)
    #{beanId} :引用另外一個Bean
    #{beanId.propName} :引用Bean 的屬性(操做數據)
    #{beanId.toString()} :引用Bean 的方法(執行方法)
    #{T(類).字段|方法} :引用靜態方法或字段,例如:T(java.lang.Math).PI
    #{3 lt 4 == 4 ge 3} :運算符支持
    #{user.name matches ‘[a-z]{6,}’} :正則表達式支持
    #{likes[3]} :集合支持
 

示例代碼以下:

在spring容器中進行配置:

 
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--6.5.四、SpEL 
        <property name="cname" value="#{'Bruce'}"></property>

        <property name="cname" value="#{customerId.cname.toUpperCase()}"></property>
            經過另外一個bean,得到屬性,再調用的方法。
        <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
            ?. 若是對象不爲null,將調用方法,爲null,也去調用方法,不報錯。

    -->            
    <bean id="customerId" class="com.itheima.f_xml.d_SpEL.Customer">
        <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
        <property name="pi" value="#{T(java.lang.Math).PI}"></property>
    </bean>
</beans>

  

  • 9、集合注入

在spring容器中進行配置:

 
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--6.5.五、集合注入
        集合的注入都是給<property>添加子標籤
            數組:<array>
            List:<list>
            Set:<set>
            Map:<map>   注意:map存放的是k/v鍵值對,使用<entry>描述
            Properties  使用 <props> 和 <prop> 描述,示例:<props><prop key=""></prop></props>

                普通數據 放在:<value>
                引用數據 放在:<ref>
    -->            
    <bean id="collectionDataId" class="entity.CollectionData">
        <property name="arrayData">
            <array>
                <value>cmj</value>
                <value>lxy</value>3
                <value>明軍</value>
                <value>曉藝</value>
            </array>
        </property>

        <property name="listData">
            <list>
                <value>琴棋書畫</value>
                <value>擼啞鈴</value>
                <value>花鳥魚蟲</value>
                <value>擼娃娃</value>
            </list>
        </property>

        <property name="setData">
            <set>
                <value>看電影</value>
                <value>運動</value>
                <value>創做</value>
                <value>旅行</value>
            </set>
        </property>

        <property name="mapData">
            <map>
                <entry key="bruce" value="布魯斯"></entry>
                <entry>
                    <key><value>lucy</value></key>
                    <value>露西</value>
                </entry>
            </map>
        </property>

        <property name="propsData">
            <props>
                <prop key="處女座">心裏善良</prop>
                <prop key="天蠍座">宅心仁厚</prop>
                <prop key="緣定此生">今生不悔</prop>
            </props>
        </property>
    </bean>
</beans>

  

  • 10、裝配Bean:基於annotation(註解)

  • 註解:就是一個類,格式:@註解名稱
  • 開發中:使用註解 取代 xml配置文件。
1. @Component 取代 <bean class=""></bean>
   @Component("id的值") 

2. web開發中,提供3個@Component註解衍生註解(功能同樣)取代 <bean class=""></bean>
    @Repository :dao層
    @Service :service層
    @Controller :web層
        注意:SpringMVC 中推薦使用註解哦!
3. 依賴注入,給私有字段設置,也能夠給setter方法設置
    普通值:@Value("")
    引用值:
        方式1:按照【類型】注入
            @Autowired
        方式2:按照【類型+名稱】注入1
            @Autowired
            @Qualifier("名稱")
        方式3:按照【名稱】注入2
            @Resource("名稱")
4. 生命週期
    @PostConstruct :初始化
    @PreDestroy :銷燬
5. 做用域
    @Scope("prototype") 多例

  

  • 註解使用前提,必須添加命名空間,讓spring掃描含有註解類。

示例代碼:

<?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">

    <!-- 組件掃描:掃描含有註解的類 -->
    <context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan>
</beans>

  

  • 示例截圖:

     

演示:
單例、多例、初始化、銷燬
UserService.java

 

package service;

public interface UserService {
    void addUser();
}

UserServiceImpl.java

 

@Service("userServiceId") // 單例
@Scope("prototype") // 多例
public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("c_other add user");
    }

    @PostConstruct // 初始化
    public void myInit() {
        System.out.println("個人初始化方法");
    }

    @PreDestroy // 銷燬
    public void myDestory() {
        System.out.println("個人銷燬方法");
    }
}

  

在spring容器中進行配置:

 
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/spring-context.xsd">
    <!-- 組件掃描:掃描含有註解的類 -->
    <context:component-scan base-package="service"></context:component-scan>
</beans>

  

 測試代碼:
TestOther.java

package service;

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

public class TestOther {

    @Test
    public void demo01() {
        // 如今:使用spring 工廠
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService1 = applicationContext.getBean("userServiceId", UserService.class); // 這種方式底層會自動轉換
        UserService userService2 = applicationContext.getBean("userServiceId", UserService.class); // 這種方式底層會自動轉換

        // 默認Bean的做用域是單例,因此打印的對象的地址是同樣的
        // System.out.println(userService1); // com.itheima.c_inject.d_scope.UserServiceImpl@2ac273d3
        // System.out.println(userService2); // com.itheima.c_inject.d_scope.UserServiceImpl@2ac273d3

        // 如今在配置文件中添加scope屬性,值爲prototype,此時Bean的做用域變爲多例了,再次打印,輸出地址不同了
        System.out.println(userService1); // com.itheima.c_inject.d_scope.UserServiceImpl@66480dd7
        System.out.println(userService2); // com.itheima.c_inject.d_scope.UserServiceImpl@52a86356

        applicationContext.close();
    }
}

 

參考於筆記和博客園https://www.cnblogs.com/chenmingjun/(黑澤君)

********************************************************************************************

個人博客園地址:https://www.cnblogs.com/zyx110/

相關文章
相關標籤/搜索