Spring框架原理(一)-- IOC与AOP

1 篇文章 0 订阅
1 篇文章 0 订阅

一、Spring介绍

 Spring 作为现在最优秀的框架之一,已被广泛的使用。Spring 框架中的核心组件只有三个:Core、Context 和 Beans。它们构建起了整个 Spring 的骨骼架构。没有它们就不可能有 AOP、Web 等上层的特性功能。

 Java 1.3之后一个重要特征是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,spring就是通过反射来实现注入的。而Spring的AOP实现是基于反射和jdk动态代理的配合使用。

随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合作,随即分工出现。如何划分模块,如何定义接口方便分工成为软件工程设计中越来越关注的问题。良好的模块化具有以下优势:可扩展、易验证、易维护、易分工、易理解、代码复用。

     优良的模块设计往往遵守“低耦合高内聚”的原则。而“框架”是对开发中良好设计的总结,把设计中经常使用的代码独立出来,所形成的一种软件工具。用户遵守它的开发规则,就可以实现良好的模块化,避免软件开发中潜在的问题。广义上的框架无处不再,一个常见的例子就是PC硬件体系结构,人们只要按照各自需要的主板、显卡、内存等器件就可以任意组装成自己想要的电脑。而做主板的厂商不用关心做显卡厂商的怎么实现它的功能。软件框架也是如此,开发人员只要在Spring框架中填充自己的业务逻辑就能完成一个模块划分清晰纷的系统。

    可以通过一个银行通知用户月收支记录的小例子来介绍轻型J2EE框架Spring的主要内容、它所解决的问题和实现的方法。

Spring框架主要可以分为3个核心内容:

        1、容器

        2、控制反转(IoCInversion of Control

        3、面向切面编程(AOPAspect-OrientedProgramming

      例子中依次对这些特性进行介绍,描述了软件模块化后存在的依赖与问题,以及Spring框架如何解决这些问题。 

二、example

 假设有一个如下应用场景:(1)一个银行在每月的月初都需要向客户发送上个月的账单,账单发送的方式可以为纸质邮寄、或者短信方式。(2)还有一个潜在的需求:为了安全起见,在每个函数操作过程中都需要记录日志,记录参数传入是否正常,函数是否正常结束,以便出错时系统管理员查账。



  首先定义一个账单输出的接口:

//接口

public interface ReportGenerator{

    public void generate(String[][]table) ;

}

实现“打印纸质账单”与“发送短信”两个具体功能:

//账单报表实现类

public class PageReportGeneratorimplement ReportGenerator {

    public void generate(String[][]table) {

        log4j.info(... );    //输出日志

        ...打印操作,以便工作人员邮递给客户

        log4j.info(... );    //输出日志

    }

}

//短信报表实现类

public class SMSReportGeneratorimplement ReportGenerator {

    public void generate(String[][]table) {

        log4j.info(... );

        ...短信发送操作

        log4j.info(... );

     }

}

上层业务逻辑对上个月的账目进行统计并调用接口产生纸质或者短信结果:

//上层业务中的服务类

publicclassReportService{

    privateReportGenerator reportGenerator =newSMSReportGenerator();

    publicvoidgenerateMonthlyReport(intyear, intmonth) {

        log4j.info( ... );

        String[][]statistics = null;

        ...

        reportGenerator.generate(statistics);

    }


 A、模块化后出现的问题与隐患

        假设随着工程的复杂化,上面的例子需要分成两个模块,以便开发时分工,一般会以如下结构划分:


    划分后再看原来的代码: 

//上层业务中的服务类

public class ReportService{

    private ReportGenerator reportGenerator = new SMSReportGenerator();//隐患

 

    public void generateMonthlyReport(int year, int month) {

        ...

    }

}

在服务类有privateReportGenerator reportGenerator = new SMSReportGenerator();这么一行代码,ReportService类与SMSReportGenerator类不属于同一个模块,当开发人员B对内部实现进行修改时,由于存在依赖,开发人员A也要进行修改(比如之前喜欢短信收账单的客户感觉短信不够详细,希望以后改用邮件收账单,那么开发人员B需要实现一个MailReportGenerator类,在开发人员B修改代码时,开发人员A也需要改代码------声明部分修改)。如果系统庞大new 
SMSReportGenerator()
大量使用的话,修改就会十分复杂,一个声明没有修改就会出现大的BUG

    所以需要一种划分,让各个模块尽可能独立,当开发人员B修改自己的模块时,开发人员A不需要修改任何代码。

 B、问题出现的原因

    为例子中的程序画一个UML依赖图:


    可以发现上述问题出现的原因主要是:模块A与模块B不但存在接口依赖,还存在实现依赖。ReportGenerator每次修改它的实现,都会对ReportService产生影响。那么需要重构消除这种实现依赖。

C、用容器解决问题

     消除实现依赖一般可以通过添加一个容器类来解决。在例子程序容器代码如下:

//容器类

public class Container {

 

    public static Container instance;

 

    private Map<String, Object> components;

 

    public Container(){

        component = newHashMap<String,Object>();

        instance = this;

 

        ReportGenertor reportGenertor = newSMSReportGenertor();

        components.put(“reportGenertor”,reportGenertor);

 

        ReportService reportService = newReportService();

        components.put(“reportService”,reportService);

    }

 

    public Object getComponent(String id){

        return components.get(id);

    }

}

使用容器后,模块A的ReportService的属性实现方法也发生了变化。

//服务类变更,降低了耦合


public class ReportService{

 

    //private ReportGenerator reportGenerator =new SMSReportGenerator();

    private ReportGenerator reportGenerator =(ReportGenerator) Container.instance.getComponent(“reportGenerator”);

 

    public void generateMonthlyReport(int year, int month) {

        ...

    }

}

 这样的话,class都在容器中实现,使用者只需要在容器中查找需要的实例,开发人员修改模块B后(在模块中增加邮件报表生成类MailReportGenerator),只需要在容器类中修改声明(把ReportGenertor 
reportGenertor = new SMSReportGenertor();改为ReportGenertorreportGenertor = new 
MailReportGenertor();)即可,模块A不需要修改任何代码。一定程度上降低了模块之间的耦合。

3、Spring中的控制反转

A、还存在的耦合

     使用容器后模块A与模块B之间的耦合减少了,但是通过UML依赖图可以看出模块A开始依赖于容器类:

 


  之前的模块A对模块B的实现依赖通过容器进行传递,在程序中用(ReportGenerator) Container.instance.getComponent(reportGenerator)的方法取得容器中SMSReportGenertor的实例,这种用字符(reportGenerator)指代具体实现类SMSReportGenertor的方式并没有完全的解决耦合。所以在银行账单的例子中我们需要消除ReportService对容器Container的依赖。

    B
、控制反转与依赖注入

    在我们常规的思维中,ReportService需要初始化它的属性privateReportGenerator reportGenerator就必须进行主动搜索需要的外部资源。不使用容器时,它需要找到SMSReportGenertor()的构造函数;当使用容器时需要知道SMSReportGenertor实例在容器中的命名。无论怎么封装,这种主动查找外部资源的行为都必须知道如何获得资源,也就是肯定存在一种或强或弱的依赖。那是否存在一种方式,让ReportService不再主动初始化reportGenerator,被动的接受推送的资源?

    这种反转资源获取方向的思想被称为控制反转(IoCInversion of Control),使用控制反转后,容器主动地将资源推送给需要资源的类(或称为beanReportService,而ReportService需要做的只是用一种合适的方式接受资源。控制反转的具体实现过程用到了依赖注入(DIDependecncy Injection)的设计模式,ReportService类接受资源的方式有多种,其中一种就是在类中定义一个setter方法,让容器将匹配的资源注入:setter的写法如下: 

//为需要依赖注入的类加入一种被称为setter的方法

 

public class ReportService{

 

    /*private ReportGenerator reportGenerator =

        (ReportGenerator)Container.instance.getComponent(“reportGenerator”); */

 

    private ReportGenerator reportGenerator;

 

    public void setReportGenerator( ReportGeneratorreportGenerator) {

        this.reportGenerator = reportGenerator;

    }

 

    public void generateMonthlyReport(int year, int month) {

        ... 

    }

}

//容器类  

public class Container {

      

    ...

    public Container ( ) {

        component = newHashMap<String,Object>();

        instance = this;

        ReportGenertor reportGenertor = newSMSReportGenertor();

        components.put(“reportGenertor”,reportGenertor);

 

        ReportService reportService = newReportService();

        reportService.setReportGenerator(reportGenerator);//使用ReportService的setter方法注入依赖关系

        components.put(“reportService”,reportService);

    }

    ...

}

  这样一来ReportService就不用管SMSReportGenertor在容器中是什么名字,模块A对于模块B只有接口依赖,做到了松耦合。

C、Spring IoC容器的XML配置

    每个使用Spring框架的工程都会用到容器与控制反转,为了代码复用,Spring把通用的代码独立出来形成了自己的IoC容器供开发者使用:



 与上面例子中实现的容器相比,Spring框架提供的IoC容器要远远复杂的多,但用户不用关心Spring  

IoC容器的代码实现,Spring提供了一种简便的bean依赖关系配置方式------使用XML文件,在上面的例子中,配置依赖关系只要在工程根目录下的“application.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: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="smsReportGenerator"class="bank.SMSReportGenerator" />

 

    <bean id="reportService"class="bank.ReportService"> 

       <propertyname="reportGenerator"ref="smsReportGenerator" />

    </bean>

 </beans>

 <?xml version="1.0" encoding="UTF-8"?>是标准的XML头,xmlns引用的是一些命名空间,两个一般在工程中自动生成。后面的内容由用户输入,主要表示实例化SMSReportGenerator,实例化ReportService并把SMSReportGenerator的对象smsReportGenerator赋值给ReportService的属性reportGenerator,完成依赖注入。

4Spring中的面向切面编程

 

    A、日志问题以及延伸

    在例子的需求中有一条是:需要记录日志,以便出错时系统管理员查账。回顾例子中的代码,在每个方法中都加了日志操作: 

//服务类

public class ReportService{

    ...

    public void generateMonthlyReport(int year, int month) {

        log4j.info( ... );   //记录函数的初始状况参数等信息

        String[ ][ ] statistics = null;

        ...

        reportGenerator.generate(statistics);

        log4j.info( ... );   //记录函数的执行状况与返回值

    }

}

/凭条报表实现类  

public class PageReportGeneratorimplement ReportGenerator {

 

    public void generate(String[ ][ ] table) {

 

        log4j.info( ...);       //记录函数的初始状况参数等信息

        打印操作

        log4j.info( ...);       //记录函数的执行状况与返回值

    }

}

  可以看出在每个方法的开始与结尾都调用了日志输出,这种零散的日志操作存在着一些隐患,会导致维护的困难。比如日志输出的格式发送了变化,那么无论模块A还是模块B的程序员都要对每个方法每个输出逐条修改,极容易遗漏,造成日志输出风格的不一致。又比如不用Log4j日志输出工具更换其他工具,如果遗漏一个将会出现严重BUG

    与日志输出相似的问题在编程中经常遇到,这种跨越好几个模块的功能和需求被称为横切关注点,典型的有日志、验证、事务管理等。


横切关注点容易导致代码混乱、代码分散的问题。而如何将很切关注点模块化是本节的重点。
 
    B
、代理模式

    传统的面向对象方法很难实现很切关注点的模块化。一般的实现方式是使用设计模式中的代理模式。代理模式的原理是使用一个代理将对象包装起来,这个代理对象就取代了原有对象,任何对原对象的调用都首先经过代理,代理可以完成一些额外的任务,所以代理模式能够实现横切关注点。


 可能在有些程序中有很多横切关注点,那么只需要在代理外再加几层代理即可。以银行账单为例介绍一个种用Java Reflection API动态代理实现的横切关注点模块化方法。系统提供了一个InvocationHandler接口


//系统提供的代理接口

public interface InvocationHandler{

    public Object invoke(Object proxy, Method method,Object[] args) throwThrowable;

}

我们需要实现这个接口来创建一个日志代理,实现代码如下:

//日志代理实现  

public class LogHandlerimplement InvocationHandler{

 

    private Object target;

 

    public LogHandler(Object target){

        this.target = target;

    }

    public Object invoke(Object proxy, Method method,Object[] args ) throw Throwable{

 

        //记录函数的初始状况参数等信息

        log4j.info(“开始:方法”+method.getName() + “参数”+Arrays.toString(args) );

  

 

        Object result = method.invoke(target,args);

 

        //记录函数的执行状况与返回值

        log4j.info(“结束:方法”+method.getName() + “返回值”+ result );

 

    }

 }

    这样既可以使得日志操作不再零散分布于各个模块,易于管理。调用者可以通过如下方式调用:

//主函数  

public class Main{

    public static void main(String[ ] args){

        ReportGenerator reportGeneratorImpl  =newSMSReportGenerator ();

 

        //通过系统提供的Proxy.newProxyInstance创建动态代理实例

        ReportGenerator reportGenerator =(ReportGenerator ) Proxy.newProxyInstance( 

            reportGeneratorImpl.getClass().getClassLoader(),

            reportGeneratorImpl.getClass().getInterfaces(),

            new LogHandler(reportGeneratorImpl)

        ) ;

        ...

    }

}

  代理模式很好的实现了横切关注点的模块化,解决了代码混乱代码分散问题,但是我们可以看出用 Java Reflection API 实现的动态代理结构十分复杂,不易理解,Spring框架利用了代理模式的思想,提出了一种基于JAVA注解(Annotation)和XML配置的面向切面编程方法(AOPAspect-Oriented Programming)简化了编程过程。

    CSpring AOP 使用方法

    Spring AOP使用中需要为横切关注点(有些时候也叫切面)实现一个类,银行账单的例子中,切面的实现如下:

//切面模块实现  

@Aspect    //注解1

public class LogAspect{

 

    @Before(“execution(**.*(..))”)    //注解2

    public void LogBefore(JoinPoint joinPoint)  throw Throwable{

        log4j.info(“开始:方法”+joinPoint.getSignature().getName() );

    }

 

    @After(“execution(**.*(..))”)     //注解3

    public void LogAfter(JoinPoint joinPoint)  throw Throwable{

        log4j.info(“结束:方法”+joinPoint.getSignature().getName() );

    }

}

@Aspect
public class LogAspect {
	
	@Before("execution(* *.*(..))")
	public void logBefore(JoinPoint jionPoint){
		System.out.println("假设这是一个日志:"+jionPoint.getTarget().getClass().getName()+"."+jionPoint.getSignature().getName()+"方法开始执行!");
	}
	
	@After("execution(* *.*(..))")
	public void logAfter(JoinPoint jionPoint){
		System.out.println("假设这是一个日志:"+jionPoint.getTarget().getClass().getName()+"."+jionPoint.getSignature().getName()+"方法执行完成!");
	}
}

public class Main {
	
	public static void main(String[] args) {
	
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ReportService reportService = (ReportService)context.getBean("reportService");
		reportService.generateMonthlyReport(2011, 7);
		
	}
	
}


  注解1表示这个类是一个切面,注解2"* *.*(..)* "是一个通配符,表示在容器中所有类里有参数的方法。@Before(execution(* *.*(..)))表示在所有类里有参数的方法前调用切面中德LogBefore()方法。同理,注解3@After(execution(* *.*(..)))表示在所有类里有参数的方法执行完后调用切面中的LogAfter()方法。
    
实现完切面类后,还需要对Spring工程中的application.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:p="http://www.springframework.org/schema/p"

    xmlns:aop="http://www.springframework.org/schema/aop"   

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 

    <aop:aspectj-autoproxy/>

    <bean id="smsReportGenerator"class="bank.SMSReportGenerator" />

    <bean id="reportService"class="bank.ReportService">

        <propertyname="reportGenerator"ref="smsReportGenerator" />

    </bean>

    <bean class="bank.LogAspect"/>

</beans>

 这比之前IoC依赖关系配置的XML文件多了:xmlns:aop=http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
3个主要是声明XML中用于AOP的一些标签, <beanclass="bank.LogAspect" />是在容器中声明LogAspect切面,<aop:aspectj-autoproxy />用于自动关联很切关注点(LogAspect)与核心关注点(SMSReportGeneratorReportService)。不难发现Spring AOP的方法实现横切关注点得模块化要比用Java ReflectionAPI简单很多。

5Spring总结

 

         银行月账单报表例子通过使用Spring框架后变成了如下结构:


 Spring框架的基础上原来存在耦合的程序被分成松耦合的三个模块。无论那个模块修改,对其他模块不需要额外改动。这就完成了一种良好的架构,使软件易理解,模块分工明确,为软件的扩展、验证、维护、分工提供了良好基础。这就是Spring框架作用。当然Spring除了容器、控制反转、面向切面之外还有许多其他功能,但都是在这三个核心基础上实现的。

参考:

http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/
http://blog.csdn.net/yangdelong/article/details/4728488
http://blog.csdn.net/it_man/article/details/4402245

http://my.oschina.net/myriads/blog/37922



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值