程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring學習之Ioc控制反轉(1),springioc

Spring學習之Ioc控制反轉(1),springioc

編輯:JAVA綜合教程

Spring學習之Ioc控制反轉(1),springioc


開始之前:

1. 本博文為原創,轉載請注明出處

2. 作者非計算機科班出身,如有錯誤,請多指正

---------------------------------------------------------------------------開始啦啦啦啦啦-------------------------------------------------------------------------------

從開始接觸spring起,聽到最多的就是Ioc(控制反轉)和AOP(面向切面編程)啦。Spring的官方文檔給出了這樣一個框架圖(><看起來好深奧~~)。不過本篇要介紹的所謂的控制反轉,就是通過core裡面的bean以及context jar包實現的哦。其他的jar包會在之後的章節中講到。

控制反轉,也可以稱之為依賴注入(dependency injection),指的是當依賴被創建,或從工廠裡獲得以後,其他類可以通過構造方法,工廠方法,或set方法得到此依賴。通俗一點的說,就是一個類裡面依賴的創建及使用,不需要在類本身new出此依賴,只要注入即可使用。舉個例子,我有一輛汽車,需要燃油才能發動,然而我總不能自己去開采燃油,我只需要去加油站加點油,不管你是中石油還是殼牌油,總之油注入進來了,車就可以發動了。這裡的燃油就是一個依賴類,這樣可以被注入的依賴類,我們稱之為bean,而對bean進行初始化,配置和集成操作的,我們稱之為Ioc容器(類似於加油站了)。Ioc容器的編譯可以使用xml,Java注解或Java程序來實現。雖然本人更喜歡注解的方式(尤其是使用了Spring Boot),但是入門起見,還是用xml掌握一下基礎知識再根據心情選方式吧~

那麼問題來啦,bean到底是咋配置的呢?下面給出了簡單的bean的定義結構

<?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="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

這是一段簡單的xml代碼,可以看到beans裡面定義了很多的bean,每個bean都映射到一個類,這些類就是你所要使用的依賴啦。下面我們來看容器和beans是怎麼關聯的。

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

這裡的ApplicationContext是context jar包裡面的容器接口,它讀取了兩個xml文件作為容器的配置,來完成各種bean的創建與關聯。

讓我們來實際操作一下,首先定義兩個xml的文件

service.xml (service裡面有一個依賴叫做petStore,而petStore裡面又有兩個依賴accountDao和itemDao)

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

    <!-- services -->

    <bean id="petStore" class="com.dabingguozi.generaltest.pojo.PetStoreService">
        <property name="accountDAO" ref="accountDAO"/>
        <property name="itemDAO" ref="itemDAO"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>
    
    <!-- more bean definitions for services go here -->

</beans>

 

daos.xml (service中petStore所注入的依賴accountDao和itemDao在這裡定義)

<?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="accountDAO"
          class="com.dabingguozi.generaltest.pojo.AccountDAO">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <bean id="itemDAO" class="com.dabingguozi.generaltest.pojo.ItemDAO">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for data access objects go here -->

</beans>

最後,不要忘記了,在Java裡面定義你的三個類(PetStoreService.java, ItemDAO.java, AccountDAO.java注意要包含set方法)和你的主程序(如下)

public class App {
    public static void main(String[] a){
        ApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"daos.xml", "services.xml"});
// retrieve configured instance
        PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
        List<String> userList = service.getUsernameList();
        System.out.print(userList);
    }
}

如有報錯,看看是不是jar包沒有導入,本人使用maven管理lib,詳見:https://mvnrepository.com/artifact/org.springframework

在Intellij裡面試一試,看看是不是我們想要的效果呢? 

 

我們成功的打出了依賴的類名。散花^^如此一來,我們的Spring之旅正式開始啦~~

 

 

下一篇:Spring學習之Ioc控制反轉(2)(這不是link,戳不開2333)

下一篇繼續研究控制反轉中bean定義的一些內容,博主首博,誠謝觀看。

 

本博客內容參考spring framework官方文檔,如有沖突,請參照原版內容

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/index.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved