反转控制和依赖注入
Spring Frame的核心特性之一就是两个紧密相关观念的支持:反转控制(Ioc)和依赖注入(DI)。
Ioc是一种设计模式:组装器(这里是Spring Framework)将在运行时而不是编译时绑定对象,通过使用这种方式,应用程序开发者可以针对一组接口进行编程,这样可以在不同环境中进行切换,而无需重新编译代码。
依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念。具体含义是:当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入
尽管理论上可以通过许多方式实现IoC,但DI是最常见的技术。通过使用DI,一段程序代码(Spring Framework中的一个类)可以声明它依赖于另一块程序代码(一个接口),然后组装器可以在运行时注入它的依赖实例。
接口:
package com.xia.services; public interface Person { public void run(); public void say(); }
实现类1:
package com.xia.service.impl; import com.xia.services.Person; public class PersonImpl1 implements Person{ @Override public void run() { // TODO Auto-generated method stub System.out.println("1run"); } @Override public void say() { // TODO Auto-generated method stub System.out.println("1say"); } }
实现类2:
package com.xia.service.impl; import com.xia.services.Person; public class PersonImpl2 implements Person{ @Override public void run() { // TODO Auto-generated method stub System.out.println("2run"); } @Override public void say() { // TODO Auto-generated method stub System.out.println("2say"); } }
普通实例化对象,new,Test类:
package com.xia.test; import static org.junit.Assert.*; import com.xia.service.impl.PersonImpl1; import com.xia.service.impl.PersonImpl2; import com.xia.services.Person; public class Test { @org.junit.Test public void test() { Person p1 =new PersonImpl2(); p1.say(); p1.run();
Person p2 =new PersonImpl2();
p2.say();
p2.run();
} }
工厂设计模式:
创建一个工厂类:
package com.xia.services; import com.xia.service.impl.PersonImpl1; import com.xia.service.impl.PersonImpl2; public class Factory { private static final String PERSON1 = "person1"; private static final String PERSON2 = "person2"; public Person personFactory(String person){ if(person.equals(PERSON1)){ return new PersonImpl1(); }else if(person.equals(PERSON2)){ return new PersonImpl2(); }else{ throw new IllegalArgumentException("参数错误"); } } }
Test:
package com.xia.test; import static org.junit.Assert.*; import com.xia.service.impl.PersonImpl1; import com.xia.service.impl.PersonImpl2; import com.xia.services.Factory; import com.xia.services.Person; public class Test { @org.junit.Test public void test() { Person p2 =new Factory().personFactory("person2"); p2.say(); p2.run(); Person p1 =new Factory().personFactory("person1"); p1.say(); p1.run(); } }
Ioc和DI:
删除工厂类,在项目的根目录下创建bean.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="person1" class="com.xia.service.impl.PersonImpl1"></bean> <!--name为对象名,class为对象所属的类的全限定名 --> <bean name="person2" class="com.xia.service.impl.PersonImpl2"></bean> </beans>
Test:
package com.xia.test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xia.services.Person; public class Test { @org.junit.Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //读取xml中的文件 Person p1 = ac.getBean("person1",Person.class); //获取xml文件中name为“person1”的bean对象,并赋给p1.
p1.say(); p1.run(); Person p2 = ac.getBean("person2",Person.class); p2.say(); p2.run(); } }

浙公网安备 33010602011771号