java框架学习日志

引自http://blog.csdn.net/csh624366188/article/details/7651702/

   **spring AOP:面向切面**

切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在spring AOP中,切面可以使用基于模式)或者基于Aspect注解方式来实现。通俗点说就是我们加入的切面类(比如log类),可以这么理解。
连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在Spring AOP中,一个连接点总是表示一个方法的执行。通俗的说就是加入切点的那个点
通知(Advice):在切面的某个特定的连接点上执行的动作。其中包括了“around”、“before”和“after”等不同类型的通知(通知的类型将在后面部分进行讨论)。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
切入点(Pointcut):匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。
引入(Introduction):用来给一个类型声明额外的方法或属性(也被称为连接类型声明(inter-type declaration))。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存机制。
目标对象(Target Object): 被一个或者多个切面所通知的对象。也被称做被通知(advised)对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理(proxied)对象。
AOP代理(AOP Proxy):AOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
织入(Weaving):把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。

通知类型:
前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
异常通知(After throwing advice):在方法抛出异常退出时执行的通知。
最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。
环绕通知是最常用的通知类型。和AspectJ一样,Spring提供所有类型的通知,我们推荐你使用尽可能简单的通知类型来实现需要的功能。例如,如果你只是需要一个方法的返回值来更新缓存,最好使用后置通知而不是环绕通知,尽管环绕通知也能完成同样的事情。用最合适的通知类型可以使得编程模型变得简单,并且能够避免很多潜在的错误。比如,你不需要在JoinPoint上调用用于环绕通知的proceed()方法,就不会有调用的问题。

第一种是基于xml配置文件方式的实现
XML配置中的AOP实现
<?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:aop="http://www.springframework.org/schema/aop"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>  
    <bean id="registerDaoImpl" class="com.zxf.dao.RegisterDaoImpl"/>  
    <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">  
        <property name=" registerDaoImpl " ref=" RegisterDaoImpl "/>  
    </bean>  
    <!-- 日志切面类 -->  
    <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>  
    <!-- 第1步: AOP的配置 -->  
    <aop:config>  
        <!-- 第2步:配置一个切面 -->  
        <aop:aspect id="logAspect" ref="logAspectBean">  
            <!-- 第3步:定义切入点,指定切入点表达式 -->  
            <aop:pointcut id="allMethod"   
                expression="execution(* com.zxf.service.*.*(..))"/>   
            <!-- 第4步:应用前置通知 -->  
            <aop:before method="before" pointcut-ref="allMethod" />  
            <!-- 第4步:应用后置通知 -->  
            <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>  
            <!-- 第4步:应用最终通知 -->  
            <aop:after method="after" pointcut-ref="allMethod"/>  
            <!-- 第4步:应用抛出异常后通知 -->  
            <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>  
            <!-- 第4步:应用环绕通知 -->  
            <!--  
            <aop:around method="doAround" pointcut-ref="allMethod" /> 
             -->  
        </aop:aspect>  
    </aop:config>  
</beans>  

第二种基于注解的AOP的实现

 首先创建一个用来作为切面的类LogAnnotationAspect,同时把这个类配置在spring的配置文件中。
    在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了对AspectJ基于注解的切面的支持,从而 更进一步地简化AOP的配置。具体的步骤有两步。

Spring的配置文件是如下的配置:

[html] view plain copy print?
<?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:aop="http://www.springframework.org/schema/aop"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>  
    <bean id="registerDao" class="com.zxf.dao.RegisterDaoImpl"/>  
    <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">  
        <property name="registerDao" ref="registerDao"/>  
    </bean>  
    <!-- 把切面类交由Spring容器来管理 -->  
    <bean id="logAspectBean" class="com.zxf.aspect.LogAnnotationAspect"/>  
    <!-- 启用spring对AspectJ注解的支持 -->  
    <aop:aspectj-autoproxy/>  
</beans>  
/** 
 * 日志切面类 
 */  
@Aspect  //定义切面类  
public class LogAnnotationAspect {  
    @SuppressWarnings("unused")  
    //定义切入点,提供一个方法,这个方法的名字就是改切入点的id  
    @Pointcut("execution(* com.zxf.service.*.*(..))")  
    private void allMethod(){}  
    //针对指定的切入点表达式选择的切入点应用前置通知  
    @Before("execution(* com. zxf.service.*.*(..))")  
    public void before(JoinPoint call) {  
        String className = call.getTarget().getClass().getName();  
        String methodName = call.getSignature().getName();  
        System.out.println("【注解-前置通知】:" + className + "类的"   
                + methodName + "方法开始了");  
    }  
    //访问命名切入点来应用后置通知  
    @AfterReturning("allMethod()")  
    public void afterReturn() {  
        System.out.println("【注解-后置通知】:方法正常结束了");  
    }  
    //应用最终通知  
    @After("allMethod()")  
    public void after(){  
        System.out.println("【注解-最终通知】:不管方法有没有正常执行完成,"   
                + "一定会返回的");  
    }  
    //应用异常抛出后通知  
    @AfterThrowing("allMethod()")  
    public void afterThrowing() {  
        System.out.println("【注解-异常抛出后通知】:方法执行时出异常了");  
    }  
    //应用周围通知  
    //@Around("allMethod()")  
    public Object doAround(ProceedingJoinPoint call) throws Throwable{  
        Object result = null;  
        this.before(call);//相当于前置通知  
        try {  
            result = call.proceed();  
            this.afterReturn(); //相当于后置通知  
        } catch (Throwable e) {  
            this.afterThrowing();  //相当于异常抛出后通知  
            throw e;  
        }finally{  
            this.after();  //相当于最终通知  
        }  
        return result;  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值