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复制出来的,亲测可用,如有疑问的地方请联系我。