SpringAop与AspectJ的联系与区别

6 篇文章 0 订阅
3 篇文章 0 订阅

SpringAop与AspectJ的联系与区别

区别

AspectJ

AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。

spring aop

Spring提供了四种类型的Aop支持
* 基于经典的SpringAOP
* 纯POJO切面
* @ASpectJ注解驱动的切面
* 注入式AspectJ切面(其实与Spring并无多大的关系,这个就是使用AspectJ这个框架实现Aop编程)

基于经典的SpringAop

其使用ProxyFactoryBean创建:
增强(通知)的类型有:
前置通知:org.springframework.aop.MethodBeforeAdvice
后置通知:org.springframework.aop.AfterReturningAdvice
环绕通知:org.aopalliance.intercept.MethodInterceptor
异常通知:org.springframework.aop.ThrowsAdvice

public interface IBookDao {
    public int add()
    public int delete();
}

public class BookDaoImpl implements IBookDao{
    public int add() {
        System.out.println("正在添加图书...");
        return 0;
    }
    public int delete() {
        System.out.println("正在删除图书...");
        return 0;
    }
}

//实现了MethodInterceptor的环绕增强类
public class MyAdvice implements MethodInterceptor{

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Around Advice before method invocation");
        Object o = invocation.proceed();
        System.out.println("Around Advice after method invocation");
        return o;
    }
}
//将每一个连接点都当做切点(拦截每一个方法)
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean>

    <bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean>

    <bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="bookDao"/>
        <property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/>
        <property name="interceptorNames" value="myadvice"/>
    </bean>
使用RegexMethodPointcutAdvisor针对某些特定的方法进行拦截增强
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean>

    <bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean>


    <bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"-->
        <property name="patterns" value=".*add"/>
        <property name="advice" ref="myadvice"/>
    </bean>
<!--使用的时候使用这个id,而不是原始的那个id-->
    <bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="bookDao"/>
        <property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/>
        <property name="interceptorNames" value="rmpAdvisor"/>
    </bean>
注意

像上面这样,每定义一个dao都需要定义一个ProxyFactoryBean,显得很麻烦,所以我们引入自动代理,也就是自动创建代理对象

BeanNameAutoProxyCreator
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean>

    <bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean>

    <bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"-->
        <property name="patterns" value=".*add"/>
        <property name="advice" ref="myadvice"/>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="*Dao"></property>
        <property name="interceptorNames" value="rmpAdvisor"></property>
    </bean>
DefaultAdvisorAutoProxyCreator
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean>

    <bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean>

    <bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"-->
        <property name="patterns" value=".*add"/>
        <property name="advice" ref="myadvice"/>
    </bean>

    <!--根据切面中生成信息生成代理-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
纯POJO切面,需要使用XML进行配置
public interface IBookDao {
    public int add();
    public int delete();
}

public class BookDaoImpl implements IBookDao{

    public int add() {
        int a = 1/0;
        System.out.println("正在添加图书...");
        return 0;
    }

    public int delete() {
        System.out.println("正在删除图书...");
        return 0;
    }
}
public class PojoAdvice {
    public void before(){
        System.out.println("前置通知");
    }
    public void after(Object returnval){
        System.out.println("后置通知"+",处理后的结果为:"+returnval);
    }
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前置增强...");
        Object o = proceedingJoinPoint.proceed();
        System.out.println("环绕后置增强...");
        return o;
    }
    public void afterThrowing(Throwable e){
        System.out.println("异常通知:"+e.getMessage());
    }
}
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean>

    <bean id="pojoAdvice" class="com.njust.learning.spring.pojoaop.PojoAdvice"></bean>

    <aop:config>
        <aop:pointcut id="p" expression="execution (* *.add(..))"/>
        <aop:aspect ref="pojoAdvice">
            <aop:before method="before" pointcut-ref="p"></aop:before>
            <!--通过设置returning来将返回值传递给通知-->
            <aop:after-returning method="after" pointcut-ref="p" returning="returnval"/>
            <aop:around method="around" pointcut-ref="p"/>
            <!--通过设置returning来将异常对象传递给通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="p" throwing="e"/>
        </aop:aspect>
    </aop:config>

联系

我们借助于Spring Aop的命名空间可以将纯POJO转换为切面,实际上这些POJO只是提供了满足切点的条件时所需要调用的方法,但是,这种技术需要XML进行配置,不能支持注解
所以spring借鉴了AspectJ的切面,以提供注解驱动的AOP,本质上它依然是Spring基于代理的AOP,只是编程模型与AspectJ完全一致,这种风格的好处就是不需要使用XML进行配置

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring AOPAspectJ AOP有以下几个区别: 1. 目标不同:Spring AOPSpring框架支持的面向切面编程的一部分,而AspectJ是一个独立的面向切面的框架,它扩展了Java语言并定义了AOP语法。 2. 实现方式不同:Spring AOP是在运行时进行织入的,只能针对方法进行AOP,无法针对构造函数、字段进行AOPAspectJ可以在编译成class时就进行织入,还提供了后编译器织入和类加载期织入的方式。 3. 功能差异:由于AspectJ是一个更强大的框架,它提供了更丰富的AOP功能。除了支持方法级别的切面,AspectJ还可以对构造函数、字段进行切面处理,还支持更复杂的切面表达式和切入点定义。 4. 选用原则:选择使用Spring AOP还是AspectJ AOP取决于具体的需求和项目情况。如果只需要简单的切面功能,并且已经使用了Spring框架,那么Spring AOP可以满足大部分需求。但如果需要更高级的AOP功能,或者需要在编译期进行织入,那么可以考虑使用AspectJ。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring AOPAspectJ 之间的差别](https://blog.csdn.net/xiashenbao/article/details/119953180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Spring AOPAspectJ区别](https://blog.csdn.net/qq_36259539/article/details/109140778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值