Spring ProxyFactory

来源:互联网 发布:java软件工程培训 编辑:程序博客网 时间:2024/06/05 09:01
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

 

0 0