我自己在写一个简单的AOP和IOC框架,只是想搞懂内部逻辑,比较简单,项目目前只实现了IOC和文件
注解扫描,随后我会加上aop。
package aop_and_ioc.aop.proxy.stat; import java.lang.annotation.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @author 青韵 * @date 2020/8/4 - 9:20 */ public class AOPTest { public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Scanner.init(); String url = "test/hello"; Handler.handle(url); } } // AOP测试 class Interceptor1 implements Interceptor { @Override public void invoke(Invocation i) { System.out.println("before"); i.invoke(); System.out.println("after"); } } class Interceptor2 implements Interceptor { @Override public void invoke(Invocation i) { System.out.println("2222222"); i.invoke(); System.out.println(333333); } } class Test { @Before({Interceptor1.class, Interceptor2.class}) public void hello() { System.out.println("hello"); } } class Handler { public static void handle(String url) { Action action = ActionMapping.getAction(url); try { Handler.setInterceptor(action); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { System.out.println("拦截器出问题"); e.printStackTrace(); } try { Handler.exeMethod(action); } catch (InvocationTargetException | IllegalAccessException e) { System.out.println("调用方法出问题"); e.printStackTrace(); } } private static void setInterceptor(Action action) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Before before = action.method.getAnnotation(Before.class); Class<? extends Interceptor>[] classes = before.value(); action.interceptors = new Interceptor[classes.length]; for (int i = 0; i < classes.length; i++) { Constructor<? extends Interceptor> constructor = classes[i].getDeclaredConstructor(); constructor.setAccessible(true); action.interceptors[i] = constructor.newInstance(); } } private static void exeMethod(Action action) throws InvocationTargetException, IllegalAccessException { if (action.interceptors.length <= 0) { action.method.invoke(action.obj); } else { Invocation inter = new Invocation(); inter.action = action; inter.invoke(); } } } class Scanner { /** * 假定扫描类后put * @throws NoSuchMethodException */ public static void init() throws NoSuchMethodException { String url = "test/hello"; Action a = new Action(); a.controllerClass = Test.class; a.method = Test.class.getDeclaredMethod("hello"); a.obj = IOC.get(a.controllerClass); ActionMapping.put(url, a); } } class ActionMapping { public static boolean put(String url, Action action) { if (SingletonHolder.mapping.containsKey(url)) { return false; } SingletonHolder.mapping.put(url, action); return true; } public static Action getAction(String url) { return SingletonHolder.mapping.get(url); } public static Class<?> getCls(String url) { return getAction(url).controllerClass; } private static class SingletonHolder { static Map<String, Action> mapping = new ConcurrentHashMap<>(); } } class IOC { public static boolean put(Class<?> cls, Object object) { if (SingletonHolder.objectPool.containsKey(cls)) { return false; } SingletonHolder.objectPool.put(cls, object); return true; } public static Object get(Class<?> cls) { if (! SingletonHolder.objectPool.containsKey(cls)) { Object object = null; try { object = cls.getDeclaredConstructor().newInstance(); IOC.put(cls, object); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); System.out.println("IOC注入失败"); } return object; } return SingletonHolder.objectPool.get(cls); } private static class SingletonHolder { static Map<Class<?>, Object> objectPool = new ConcurrentHashMap<>(); } } class Action { Class<?> controllerClass; Object obj; Method method; Interceptor[] interceptors; } @Inherited @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @interface Before { Class<? extends Interceptor>[] value(); } interface Interceptor { void invoke(Invocation i); } class Invocation { Action action; private int index; public void invoke() { try { if (index < action.interceptors.length) { action.interceptors[index++].invoke(this); } else { action.method.invoke(action.obj); } } catch (IllegalAccessException e) { System.out.println("非法操作"); e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); System.out.println("调用目标错误"); } } }