天天看點

自學spring的第二天一、如何注入bean對象二、内部bean用法三、級聯屬性配置四、集合注入五、Spring的bean簡化配置六、自注入Bean對象七、Spring表達式語言

一、如何注入bean對象

  • 簡介
  • 如何将bean對象注入到另一個bean對象的屬性裡面
  • 用法
    • 具體使用ref标簽實作
      • 屬性1:bean
        • 注入目前容器的bean對象,如果目前容器沒有bean對象,它還會從父容器裡面去找,如果不存在,則報異常
      • 屬性2:parent
        • 注入父容器的bean對象,如果沒有,則報異常
  • 舉例
    • 定義父容器的parentbeans.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">
      
          <!--配置Car bean對象-->
          <bean id="car" class="com.gec.bean.Car">
              <property name="brand">
                  <value>特斯拉</value>
              </property>
              <property name="price">
                  <value>300000.00</value>
              </property>
          </bean>
      
      </beans>
                 
    • 定義子容器的childbeans.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="boss" class="com.gec.bean.Boss">
              <property name="bossName">
                  <value>鐘老闆</value>
              </property>
      
              <property name="car">
                  <!--從父容器裡面去找bean對象,id=car-->
                  <ref parent="car" />
              </property>
          </bean>
      
      </beans>
                 
    • 建立子容器的對象,指定容器的父子關系
      package com.gec.app;
      
      import com.gec.bean.Boss;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class ParentIocMainTest {
      
          public static void main(String[] args) {
      
              //建立一個父IOC容器
              ApplicationContext parentCtx=new ClassPathXmlApplicationContext("parentbean.xml");
              //建立一個子IOC容器
              ApplicationContext childCtx=new ClassPathXmlApplicationContext(
                      new String[]{"childbean.xml"},parentCtx);
      
              Boss bossObj= (Boss) childCtx.getBean("boss");
      
              System.out.println(bossObj.getBossName());
              System.out.println(bossObj.getCar());
      
          }
      }
      
                 

二、内部bean用法

  • 簡介
  • 沒有聲明id或者是name的bean,也就是無法被外部所使用,類似于java的匿名内部類
  • 用法
    <?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">
    
        <!--配置Boss的bean對象-->
        <bean id="boss" class="com.gec.bean.Boss">
            <property name="bossName">
                <value>鐘老闆</value>
            </property>
            <property name="car">
                <!--定義一個内部bean對象-->
               <bean class="com.gec.bean.Car">
                   <property name="brand">
                       <value>特斯拉</value>
                   </property>
                   <property name="price">
                       <value>300000.00</value>
                   </property>
               </bean>
            </property>
        </bean>
    
    </beans>
               

三、級聯屬性配置

  • 簡介
  • 針對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">
    
    
        <!--配置Boss的bean對象-->
        <bean id="boss" class="com.gec.bean.Boss">
            <property name="bossName">
                <value>鐘老闆</value>
            </property>
            <property name="car">
                <bean class="com.gec.bean.Car" />
            </property>
            <property name="car.brand">
                <value>寶馬</value>
            </property>
            <property name="car.price">
                <value>300000.00</value>
            </property>
        </bean>
    
    </beans>
               

四、集合注入

1、簡介

  • 針對List、Set、Map集合資料的注入

2、針對List集合的資料注入

  • 用法
    • 定義Boss類
      package com.gec.bean;
      
      import java.util.List;
      
      public class Boss {
      
          private String bossName;
          private Car car;
          //有那些興趣
          private List<String> favlist;
          //有擁有那些車
          private List<Car> carList;
      
          public String getBossName() {
              return bossName;
          }
      
          public void setBossName(String bossName) {
              this.bossName = bossName;
          }
      
          public Car getCar() {
              return car;
          }
      
          public void setCar(Car car) {
              this.car = car;
          }
      
          public List<String> getFavlist() {
              return favlist;
          }
      
          public void setFavlist(List<String> favlist) {
              this.favlist = favlist;
          }
      
          public List<Car> getCarList() {
              return carList;
          }
      
          public void setCarList(List<Car> carList) {
              this.carList = carList;
          }
      
          @Override
          public String toString() {
              return "Boss{" +
                      "bossName='" + bossName + '\'' +
                      ", car=" + car +
                      '}';
          }
      }
      
                 
    • 定義beans.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="car01" class="com.gec.bean.Car">
            <property name="brand">
                <value>寶馬1</value>
            </property>
            <property name="price">
                <value>1000000.00</value>
            </property>
        </bean>
    
        <bean  id="car02" class="com.gec.bean.Car">
            <property name="brand">
                <value>寶馬2</value>
            </property>
            <property name="price">
                <value>2000000.00</value>
            </property>
        </bean>
    
        <bean  id="car03" class="com.gec.bean.Car">
            <property name="brand">
                <value>寶馬3</value>
            </property>
            <property name="price">
                <value>3000000.00</value>
            </property>
        </bean>
    
    
        <!--配置Boss的bean對象-->
        <bean id="boss" class="com.gec.bean.Boss">
            <property name="bossName">
                <value>鐘老闆</value>
            </property>
            <property name="car">
                <bean class="com.gec.bean.Car" />
            </property>
            <property name="car.brand">
                <value>寶馬</value>
            </property>
            <property name="car.price">
                <value>300000.00</value>
            </property>
    
            <property name="favlist">
                <list>
                    <value>音樂</value>
                    <value>足球</value>
                    <value>遊泳</value>
                </list>
            </property>
    
            <property name="carList">
                <list>
                    <ref bean="car01" />
                    <ref bean="car02" />
                    <ref bean="car03" />
                </list>
            </property>
        </bean>
    
    </beans>
               

3、針對Set集合資料注入

<!--配置Boss的bean對象-->
    <bean id="boss" class="com.gec.bean.Boss">
        <property name="bossName">
            <value>鐘老闆</value>
        </property>
        <property name="car">
            <bean class="com.gec.bean.Car" />
        </property>
        <property name="car.brand">
            <value>寶馬</value>
        </property>
        <property name="car.price">
            <value>300000.00</value>
        </property>

        <property name="favlist">
            <list>
                <value>音樂</value>
                <value>足球</value>
                <value>遊泳</value>
            </list>
        </property>

        <property name="carList">
            <list>
                <ref bean="car01" />
                <ref bean="car02" />
                <ref bean="car03" />
            </list>
        </property>

        <property name="sports">
            <set>
                <value>籃球</value>
                <value>籃球</value>
                <value>籃球</value>
            </set>
        </property>
        

    </bean>

           

4、針對Map集合資料注入

5、針對Properties對象資料注入

五、Spring的bean簡化配置

1、字面值寫法

簡化

2、針對引用簡化

  • 簡化之後

3、p标簽的簡化寫法

  • 導入p标簽的命名空間
    xmlns:p="http://www.springframework.org/schema/p"
               
  • 通過p标簽定義bean配置

4、通過utils标簽定義集合

  • 如何使用utils标簽,導入此标簽的命名空間
    xmlns:utils="http://www.springframework.org/schema/util"
               
  • 定義一個ArrayList的bean集合
  • 如何定義一個Set集合bean
  • 如何定義一個Map集合bean

六、自注入Bean對象

1、簡介

  • 通過autowire屬性配置實作,實作自動的注入Bean對象

2、屬性

  • byName:從目前IOC容器自動注入name=屬性名的bean對象
  • byType:根據類型自動注入到bean對象的屬性裡面
  • constructor:通過構造器實作自裝配功能

3、用法

  • byName
  • byType
    • 自動根據bean類型自動注入到屬性
  • constructor
    • 自動根據構造器自動注入
      • 前提是類對象要有構造器
    • 定義Boss類

七、Spring表達式語言

1、概述

  • 它是從spring3才引入的,表達式作用以一種更加簡潔的方式去實作bean對象屬性值的注入,并且能夠實作動态注入資料值效果

2、文法

#{表達式}
           

3、功能

  • 通過bean id,通路bean對象
  • 通過表達式可以通路bean對象的屬性及方法
  • 對值進行算術運作
  • 對集合進行通路
  • 對類成員進行調用及通路

4、通過beanid,通路bean對象

5、通路bean對象的屬性及方法

6、對類成員進行調用及通路

  • 簡介
    • 如果要在SpEl中通路類作用域的方法和常量的話,要依賴T()這個關鍵的運算符。
  • 用法

7、對值進行算術運算

  • 簡介
    • 實作資料值加、減、乘
  • 舉例
    • 計算圓形周長(2pir)
    • 計算圓形面積
    • 實作

8、對集合通路

  • 定義歌曲對象
  • 自動點唱機對象
  • 配置歌曲清單
  • 擷取jukebox的屬性songs集合的第二個歌曲對象
  • 擷取jukebox的屬性songs的artist屬性等于周傑倫的歌曲清單集合,傳回是集合
<property name="songsList" value="#{jukebox.songs.?[artist eq '周傑倫']}"/>
           
  • 擷取jukebox的屬性songs的第一個artist屬性等于周傑倫的歌曲,傳回是歌曲對象
<property name="firstSongs" value="#{jukebox.songs.^[artist eq '周傑倫']}"/>
           
  • 擷取jukebox的屬性songs的最後一個artist屬性等于周傑倫的歌曲,傳回是歌曲對象
<property name="lastSongs" value="#{jukebox.songs.$[artist eq '周傑倫']}"/>