<IDEA>动态代理添加日志的方法(proxy<代理>和invoke的简单用法)

 
public interface PersonDao {
   public void Song(String name);
   public void Dance(String name);

}

1.首先创建一个dao层接口,任意方法,

接着实现接口方法(@Repository是spring中的bean的注解)

@Repository
public class PersonDaoImpl implements PersonDao{

    @Override
    public void Song(String name) {
        System.out.println("刘德华唱了一首"+name);
    }

    @Override
    public void Dance(String dance) {
        System.out.println("刘德华跳了一个"+dance);
    }
}

。。创建日志工具类(这里根据需求设置参数)

public class LogFactroy {
    public static void LogBefore(String dao,String method,Object... args){
        System.out.println("前置日志是:当前执行的是"+dao+"的"+method+"的方法,参数是"+args);
    }
    public static void LogAfter(String dao,String method,Object... args){
        System.out.println("后置日志是:当前执行的是"+dao+"的"+method+"的方法,参数是"+args);
    }
    public static void LogThrowing(String method,Exception e){
        System.out.println("异常日志是:当前异常的的是"+method+"方法,错误是"+e);
    }
    public static void LogAfterReturn(String dao,String method,Object result){
        System.out.println("返回日志是:当前返回的的是"+dao+"的"+method+"的方法的结果"+result);
    }

3.创建代理对象

步骤如下①创建静态方法

②通过"Proxy"类提供的一个newProxyInstance方法用来创建一个对象的代理对象newProxyInstance方法用来返回一个代理对象

3个参数分别为ClassLoader loader,Class<?>[] interfaces, InvocationHandler h

ClassLoader loader用来指明生成代理对象使用哪个类装载器

Class<?>[] interfaces用来指明生成哪个对象的代理对象

InvocationHandler h会重写一个方法,用来指明产生的这个代理对象要做什么事情(在这个方法里去调用我们写好的日志工具)

public class PersonDaoProxy {
    public static Object createPersonProxy(Object target){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result=null;
                try {
                    try {
                        LogFactroy.LogBefore(target.getClass().getName(),method.getName(),args);
                     // 调用目标对象的方法
                        result=method.invoke(target,args);
                        LogFactroy.LogAfter(target.getClass().getName(),method.getName(),args);
                    }finally {
                        LogFactroy.LogAfterReturn(target.getClass().getName(),method.getName(),result);
                    }
                }catch (Exception e){
                        LogFactroy.LogThrowing(method.getName(),e);
                }
                return result;
            }
        });
    }
}

4.测试

<!--JUnit4测试单元-->
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
<!--Spring自动注入(xml需配置包扫描 
<context:component-scan base-package="cn.pht"></context:component-scan>)-->

    @Autowired
    PersonDao personDao;

    @Test
    public void test1(){
<!--用代理对象类PersonDaoProxy的方法去运行对象(才会调用代理对象类中的代理方法)-->
        PersonDao personDaoProxy= (PersonDao) PersonDaoProxy.createPersonProxy(personDao);
        personDaoProxy.Dance("江南style");
        personDaoProxy.Song("王妃");
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值