package com.controller;

import com.aop_proxy_jdk.Advice;
import com.aop_proxy_jdk.TargetImpl;
import com.aop_proxy_jdk.TargetInterface;
import com.exception.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ExceptionTestController {
    @RequestMapping("/exception1")
    @ResponseBody // 异常测试
    public Map exception1() {
        HashMap stringStringHashMap = new HashMap<String, String>();
        stringStringHashMap.put("1","2");
        throw new MyException();
//        return stringStringHashMap;
    }
    @RequestMapping("/exception2")
    @ResponseBody //
    public Map exception2() {
        TargetImpl target = new TargetImpl();
        Advice advice = new Advice();
        TargetInterface targetInterface = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        advice.before();
                        method.invoke(target,args);
                        advice.after();
                        return null;
                    }
                }
        );
        targetInterface.save();
        return null;
//        return stringStringHashMap;
    }

}
