Spring3.0.5源码(1)

源码下载:

http://www.cnblogs.com/xing901022/p/4178963.html

一,搭建简易Spring的环境

Persion.java

package com.test.pojo;

import java.io.Serializable;

public class Persion implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 525991434733900778L;
    private String name;
    private transient Integer age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public void info(){
        System.out.println("name:"+getName()+" age:"+getAge());
    }

}

Game.java

package com.test.pojo;

import java.io.Serializable;

public class Game implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = -6645849660330772400L;
    private String gameName;
    private String gameCode;
    
    private Persion persion;                  //使用bean.xml注入Persion的Bean
    
    public void doGame(){
        System.out.println("Game : " + persion.getName() +"正在玩" + gameName);
    }
    
    
    public Persion getPersion() {
        return persion;
    }
    public void setPersion(Persion persion) {
        this.persion = persion;
    }
    public String getGameName() {
        return gameName;
    }
    public void setGameName(String gameName) {
        this.gameName = gameName;
    }
    public String getGameCode() {
        return gameCode;
    }
    public void setGameCode(String gameCode) {
        this.gameCode = gameCode;
    }
    
    @Override
    public String toString() {
        return super.toString();
    }
    
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="persion" class="com.test.pojo.Persion">          //1, 将Persion注入Spring容器
        <property name="name" value="YY"/>
        <property name="age" value="12"/>
    </bean>
    
    
    <bean id="game" class="com.test.pojo.Game">
        <property name="gameName" value="Snack"/> 
        <property name="gameCode" value="100"/>
        <property name="persion" ref="persion"/>               //2, 将persion的Bean注入Game  
    </bean>
</beans>

PersionTest.java

package com.test.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.pojo.Game;
import com.test.pojo.Persion;

public class PersionTest {
    
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");     //1, 读取bean.xml中的内容
        Game p = ctx.getBean("game",  Game.class);                                   //2, 根据Bean的名字和类型,通过Spring容器创建bean的引用对象
        p.doGame();
    }

}

 二,源码解析

 

posted @ 2017-02-24 13:44  wanhua.wu  阅读(425)  评论(0编辑  收藏  举报