spring框架 aop:aspectj-autoproxy proxy-target-class=“true“用法理解

一、场景描述

在spring框架中,集成使用AOP面向切面编程:

1、当一个类有接口的时候,那么spring默认使用的是JDK动态代理
2、如果当前类没有接口的时候,那么spring会默认使用CGLIB动态代理
3、如果一个类有接口的时候,还想要使用CGLIB动态代理,
那么就需要在spring的配置文件中加上 :
aop:aspectj-autoproxy proxy-target-class="true" 属性。

二、分别进行代码演示

先准备公共代码

切面类:MyAspect 
package com.ba01;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Date;

@Aspect
public class MyAspect {
    @Around(value = "mypt()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object object = null;
        System.out.println("环绕通知,在目标方法之前调用,输出时间," + new Date());

        //1、可以控制目标方法的执行
        String name = "";
        Object[] args = joinPoint.getArgs();
        if(args != null && args.length > 1){
            name = (String) args[0];
        }
        if(!"zhangshan".equals(name)){
            //2、目标方法的调用
            object = joinPoint.proceed();//相当于method.invoke();
        }
        System.out.println("环绕通知,在目标方法之后调用,输出时间," + new Date());
        //3、可以直接修改方法的返回值
        object = "1234werqwe";
        return object;
    }


    @AfterThrowing(value = "execution(* *(..))",throwing = "ex")
    public void myafterThrowing(Exception ex){
        System.out.println("异常通知:" + ex.getMessage());
        //可以发送邮件,短信,来通知开发人员
    }

    @Before(value = "mypt())")
    public void myafterThrowing(){
        System.out.println("前置通知:");
    }

    @Pointcut(value = "execution(* *(..))")
    private void mypt(){

    }
}

1、当一个类有接口的时候,那么spring默认使用的是JDK动态代理

接口SomeService 
public interface SomeService {
    public void doSome(String name,Integer age);
    public String doOther(String name,Integer age);
}

接口实现类SomeServiceImpl

package com.ba01;

import com.ba01.SomeService;
import org.springframework.stereotype.Component;

public class SomeServiceImpl implements SomeService{

    @Override
    public String doOther(String name, Integer age) {
        System.out.println("====doOther方法===========");
        return "abcd";
    }
    @Override
    public void doSome(String name, Integer age) {
        System.out.println("====dosome方法===========");
    }
}

spring的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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--声明目标对象-->
    <bean id="someService" class="com.ba01.SomeServiceImpl"/>
    <!--声明切面类对象-->
    <bean id="myAspect" class="com.ba01.MyAspect"/>

    <!--声明自动代理生成器
        aspectj-autoproxy:会把spring 容器中所有的目标对象,一次性的生成代理对象
    -->
    <aop:aspectj-autoproxy/>
</beans>
执行测试类,主要就是把获得的bean对象打印出来
@Test
    public void shouldAnswerWithTrue()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        SomeService someService =  (SomeService) context.getBean("someService");
        System.out.println(someService.getClass());
    }

输出结果,代表是使用的是DK动态代理

class com.sun.proxy.$Proxy14

Process finished with exit code 0

2、如果当前类没有接口的时候,那么spring会默认使用CGLIB动态代理

去掉实现类的接口,直接为普通的类

public class SomeServiceImpl{

    public String doOther(String name, Integer age) {
        System.out.println("====doOther方法===========");
        return "abcd";
    }
    public void doSome(String name, Integer age) {
        System.out.println("====dosome方法===========");
    }
}
切面类不变,spring的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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--声明目标对象-->
    <bean id="someService" class="com.ba01.SomeServiceImpl"/>
    <!--声明切面类对象-->
    <bean id="myAspect" class="com.ba01.MyAspect"/>

    <!--声明自动代理生成器
        aspectj-autoproxy:会把spring 容器中所有的目标对象,一次性的生成代理对象
    -->
    <aop:aspectj-autoproxy/>
</beans>
直接运行测试类,注意此时是用的SomeServiceImpl ,而不是接口SomeService
@Test
    public void shouldAnswerWithTrue()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        SomeServiceImpl someService =  (SomeServiceImpl) context.getBean("someService");
        System.out.println(someService.getClass());
    }
运行结果,我们发现现在变成了CGLIB动态代理,这个是spring框架自动帮我们实现的
class com.ba01.SomeServiceImpl$$EnhancerBySpringCGLIB$$4ec165aa

Process finished with exit code 0

3、如果一个类有接口的时候,还想要使用CGLIB动态代理

修改spring的xml文件
<beans>
...
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

实现类还是加上对接口的实现

public class SomeServiceImpl implements SomeService{

    @Override
    public String doOther(String name, Integer age) {
        System.out.println("====doOther方法===========");
        return "abcd";
    }
    @Override
    public void doSome(String name, Integer age) {
        System.out.println("====dosome方法===========");
    }
}
测试类泡一下
@Test
    public void shouldAnswerWithTrue()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        SomeService someService =  (SomeService) context.getBean("someService");
        System.out.println(someService.getClass());
    }

运行结果,此时虽然有实现接口,但是spring默认使用了CGLIB动态代理

class com.ba01.SomeServiceImpl$$EnhancerBySpringCGLIB$$63108b69

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的佩奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值