现在的位置: 首页 > 综合 > 正文

2011级-csdn-java-张侃—Spring(1)

2012年02月22日 ⁄ 综合 ⁄ 共 2464字 ⁄ 字号 评论关闭

一、开始spring之旅

Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发.

 

public class PersonServiceBean {
     private PersonDao personDao = new PersonDaoBean();
	
      public void save(Person person){
            personDao.save(person);
     }
}

PersonDaoBean 是在应用内部创建及维护的。所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。

 

依赖注入(Dependency Injection)

当我们把依赖对象交给外部容器负责创建,那么PersonServiceBean 类可以改成如下:

public class PersonServiceBean {
     private PersonDao personDao ;
    //通过构造器参数,让容器把创建好的依赖对象注入进PersonServiceBean,当然也可以使用setter方法进行注入。
     public PersonServiceBean(PersonDao personDao){
         this.personDao=personDao;
     }	
      public void save(Person person){
            personDao.save(person);
     }
}

所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中。

 

为何要使用Spring

至少在我看来,在项目中引入spring立即可以带来下面的好处

1.降低组件之间的耦合度,实现软件各层之间的解耦。
 
2.可以使用容器提供的众多服务,如:事务管理服务、消息服务等等。当我们使用容器管理事务时,开发人员就不再需要手工控制事务.也不需处理复杂的事务传播。
3.容器提供单例模式支持,开发人员不再需要自己编写实现代码。
4.容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。
5.容器提供的众多辅作类,使用这些类能够加快应用的开发,如: JdbcTemplate、 HibernateTemplate。
6.Spring对于主流的应用框架提供了集成支持,如:集成Hibernate、JPA、Struts等,这样更便于应用的开发。

如果使用Spring, 我们就不再需要手工控制事务


另外,如果使用spring, 我们也不需要处理复杂的事务传播行为

 

public void payment(){
       Bean1.update();//更新金额
       Bean2.save();//记录操作日志
}

如果我们不使用Spring,针对下面这两种业务需求,我们该如何做?
第1种可能的业务需求:要求Bean1.update()和Bean2.save()在同一个事务中执行。

第2种可能的业务需求:要求不管Bean1.update() 的事务是否成功,都需要记录操作日志

 

 

public class Bean1 { 
      public void update(){//注意:下面省略了一些代码
	 Connection conn = null;
	 conn.setAutoCommit(false);
     	 Statement.executeUpdate(“update account set amount=? where id=?"); 	
      }
}
public class Bean2 {
      public void save(){//注意:下面省略了一些代码
	 Connection conn = null;
	 conn.setAutoCommit(false);
     	 Statement.executeUpdate(“insert into Log (content) values (?)");
      }
}

使用Spring,不再需要我们处理复杂的事务传播行为
使用Spring,我们只需要通过声明式的事务属性配置就可以轻松地实现这两种业务需求

1.要求Bean1.update()和Bean2.save()的在同一个事务中执行

2.要求不管Bean1.update() 的事务是否成功,都需要记录日志。

 

 

@Transactional(propagation=Propagation.Required)
public void payment(){
       Bean1.update();//更新金额
       Bean2.save();//记录日志
}
 public class Bean1 {
      @Transactional(propagation=Propagation.Required)
      public void update(){
                    executeUpdate(“update account set amount=? where id=?"); 	
      }
}
public class Bean2 {
      @Transactional(propagation=Propagation.RequiresNew)
      public void save(){
	executeUpdate(“insert into Log (content) values (?)");
      }
}

spring的配置文件模版

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	.....
</beans>

 

该配置模版可以从spring的参考手册或spring的例子中得到。配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。

 

抱歉!评论已关闭.