您的位置:首页 > 编程语言 > Java开发

Spring 4.0.2 学习笔记(1) - 最基本的注入

2014-04-16 22:05 211 查看
1、 添加maven支持

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


基本的注入,只要这一个依赖项即可

2、 编写几个测试类

假设场景: 有一个程序员jimmy, 他有几台电脑, 一个宠物,每天下班回家后,宠物会用某种形式欢迎他回家.

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();
}

}


运行效果:
My name is jimmy.yang, and I have 2 computers:
Lenovo ThinkPad
Apple MAC Book
And I have a pet, everyday,when I go home, it will welcome me by
wang! wang!

下一篇将学习如何自动注入,以及如何使用properties属性文件来注入对象属性.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: