手动实现AOP自动切入(基于jdk动态代理)

Controller

package myapplication.com.Aop.controller;
import myapplication.com.Aop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/aop")
public class UserController {
    @Autowired
    @Qualifier("userServiceProxyInstance")
    UserService userService;

    @RequestMapping("/userInfo")
    public String getUserNameAndAddress(){
        String userNameAndAddress = userService.getUserNameAndAddress();
        return userNameAndAddress;
    }

    @RequestMapping("/checkUser")
    public boolean checkUserNameAndAddress(){
        boolean checkUserNameAndAddress = userService.checkUserNameAndAddress();
        return checkUserNameAndAddress;
    }
}

Service

package myapplication.com.Aop.service;

public interface UserService {
    public String getUserNameAndAddress();
    public boolean checkUserNameAndAddress();
}

ServiceImp

package myapplication.com.Aop.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userServiceInstance")
public class UserServiceImp implements UserService{
    @Override
    public String getUserNameAndAddress() {
        System.out.println("getUserNameAndAddress admin - 123456");
        return "admin - 123456";
    }
    @Override
    public boolean checkUserNameAndAddress() {
        System.out.println("checkUserNameAndAddress true");
        return true;
    }
}

InvocationHandler -- utils

package myapplication.com.Aop;
import myapplication.com.Aop.service.UserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyAopInvocationHandler implements InvocationHandler {
    private UserService userService;
    public MyAopInvocationHandler(UserService userService){
        this.userService = userService;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");
        //真正执行service层的方法
        Object invoke = method.invoke(userService,args);
        System.out.println("after");
        return invoke;
    }
}

config配置类

package myapplication.com.Aop.Config;
import myapplication.com.Aop.MyAopInvocationHandler;
import myapplication.com.Aop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

@Configuration
public class ProxyInstance {
    @Autowired
    @Qualifier("userServiceInstance")
    UserService userService;

    @Bean
    public MyAopInvocationHandler myAopInvocationHandler(){
        return new MyAopInvocationHandler(userService);
    }
    
    @Bean("userServiceProxyInstance")
    public UserService getProxy(@Qualifier("myAopInvocationHandler") InvocationHandler h){
        Object o = Proxy.newProxyInstance(this.getClass().getClassLoader(), userService.getClass().getInterfaces(), h);
        return   (UserService)o;
    }
}

访问http://localhost:8081/aop/userInfo , http://localhost:8081/aop/userInfo
控制台打印

注:核心在于除了必须实现InvocationHandler接口和用Proxy
生成代理instance外,还要注入代理对象,其实真正是调用代理对象的方法。
详见jdk动态代理原理:
https://www.jianshu.com/p/8a286d964932

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容