定义一个抽象的类(接口):
package springIOC;
/**
@author:micro_hz
2015年8月12日
*/
public interface Animals {
//定义方法
void eat();
void roar();
void run();
}
定义两个实现类:
package springIOC;
/**
@author:micro_hz
2015年8月12日
*/
public class Cat implements Animals{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("i am cat and i am eating");
}
@Override
public void roar() {
// TODO Auto-generated method stub
System.out.println("i am cat and i am roaring");
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("i am cat and i am running");
}
}
package springIOC;
/**
@author:micro_hz
2015年8月12日
*/
public class Dog implements Animals{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("i am dog and i am eating");
}
@Override
public void roar() {
// TODO Auto-generated method stub
System.out.println("i am dog and i am roaring");
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("i am dog and i am running");
}
}
然后在另一个类中直接使用接口:
package springIOC;
/**
@author:micro_hz
2015年8月12日
*/
public class DayLife {
//声明接口
Animals animal;
void print()
{
animal.eat();
animal.roar();
animal.run();
}
public Animals getAnimal() {
return animal;
}
public void setAnimal(Animals animal) {
this.animal = animal;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<description>Spring公共配置 </description>
//a1的实现类为Cat,a2的实现类为Dog
<bean id="a1" class="springIOC.Cat"></bean>
<bean id="a2" class="springIOC.Dog"></bean>
//d1的实现类为DayLife属性为a1的实现类Cat
<bean id="d1" class="springIOC.DayLife">
<property name="animal">
<ref bean = "a1"></ref>
</property>
</bean>
</beans>
测试:
package springIOC;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
@author:micro_hz
2015年8月12日
*/
public class Test {
public static void main(String args[])
{
ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Animals c = (Animals)context.getBean("a1");
c.eat();
c.roar();
c.run();
Animals d = (Animals)context.getBean("a2");
d.roar();
d.run();
d.eat();
DayLife dl = (DayLife)context.getBean("d1");
dl.print();
}
}
运行结果:
i am cat and i am eating
i am cat and i am roaring
i am cat and i am running
i am dog and i am roaring
i am dog and i am running
i am dog and i am eating
i am cat and i am eating
i am cat and i am roaring
i am cat and i am running
必须要在DayLife里添加animal的setter实现对animal的依赖注入。