IOC相当于一个“代理”的角色,这是其核心思想。资源不由使用资源的双方管理,而由不使用资源的第三方管理,有很多优点。
第一,资源集中管理,实现资源的可配置和易管理。第二,降低了使用资源双方的依赖程度,也就是我们说的耦合度。传统应用程序都是由我们在类内部主动创建依赖对象,也就是“正转”,从而导致类与类之间高耦合,难于测试;有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,对象被动地接受依赖对象,也就是“反转”,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。
使用Maven配置Spring,实现ioc
例如:http://www.open-open.com/lib/view/open1326850984030.html
1、maven-archetype-quickstart 创建project
2、将QuizMaster、SpringQuizMaste、StrutsQuizMaster、QuizMasterService、QuizProgram类放入res/main/java文件夹下
3、修改pom.xml,需要添加Spring 依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.ioc</groupId>
<artifactId>IOC_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>IOC_test</name>
<url>http://maven.apache.org</url>
<properties>
<jettyVersion>9.3.7.v20160115</jettyVersion>
</properties>
<dependencies>
<span style="white-space:pre"> </span><dependency>
<span style="white-space:pre"> </span><groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
<!-- spring 依赖 -->
<dependency>
<span style="white-space:pre"> </span><groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<span style="white-space:pre"> </span><version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<span style="white-space:pre"> </span><plugin>
<groupId>org.codehaus.mojo</groupId>
<span style="white-space:pre"> </span> <artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<span style="white-space:pre"> </span><execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<span style="white-space:pre"> </span><mainClass>my.ioc.IOC_test.QuizProgram</mainClass>
</configuration>
<span style="white-space:pre"> </span></plugin>
</plugins>
</build>
</project>
新建src/main/resources 源文件夹,在这个目录下新建beans.xml
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<bean id="springQuizMaster" class="my.ioc.IOC_test.SpringQuizMaster"></bean>
<bean id="strutsQuizMaster" class="my.ioc.IOC_test.StrutsQuizMaster"></bean>
<bean id="quizMasterService" class="my.ioc.IOC_test.QuizMasterService">
<property name="quizMaster" ref="strutsQuizMaster"/>
</bean>
</beans>
my.IOC.test代表类所在的packge
5、修改QuizProgram.java
package my.ioc.IOC_test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuizProgram {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
QuizMasterService quizMasterService=(QuizMasterService)beanFactory.getBean("quizMasterService");
quizMasterService.askQuestion();
}
}
注:ClassPathXmlApplicationContext 类的使用可参考 http://www.blogjava.net/xcp/archive/2011/06/22/352821.html
6、运行结果:
由于beans.xml 文件中“ref=strutsQuizMaster”, 如果修改为“ref=springQuizMaster”,则运行结果为