本节学习下依赖注入IOC:
废话少说,直接上例子,例子是模拟小狗和人是怎么说话的。
首先,定义Living接口以及它的两个实现Person和Dog
Living.java
package ioc;
/**
* @ClassName: Living
* @Description: 有生命的所有物体
* @author sunrain
* @date 2011-1-1
*/
public interface Living {
/**
* @Title: say
* @Description: 所有物体的说的表现
* @param @return
* @return String 说出来的东东
* @throws
*/
public String say();
}
Person.java
package ioc;
/**
* @ClassName: Person
* @Description: 人
* @author sunrain
* @date 2011-1-1
*/
public class Person implements Living {
public String say() {
// TODO Auto-generated method stub
return "您好!";
}
}
package ioc;
/**
* @ClassName: Dog
* @Description: 狗
* @author sunrain
* @date 2011-1-1
*/
public class Dog implements Living {
public String say() {
// TODO Auto-generated method stub
return "汪汪汪";
}
}
然后定义Living要做的任务(接口以及实现):
package ioc;
/**
* @ClassName: Task
* @Description: 定义各种任务
* @author sunrain
* @date 2011-1-1
*/
public interface Task {
/**
* @Title: eat
* @Description: 任务 -说
* @param 设定文件
* @return void 返回类型
* @throws
*/
public void say();
}
package ioc;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @ClassName: TaskImpl
* @Description: Task实现类
* @author sunrain
* @date 2011-1-1
*/
public class TaskImpl implements Task {
private static final Log log = LogFactory.getLog(TaskImpl.class);
/**
* @Fields living : 有生命的东东
*/
private Living living ;
private String name;
public TaskImpl(String name){
this.name = name;
}
public Living getLiving() {
return living;
}
public void setLiving(Living living) {
this.living = living;
}
public void say() {
log.info(this.name+" say :"+living.say());
}
}
写了一堆类,好像没讲IOC,那么 TaskImpl实现类中,我们定义了两个属性,living和name,就是我们要依赖注入的两个属性了,来看下XML吧
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloWorld" class="HelloWorldServiceImpl"> <property name="name"> <value>sunrain</value> </property> </bean> <bean id="personTask" class="ioc.TaskImpl"> <constructor-arg> <value>sunrain</value> </constructor-arg> <property name="living" ref="person"></property> </bean> <bean id="dogTask" class="ioc.TaskImpl"> <constructor-arg> <value>tt</value> </constructor-arg> <property name="living" ref="dog"></property> </bean> <bean id="person" class="ioc.Person"> </bean> <bean id="dog" class="ioc.Dog"> </bean> </beans>
好像都写完了,最后来写个测试类,测试下效果吧
package ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class IocTest {
/**
* @Title: main
* @Description: IOC测试类
* @param @param args 设定文件
* @return void 返回类型
* @throws
*/
public static void main(String[] args) {
Resource res = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(res);
Task t = (Task) factory.getBean("personTask");
t.say();
t = (Task)factory.getBean("dogTask");
t.say();
}
}
再看看日志:
0103/15:10:58 <INFO > [org.springframework.core.CollectionFactory 66] JDK 1.4+ collections available 0103/15:10:58 <INFO > [org.springframework.core.CollectionFactory 71] Commons Collections 3.x available 0103/15:10:58 <INFO > [org.springframework.beans.factory.xml.XmlBeanDefinitionReader 163] Loading XML bean definitions from file [G:\workspace\SpringStudy\beans.xml] 0103/15:10:58 <INFO > [ioc.TaskImpl 32] sunrain say :您好! 0103/15:10:58 <INFO > [ioc.TaskImpl 32] tt say :汪汪汪
在测试类中,我们并没有初始化TaskImpl类中的两个属性living和name,但是日志却打印出来了,这就要归功于IOC了,看看XML的配置你就会发现,在XML配置中,我们已经为TaskImpl的属性都指定了相应的值。
<bean id="personTask" class="ioc.TaskImpl"> <constructor-arg> <value>sunrain</value> </constructor-arg> <property name="living" ref="person"></property> </bean> <bean id="dogTask" class="ioc.TaskImpl"> <constructor-arg> <value>tt</value> </constructor-arg> <property name="living" ref="dog"></property> </bean>
由于对IOC的一些其他特性还不熟习,先写这样了,以后再补充了。