Spring 6.0官方文档示例(11) ProxyFactoryBean的用法

文章展示了如何在Java中定义一个普通的service类,然后通过创建配置类来设置ProxyFactoryBean,结合AOP的概念,添加MethodBeforeAdvice和MethodInterceptor。在测试类中,通过ProxyFactoryBean获取代理对象并调用service类的方法,展示方法调用前后的拦截处理。
摘要由CSDN通过智能技术生成

一、定义普通service类

package cn.edu.tju.service;


public class MyService {

    public void method1() {
        System.out.println("I am method 1");
    }

    public void method2() {
        System.out.println("I am method 2");
    }

    public void method3() {
        System.out.println("I am method 3");
    }
}

二、创建java 配置类,并在其中创建ProxyFactoryBean,供

package cn.edu.tju.config;


import cn.edu.tju.service.MyService;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import java.lang.reflect.Method;

@Configuration
public class MyConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }

    //MethodBeforeAdvice
    @Bean
    public MethodBeforeAdvice beforeAdvice() {
        MethodBeforeAdvice advice = new MethodBeforeAdvice() {

            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("will call :"+ method);
                System.out.println("args are: "+args);
            }
        };
        return advice;
    }



    //MethodInterceptor
    @Bean
    public MethodInterceptor methodInterceptor() {
        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                long starTime = System.nanoTime();
                Object re = invocation.proceed();
                long endTime = System.nanoTime();
                System.out.println(invocation.getMethod() + " :" + (endTime - starTime));
                return re;
            }
        };
        return methodInterceptor;
    }

    //ProxyFactoryBean
    @Bean
    public ProxyFactoryBean proxyFactoryBean() {
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTargetName("myService");
        proxyFactoryBean.setInterceptorNames("beforeAdvice", "methodInterceptor");
        return proxyFactoryBean;
    }
}

三、创建容器,使用ProxyFactoryBean

package cn.edu.tju;

import cn.edu.tju.config.MyConfig;
import cn.edu.tju.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestProxyFactoryBean {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        MyService bean = context.getBean("proxyFactoryBean", MyService.class);
        System.out.println("--------------------------------------------");
        bean.method1();
        System.out.println("--------------------------------------------");
        bean.method2();
        System.out.println("--------------------------------------------");
        bean.method3();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值