Spring代理

动态代理的过程

1、编写代理接口

public interface Sale {
    void chuzu(int yuan);
    void eat();
}

2、编写真实对象

public class FangDong implements Sale{
    @Override
    public void chuzu(int yuan) {
        System.out.println("房东收到的金额:"+yuan);
    }

    @Override
    public void eat() {
        System.out.println("我要吃饭了");
    }
}

3、使用Proxy.newProxyInstance()获得代理对象

public class Main {
    public static void main(String[] args) {
        //new原始对象
        FangDong fangDong = new FangDong();
        /***
         * 获得代理对象
         * 第一个参数是被代理实体的ClassLorder
         * 第二个参数是被代理实体的Interface
         * 第三个参数时实现InvocatinnHandler接口的invoke方法
         */
        Sale proxy = (Sale) Proxy.newProxyInstance(
                fangDong.getClass().getClassLoader(),
                fangDong.getClass().getInterfaces(),
                new InvocationHandler() {
                    /***
                     *
                     * @param proxy 代理对象,一般不用
                     * @param method 方法的封装类,用于获得方法名等信息,并可以执行方法
                     * @param args 当前被使用的方法的一些参数
                     * @return
                     * @throws Throwable
                     */
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("我是代理对象,我开始工作了");
                        Object res=null;
                        /***
                         * 通过获得方法判断当前对象执行的方法是否为需要增强的方法
                         */
                        if(method.getName().equals("chuzu")){
                            int y= (int) ((int)args[0]*0.2);
                            System.out.println("中介收小费:"+y);
                            args[0] = (int) args[0] - y;

                        }
                        res=method.invoke(fangDong,args);
                        return  res;
                    }
                });
        proxy.chuzu(2000);
        proxy.eat();
    }
}

spring AOP的两种实现方式

1、配置xml文件开启AOP

  1. 先编写切入后所需要执行的方法
public class LogClass {
    public void before(){
        System.out.println("前置增强");
    }
    public void after(){
        System.out.println("后置增强");
    }
}
  1. 在xml进行配置,开启AOP
<!--    加载增强需要执行的方法的对象-->
    <bean id="log" class="com.qst.log.LogClass"/>
<!--    手动配置aop-->
    <aop:config>
<!--        配置AOP切面-->
        <aop:aspect ref="log">
<!--            配置切入点,且切入点可以配置多个-->
            <aop:pointcut id="logPointcut" expression="execution(* com.qst.pojo.*.eat(..))"/>
<!--            开启最终通知,无论系统是否报异常,都会执行-->
            <aop:after method="after" pointcut-ref="logPointcut"/>
<!--            开启前置通知,在方法运行前先执行这个通知-->
            <aop:before method="before" pointcut-ref="logPointcut"/>
        </aop:aspect>
    </aop:config>

2、注解实现AOP

  1. 编写增强类,加上注解
@Aspect
public class LogClass {
    /***
     * 抽取切点
     */
    @Pointcut("execution(* com.qst.pojo.*.*(..))")
    public void myPointcut(){}
//
//    @Before("myPointcut()")
//    public void before(){
//        System.out.println("前置增强");
//    }
//    @AfterReturning("myPointcut()")
//    public void after(){
//        System.out.println("后置增强");
//    }

    /***
     * 环绕增强
     * @param pjt
     */
    @Around("myPointcut()")
    public void around(ProceedingJoinPoint pjt){
        try {
            System.out.println("前置增强");
            pjt.proceed();
            System.out.println("后置增强");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常增强");
        } finally {
            System.out.println("最终增强");
        }
    }
}
  1. 在xml文件中开启注解支持
<!--    加载增强需要执行的方法的对象-->
    <bean id="log" class="com.qst.log.LogClass"/>
<!--    开启aop注解依赖-->
    <aop:aspectj-autoproxy/>

JDK动态代理和CGLIB动态代理的区别

  1. JDK动态代理值提供了接口的代理,不支持类的代理;
  2. spring AOP 进行代理时若代理的对象没有实现接口,则会使用CGLIB动态代理进行处理,CGLIB在运行时动态的生成目标类的一个子类。并且会重写父类所有的方法增强代码,调用时先通过代理类进行增强,再直接调用父类对应的方法进行调用目标方法。从而实现AOP。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CSDN_SmallAnnum

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

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

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

打赏作者

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

抵扣说明:

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

余额充值