zoukankan      html  css  js  c++  java
  • Spring ProxyFactory

    ProxyFactory 是 Spring AOP的实现方式之一。下面介绍下ProxyFactory的用法。

    1、接口定义

    public interface UserReadService {
    
        public UserInfo getUserInfoById(Long id);
    }

    2、接口实现

    public class UserReadServiceImpl implements UserReadService {
    
        @Override
        public UserInfo getUserInfoById(Long id) {
            System.out.println("获取用户信息");
            return null;
        }
    
    }

    3、拦截器定义

    public class UserInterceptor implements MethodInterceptor {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("start");
                Object obj = invocation.proceed();
                System.out.println("end");
                return obj;
            }
            
    }

    4、测试

        public static void main(String[] args) {
            ProxyFactory factory = new ProxyFactory(new UserReadServiceImpl());
            factory.addAdvice(new UserInterceptor());
            UserReadService userReadService = (UserReadService) factory.getProxy();
            userReadService.getUserInfoById(null);
        }

     结果:

    start
    获取用户信息
    end
  • 相关阅读:
    Valid Anagram
    Spiral Matrix II
    Spiral Matrix
    Kth Smallest Element in a BST
    Count Primes
    javascript 判断浏览器
    javascript 数值交换技巧
    EntityFramework 6 分页模式
    JSON.parse 和 JSON.stringify
    CSS z-index
  • 原文地址:https://www.cnblogs.com/chenmo-xpw/p/5538663.html
Copyright © 2011-2022 走看看