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)也可以

 

posted @ 2016-07-22 16:24  Sunor  阅读(6050)  评论(1编辑  收藏  举报