目的:
解决对象间的依赖。
核心概念:
Spring最重要的核心概念是nverse of control,中文常译为”控制反转”,更具体的一个名字是Dependency Injection ,中文常译为”依赖注入”,使用spring,你不必自己在程序代码强护对象的依赖关系,只需在配置文件中加以定,spring核习容器会自动根据配置将依赖注入指定的对象。
Ioc的抽象概念是:”依赖关系的转移”。
Ioc的一些表现:”实现必须依赖抽象,而不是抽象依赖实现”,”应用程序不应依赖于容器,而是容器服务于应用程序”。
两种IoC类型:
1、setter注入:
优点:setter可以从父类继承
缺点:setter调用的次序不定
2、构造函数注入:
优点:一旦创建,组件始终处于一致的可用状态
缺点:不可被继承
如果需要注入的组件太多,会使构造函数的参数过长。
实例演示(Java Project):
Action:
<span style="font-size:14px;">public class PomainAction {
private IPomainService pomainService;
public PomainAction(){}
public PomainAction(IPomainService pomainService){
this.pomainService = pomainService;
}
//完成增加采购单的请求
public void addPomain(){
pomainService.addPomain();
}
public IPomainService getPomainService() {
return pomainService;
}
public void setPomainService(IPomainService pomainService) {
this.pomainService = pomainService;
}
}</span>
Dao:
<span style="font-size:14px;">//负责明细单的操作
public class PoitemDao {
public void addItem(){
System.out.println("增加明细单");
}
}</span><span style="font-size: 18px;">
</span>
<pre name="code" class="java"><span style="font-size:14px;">//负责采购单主单的操作
public class PomainDao {
public void add(){
System.out.println("增加主单");
}
}</span>
</pre><p></p><pre>
<span style="font-size:14px;">//负责产品的操作
public class ProductDao {
<span style="white-space: pre;"> </span>public void update(){
<span style="white-space: pre;"> </span>System.out.println("修改产品的在途数");
<span style="white-space: pre;"> </span>}
}</span>
Service接口:
public interface IPomainService {
void addPomain();
}
Service实现类:
<span style="font-size:14px;">public class PomainServiceImpl implements IPomainService{
private PomainDao pomainDao;
private PoitemDao poitemDao;
private ProductDao productDao;
public PomainServiceImpl(){}
public PomainServiceImpl(PomainDao pomainDao, PoitemDao poitemDao,
ProductDao productDao) {
this.pomainDao = pomainDao;
this.poitemDao = poitemDao;
this.productDao = productDao;
}
public void addPomain(){
System.out.println("设置connection不是自动提交");
pomainDao.add();
poitemDao.addItem();
productDao.update();
System.out.println("提交");
}
public PomainDao getPomainDao() {
return pomainDao;
}
public void setPomainDao(PomainDao pomainDao) {
this.pomainDao = pomainDao;
}
public PoitemDao getPoitemDao() {
return poitemDao;
}
public void setPoitemDao(PoitemDao poitemDao) {
this.poitemDao = poitemDao;
}
public ProductDao getProductDao() {
return productDao;
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
}</span>
applicationContext.xml (唯一的配置文件):
<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
<bean id="pomainDao" class="com.aowin.dao.PomainDao"></bean>
<bean id="poitemDao" class="com.aowin.dao.PoitemDao"></bean>
<bean id="proDao" class="com.aowin.dao.ProductDao"></bean>
<!-- set方式注入,要有相应的set和get方法 -->
<bean id="pomainService" class="com.aowin.service.PomainServiceImpl">
<property name="pomainDao" ref="pomainDao"></property>
<property name="poitemDao" ref="poitemDao"></property>
<property name="productDao" ref="proDao"></property>
</bean>
<bean id="pomainAction" class="com.aowin.action.PomainAction">
<property name="pomainService" ref="pomainService"></property>
</bean>
<!-- 构造方法注入 -->
<bean id="pomainService2" class="com.aowin.service.PomainServiceImpl">
<constructor-arg index="0" ref="pomainDao"></constructor-arg>
<constructor-arg index="1" ref="poitemDao"></constructor-arg>
<constructor-arg index="2" ref="proDao"></constructor-arg>
</bean>
<bean id="pomainAction2" class="com.aowin.action.PomainAction">
<constructor-arg index="0" ref="pomainService2"></constructor-arg>
</bean>
</beans></span>
Test:
<span style="font-size:14px;">public class Test {
public static void main(String[] args) {
//使用spring获取对象
// BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
// PomainDao mainDao = (PomainDao) factory.getBean("pomainDao");
// mainDao.add();
// BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
// PomainAction action = (PomainAction) factory.getBean("pomainAction");
// action.addPomain();
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
PomainAction action = (PomainAction) factory.getBean("pomainAction2");
action.addPomain();
}
}</span>
使用Spirng Ioc之前的Test(比较作用):
<span style="font-size:14px;">public class Test {
public static void main(String[] args) {
//构造方法注入
// PomainDao mainDao = new PomainDao();
// PoitemDao itemDao = new PoitemDao();
// ProductDao proDao = new ProductDao();
// IPomainService pomainService = new PomainServiceImpl(mainDao, itemDao, proDao);
// PomainAction action = new PomainAction(pomainService);
// action.addPomain();
//set方式注入
PomainDao mainDao = new PomainDao();
PoitemDao itemDao = new PoitemDao();
ProductDao proDao = new ProductDao();
PomainServiceImpl pomainService = new PomainServiceImpl();
PomainAction action = new PomainAction();
pomainService.setPomainDao(mainDao);
pomainService.setPoitemDao(itemDao);
pomainService.setProductDao(proDao);
action.setPomainService(pomainService);
action.addPomain();
}
}</span>
包结构:
总结:
1、重构前的项目,包结构与重构后的基本一致,唯一的不同之处便是缺少了applicationContext.xml配置文件。
2、重构前的项目,各个文件内容与重构后的一致,唯一不同的是test文件,重构前在test文件中对象之间的依赖关系较强,重构后在配置文件中解决依赖关系。
3、Spring IOC的作用就是解决对象之间的依赖关系,使用时不需要繁杂的new对象,对象之间的依赖关系只需要在配置文件中进行一次配置,可以达到多次使用。
备注:
此文总结自培训时期的Spring框架的第一课《Spring1》