1. 程式人生 > 實用技巧 >Spring 基本配置部分

Spring 基本配置部分

五、別名

於資料庫和mybatis差不多;

 <alias name="UserServiceImpl" alias="service"/>

前面是名字,後面是別名;但是的話,還有一種的方法是在bean下直接可以寫別名:

<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl" name="service2">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg type="com.saxon.Dao.UserDao" ref="OralceUserImpl"/>
    </bean>

並且可以通過分隔符來進行一個分割,實現取出多個的別名的目的;

六、import

把多個配置檔案合成一個,如果檔案內容有完全一樣的部分就會合並;

多個檔案合併後,使用一個總檔案就可以訪問;

七、依賴注入(DI)

直接上程式碼:

pojo.student:

public class Student {
    private String name;
    private Address address;
    private Map<String,String> card;
    private String[] books;
    private List<String> teacher;
    private Set<String> games;
    private Properties info;
    private String wife;
   @Override
    public String toString () {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", card=" + card +
                ", books=" + Arrays.toString (books) +
                ", teacher=" + teacher +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}
//setter getter 略,但是必須要寫

applicationContext:

<bean id="student" class="com.saxon.pojo.Student">
        <!--            第一種 直接注入-->
        <property name="name" value="saxon"/>
        <!--            第二種 物件注入-->
        <property name="address" ref="address"/>
        <!--        第三種 陣列-->
        <property name="books">
            <array>
                <value>關於我是如何變帥的那些事 1</value>
                <value>關於我是如何變帥的那些事 2</value>
                <value>關於我是如何變帥的那些事 3</value>
            </array>
        </property>
        <!--        map集合-->
        <property name="card">
            <map>
                <entry key="author" value="saxon"/>
                <entry key="author" value="saxon 2"/>
            </map>
        </property>
        <!--            list 集合-->
        <property name="teacher">
            <list>
                <value>李老師</value>
                <value>馬老師</value>
            </list>
        </property>
        <!--        set 集合-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>The Honor OF KING</value>
            </set>
        </property>
        <!--        properties-->
        <property name="info">
            <props>
                <prop key="Lover">null</prop>
            </props>
        </property>
        <!--        關於空值的設定 null和""-->
        <property name="wife">
            <null/>
        </property>
    </bean>
    <bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
    </bean>

顯示出來的內容:

Student{name='saxon', address=Address{country='CHINA', province='YUNNAN'}, card={author=saxon 2}, books=[關於我是如何變帥的那些事 1, 關於我是如何變帥的那些事 2, 關於我是如何變帥的那些事 3], teacher=[李老師, 馬老師], games=[LOL, The Honor OF KING], info={Lover=null}, wife='null'}

c名稱空間和p名稱空間:

這兩個需要引入我們的配置;
p名稱空間,使用setter注入:

xmlns:p="http://www.springframework.org/schema/p"
    <bean id="student" class="com.saxon.pojo.Student" p:name="saxon">

c名稱空間,使用構造器注入:

xmlns:c="http://www.springframework.org/schema/c"
<bean id="student" class="com.saxon.pojo.Student" c:name="saxon">

七、bean的作用域

一共有6種,但是後面的四種是基於web應用的;

Scope Description
singleton (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

測試程式碼:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml");
        Student student1 = (Student)context.getBean ("student");
        Student student2 = (Student)context.getBean ("student");
        System.out.println (student1==student2);

1.sigleton:無論建立幾個都始終只有一個並且是預設的作用域

  <bean id="student" class="com.saxon.pojo.Student" c:name="saxon" scope="singleton">

結果:true

2.prototype:每次建立都會新建一個物件,就是浪費資源

   <bean id="student" class="com.saxon.pojo.Student" c:name="saxon" scope="prototype">

結果:false;

八、自動裝配

1.顯示自動裝配 xml 裝配

applicationContext.xml

2.Java檔案自動裝配

3.隱式自動裝配

autowrite:

1.autowire="byName"

它會自動尋找我們屬性中還沒有注入的屬性,選取ID 為setXXX的XXX 自動裝配

比如:

public void setAddress (Address address) {
        this.address = address;
    }
<bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
 </bean>

這個的set字尾就是address,那麼就去找ID為address的bean;如果ID不一致就不會自動裝配;區分大小寫,id唯一;

2.autowire="byType"

會自動尋找我們屬性中還沒有注入的屬性,選取類的型別為setXXX的XXX 自動裝配

public void setAddress (Address address) {
        this.address = address;
    }
//類就是address
<bean id="address" class="com.saxon.pojo.Address">
        <property name="country" value="CHINA"/>
        <property name="province" value="YUNNAN"/>
 </bean>

com.saxon.pojo.Address型別唯一自動裝配,於id無關;id可以不寫;
還有一種使用resource,Java原生的注入方式,那個預設使用的是ByName,在byName找不到的時候就去byType按照id找,他的方式與spring的自動裝配的預設方式相反

自學總結
學習地址:狂神說Java