1. 程式人生 > >Spring 4.0- 最基本的註入

Spring 4.0- 最基本的註入

mac book post blog system .cn extend 淺談 contex return

Spring 非常好用,其中就與他的註入有關,今天,淺談一下Spring的基本註入,大神請飄過。

1、 添加maven支持

   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.2.RELEASE</version>
     </dependency> 

基本的註入,只要這一個依賴項即可,我親測。

2、 編寫幾個測試類

  假設一個場景以便於代碼邏輯

  

package com.cnblogs.yjmyzz.domain;

public abstract class Computer {

    public abstract void showInfo();
}
package com.cnblogs.yjmyzz.domain;

public class MacBook extends Computer {
    @Override
    public void showInfo() {
        System.out.println("\tApple MAC Book");
    }
}

package com.cnblogs.yjmyzz.domain;

public class ThinkPad extends Computer {

    @Override
    public void showInfo() {
        System.out.println("\tLenovo ThinkPad");
    }
}
package com.cnblogs.yjmyzz.domain;

public abstract class Pet {

    public abstract void welcomeMeToHome();
}
package
com.cnblogs.yjmyzz.domain; public class Dog extends Pet { @Override public void welcomeMeToHome() { System.out.println("\twang! wang!"); } }

package com.cnblogs.yjmyzz.domain;

import java.util.List;


public class Programmer {

    private String name;

    private Pet pet;

    private List<Computer> computers;


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

    public List<Computer> getComputers() {
        return computers;
    }

    public void setComputers(List<Computer> computers) {
        this.computers = computers;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public void show() {

        System.out.print("My name is " + name);
        System.out.print(", and I have " + computers.size() + " computer" + (computers.size() > 1 ? "s" : "") + ":");
        System.out.println();
        for (Computer c : computers) {
            c.showInfo();
        }
        System.out.println("And I have a pet, everyday,when I go home, it will welcome me by  ");
        pet.welcomeMeToHome();

    }


}

3、 spring的配置文件,這裏是最基本配置文件,其余配置請自行添加

<?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="jimmy" class="com.cnblogs.yjmyzz.domain.Programmer">
        <property name="name" value="jimmy.yang"/>
        <property name="pet" ref="wangcai"/>
        <property name="computers">
            <list>
                <ref bean="t60"/>
                <ref bean="macbook_pro"/>
            </list>
        </property>
    </bean>

    <bean id="t60" class="com.cnblogs.yjmyzz.domain.ThinkPad"/>
    <bean id="macbook_pro" class="com.cnblogs.yjmyzz.domain.MacBook"/>
    <bean id="wangcai" class="com.cnblogs.yjmyzz.domain.Dog"/>

</beans>

這個配置文件SimpleBeans.xml中,一共配置了4個bean實例, 該配置被Spring容器加載後,這些對象都會被實例化(默認是單例模式實例化),並保持在容器中,需要使用的時候,可以手動通過ApplicationContext的getBean方法獲取

4、 測試註入

為了方便,需要寫一個工具類SpringUtils

package com.cnblogs.yjmyzz.utils;

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


public class SpringUtils {

    private static ApplicationContext applicationContext;

    private static ApplicationContext getContext() {

        if (applicationContext == null) {
            applicationContext =  new ClassPathXmlApplicationContext("SimpleBeans.xml");

        }
        return applicationContext;

    }

    public static  <T> T getBean(Class<T> clazz, String beanName) {

        return getContext().getBean(beanName, clazz);

    }
}

然後就可以用這個工具類,通過getBean來獲取註入的對象實例

package com.cnblogs.yjmyzz.test;

import com.cnblogs.yjmyzz.domain.Programmer;
import com.cnblogs.yjmyzz.utils.SpringUtils;
import org.junit.Test;

public class TestSpring {

    @Test
    public void testSimpleInject(){
        Programmer jimmy=  SpringUtils.getBean(Programmer.class,"jimmy");
        jimmy.show();
    }

}

以上代碼從我的myeclipse復制出來的,親測可用,如有疑問的地方請聯系我。

Spring 4.0- 最基本的註入