Spring autowire自動裝配


 其他blog:

http://www.cnblogs.com/leiOOlei/p/3548290.html#q3

Spring 入門知識點筆記整理

配置文件如:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 5         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 6     <bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
 7         init-method="hhhh">
 8     </bean>
 9 
10 
11     <bean id="moocBeanNameAware" class="imooc_spring.test.aware.MoocBeanNameAware"></bean>
12 
13     <!-- 測試 SpEL: 可以為屬性進行動態的賦值(了解) -->
14     <bean id="girl" class="com.helloworld.User">
15         <property name="userName" value="周迅"></property>
16     </bean>
17 
18     <!-- <bean id="boy" class="com.helloworld.User" init-method="init" destroy-method="destroy"> 
19         <property name="userName" value="高勝遠"></property> <property name="wifeName" 
20         value="#{girl.userName}"></property> </bean> -->
21 
22     <bean id="girl2" class="com.helloworld.User2">
23         <property name="userName" value="Talor Swift"></property>
24     </bean>
25     
26     <!-- autowired測試,自動裝配測試 -->
27     <bean id="people" class="test.spring.autowired.Person" autowire="byName">
28         <property name="name" value="小明"></property>
29         <!-- <property name="cat" ref="cat1"></property> -->
30     </bean>
31     
32     <!-- <bean id="cat" class="test.spring.autowired.Cat">
33         <property name="name" value="我是小喵喵"></property>
34     </bean> -->
35     
36     <bean id="cat222" class="test.spring.autowired.Cat">
37         <property name="name" value="我是小喵喵"></property>
38     </bean>
39 </beans>

 

 

Person:

 1 package test.spring.autowired;
 2 
 3 public class Person {
 4     /**
 5      * Person要使用自動裝配Cat類,那么就必須有Cat屬性
 6      * <bean id="cat" class="test.spring.autowired.Cat"> <property name="name"
 7      * value="我是小喵喵"> </property> </bean>
 8      */
 9     private Cat cat;
10     private String name;
11 
12     public Person() {
13 
14     }
15 
16     public Person(Cat cat, String name) {
17         this.cat = cat;
18         this.name = name;
19     }
20 
21     public Cat getCat() {
22         return cat;
23     }
24 
25     public void setCat(Cat cat) {
26         this.cat = cat;
27     }
28 
29     /**
30      * 如果Person類的Bean設置了autowire="byName",那么首先第一步:
31      * Spring框架根據Person這個bean找到對應的Person類,比如Person類里有String name以及Cat cat 這兩個屬性,
32      * 然后依次在IOC容器(Spring配置文件)里的<property name="name" value="小明"></property>這樣的
33      * 配置來給Person這個bean進行賦值(通過Person類的setter方法,具體到Person類,是通過setName(String)來實現的),
34      * 如果在Person 這個bean的配置里沒有找到Person類的Cat屬性,而Person Bean又是自動裝配,那么就在配置文件里找到
35      * class屬性為Cat的Bean對象(<bean id="cat222" class="test.spring.autowired.Cat">
36         <property name="name" value="我是小喵喵"></property></bean>),然后通過這個Bean的配置id屬性值
37         在Person類里找到對應的setter()方法,具體到這里就是在Person里找setCat222的setter()方法,
38         經過測試,實際上在Person類里寫成setcat222(Cat cat)也可以
39      * 
40      * @param cat
41      */
42     public void setCat222(Cat cat) {
43         this.cat = cat;
44     }
45 
46     public String getName() {
47         return name;
48     }
49 
50     public void setName(String name) {
51         this.name = name;
52     }
53 
54     public void introduceSelf() {
55         if (cat == null) {
56             System.out.println("我是" + this.name + ",我還沒有小貓,貓:" + this.cat);
57             return;
58         } else {
59             System.out.println("my name is " + this.name + ",i have a Pet," + cat.whoAmI());
60         }
61     }
62 }

 

Cat:

package test.spring.autowired;

/**
 * Cat類,實現Animal接口
 * 
 * @author Wei
 *
 */
public class Cat implements Animal {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Cat(String name) {
        this.name = name;
    }

    public Cat() {
    }

    @Override
    public String whoAmI() {
        // TODO Auto-generated method stub
//        System.out.println("I am an animal,my name is " + this.name);
        return this.name;
    }

}

 

Animal:

 1 package test.spring.autowired;
 2 /**
 3  * 動物接口,測試Spring自動裝配
 4  * @author Wei
 5  *
 6  */
 7 public interface Animal {
 8     /**
 9      * 用來給子類實現
10      */
11     public String whoAmI();
12 }

測試類:

 1 package test.spring.autowired;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.util.Pub;
 7 
 8 public class AutoWiredTest {
 9     @Test
10     public void test(){
11         ClassPathXmlApplicationContext ctx = new Pub().getBeanCtx();
12         Person p = (Person) ctx.getBean("people");
13         p.introduceSelf();
14         ctx.close();
15     }
16     public static void main(String[] args) {
17         ClassPathXmlApplicationContext ctx = new Pub().getBeanCtx();
18         Person p = (Person) ctx.getBean("people");
19         p.introduceSelf();
20         ctx.close();
21     }
22 }

main方法的運行結果:

  my name is 小明,i have a Pet,我是小喵喵

具體項目:

imooc_spring不能刪除,在自動裝配這個文章里要用到.rar

 

Spring 的自動裝配的過程:

如果Person類的Bean設置了autowire="byName",那么首先第一步:
 Spring框架根據Person這個bean找到對應的Person類,比如Person類里有String name以及Cat cat 這兩個屬性,
 然后依次在IOC容器(Spring配置文件)里的<property name="name" value="小明"></property>這樣的
 配置來給Person這個bean進行賦值(通過Person類的setter方法,具體到Person類,是通過setName(String)來實現的),
 如果在Person 這個bean的配置里沒有找到Person類的Cat屬性,而Person Bean又是自動裝配,那么就在配置文件里找到
 class屬性為Cat的Bean對象(<bean id="cat222" class="test.spring.autowired.Cat">
<property name="name" value="我是小喵喵"></property></bean>),然后通過這個Bean的配置id屬性值
在Person類里找到對應的setter()方法,具體到這里就是在Person里找setCat222的setter()方法,
經過測試,實際上在Person類里寫成setcat222(Cat cat)也可以

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM