spring01-快速入門

spring


  1. spring是什麼
    • Spring是一個開源框架
    • spring能夠簡化企業級應用開發
    • spring是一個以IoC(Inverse Of Control:反轉控制)和AOP(Aspect Oriented Programming:面向切面編程)爲核心的框架
  2. 特色
    • 輕量級: spring是非侵入性的. 基於spring開發的應用中的對象能夠不依賴於spring的API
    • 依賴注入: (DI ---> dependency injection, IOC)
    • 面向切面編程(AOP)
    • 容器: spring是一個容器, 由於它包含並管理應用對象的生命週期
      ...
  3. spring模塊
    spring模塊

hello


  1. 準備
    • 引入依賴git

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.8.RELEASE</version>
      </dependency>
    • 建立Hello類github

      public class Hello {
          public void hello() {
              System.out.println("hello spring");
          }
      }
    • 編寫配置文件web

      <?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="hello" class="cn.ann.Hello"/>
      </beans>
      • beans 標籤後面的一大坨是固定寫法, 無腦複製粘貼就好
      • bean 標籤中, id 是惟一標識, 通常將類名首字母小寫, class 是類的全類名
  2. 編寫測試類測試spring

    @Test
    public void demo01() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello = (Hello) ac.getBean("hello");
        hello.hello();
    }

    運行結果:
    運行結果編程

IOC & DI


  1. 是什麼?
    • IOC: Ioc—Inversion of Control,即「控制反轉」,不是什麼技術,而是一種設計思想。在Java開發中,Ioc意味着將你設計好的對象交給容器控制,而不是傳統的在你的對象內部直接控制。 這種設計思想是爲了下降程序的耦合
    • DI: Dependency Injection,即「依賴注入」:是組件之間依賴關係由容器在運行期決定,形象的說,即由容器動態的將某個依賴關係注入到組件之中。
    • 想了解更多能夠看Iteye的開濤這位技術牛人對Spring框架的IOC的理解, 附上連接: http://jinnianshilongnian.iteye.com/blog/1413846

配置文件和測試類分析


  1. 測試類代碼分析
    • 測試類中第一行代碼 new ClassPathXmlApplicationContext(配置文件路徑) 是對IOC容器初始化, 其返回值能夠用ApplicationContext或BeanFactory接收
      1. BeanFactory: 建立對象時採用 延遲加載 的策略, 即: 第一次獲取對象時才建立
      2. ApplicationContext: 建立對象時採用 當即加載 的策略, 即: 讀取完配置文件後就加載對象
    • 測試類中第二行代碼 ac.getBean("hello") 能夠獲取IOC容器中的指定對象, 參數能夠是id值, 也能夠是Class對象
      1. 參數是 id: 獲取指定id值映射的bean
      2. 參數是 Class 對象: 獲取相應的bean, 可是容器中只能有一個該類的 bean, 即: 容器中只能有一個class屬性爲該類型的bean標籤
  2. spring對bean的管理細節
    1. 建立bean的三種方式
      1. 使用默認構造函數建立
        <bean id="user" class="cn.ann.User"/>
        • 在spring的配置文件中使用bean標籤, 配以id和class之後, 且沒有其餘標籤時, 採用的就是默認構造函數建立bean對象, 若是類中沒有默認的無參構造函數, 對象就沒法建立
      2. 使用普通工廠的方法建立session

        <bean id="userFactory" class="cn.ann.UserFactory"/>
        <bean id="user" factory-bean="userFactory" factory-method="getUser"/>
      3. 使用靜態工廠方法建立
        <bean id="user" class="cn.ann.StaticFactory" factory-method="getUser"/>
    2. bean對象的做用範圍
      • bean標籤的scope屬性
        1. singleton: 單例(默認)
        2. prototype: 原型, 就是多例
        3. request: 做用於web應用的請求範圍
        4. session: 做用於web應用的會話範圍
        5. global-session: 做用於集羣環境的會話範圍(全局會話範圍), 當不是集羣時, 就是session
    3. bean對象的生命週期
      1. singleton:
        • 建立: 隨容器的建立而建立
        • 銷燬: 隨容器的銷燬而銷燬
        • 總結: 與容器的生命週期相同
      2. prototype:
        • 建立: 第一次使用對象時建立
        • 銷燬: 經過Java的垃圾回收器回收
  3. spring的依賴注入
    1. 是什麼?
      • 當前類須要用到的其它類的對象, 有spring爲咱們提供, 咱們只須要在配置文件中維護依賴關係便可
    2. 能注入的數據: 有三種
      1. 基本數據類型和String
      2. 其餘bean類型
      3. 複雜類型, 集合類型
    3. 注入方式: 有三種(注意, 常常變化的數據不適合注入)
      1. 使用構造函數
      2. 使用set方法
      3. 使用註解
    • 使用構造函數注入app

      <bean id="user" class="cn.ann.User">
          <constructor-arg name="" value="" type="" index=""></constructor-arg>
      </bean>
      • 需使用 constructor-arg 標籤, 屬性:
        • type: 爲要插入的數據指定類型, 該參數也是構造函數中某個或某些參數的類型
        • index: 爲要插入的數據指定索引位置, 0開始
        • name: 爲構造方法中指定名稱的參數賦值
        • 以上三個用於指定給構造參數哪一個參數賦值, 可是最經常使用的時name
        • value: 用於提供基本類型和其餘數據類型
        • ref: 用於引用其餘的bean類型, 必須是IOC容器中有的bean對象

        • 優點: 若對象沒有提供無參構造器, 那麼在獲取bean的時候諸如數據是必須的操做, 不然對象沒法建立成功
        • 弊端: 若是數據不是必需的, 就不能建立對象
    • 使用set注入(更經常使用)框架

      <bean id="user" class="cn.ann.User">
          <property name="name" value="zs"/>
          <property name="age" value="23"/>
          <property name="birthday" ref="date"/>
      </bean>
      • 需使用property標籤, 屬性:
        • name: 用於指定注入的屬性名稱
        • value: 用於指定注入的屬性的值
        • ref: 用於引用其餘的bean類型, 必須是IOC容器中有的bean對象

        • 優點: 建立對象時直接調用默認構造函數就好, 沒有明確的限制
        • 弊端: 獲取對象時set方法可能沒有執行
    • 複雜類型注入
      • bean屬性(省略了getter,setter和toString):函數

        public class CollectionDemo {
            private String[] strings;
            private List<String> list;
            private Set<String> set;
            private Map<String, String> map;
            private Properties prop;
        }
      1. array類型(String[])測試

        <property name="strings">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
      2. List類型

        <property name="list">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>
      3. Set類型

        <property name="set">
            <set>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </set>
        </property>
      4. Map類型

        <property name="map">
            <map>
                <entry key="key01" value="val01"/>
                <entry key="key02" value="val02"/>
                <entry key="key03" value="val03"/>
            </map>
        </property>
      5. Properties類型

        <property name="prop">
            <props>
                <prop key="prop01">val01</prop>
                <prop key="prop02">val02</prop>
                <prop key="prop03">val03</prop>
            </props>
        </property>

    • 注意: array, list和set均可以對list結構進行注入; entry和props均可以對Map結構進行注入


代碼連接: 此處 的 spring01-quickStart

相關文章
相關標籤/搜索