美文网首页
Java反射机制

Java反射机制

作者: _花 | 来源:发表于2022-09-08 15:01 被阅读0次

    第一种方法

    调用处

    public String invokeMethod(Map<String, String> params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            ApplicationContext applicationContext = ApplicationContextHolder.getApplicationContext();
            Class<?>  aClass = applicationContext.getBean("UserService").getClass();
            Method method = aClass.getDeclaredMethod("selectUser", String.class);
            Map<String, String> data = new HashMap<String, String>();
            data.put("listId", "123");
            CommonResult result = (CommonResult) method.invoke(applicationContext.getBean("UserService"),"吕茹花");
            log.info(result.toString());
            return "aa";
        }
    

    ApplicationContextHolder.java

    package com.baidu.aduhdmap.platform.common;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Service;
    
    @Service("applicationContextProvider")
    public class ApplicationContextHolder implements ApplicationContextAware {
        private static ApplicationContext appContext;
    
        /**
         *
         * @param ctx
         * @throws BeansException
         */
        @Override
        public void setApplicationContext(ApplicationContext ctx) throws BeansException {
            ApplicationContextHolder.appContext = ctx;
        }
    
        /**
         *
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return appContext;
        }
    
    }
    

    第二种 动态代理

    调用处

    public class UserProxyTest { 
    
    @Test
    public void test() { 
    
    IUserDao target = new UserDao();
    System.out.println("目标对象信息:" + target.getClass());
    //获取代理类实例对象
    IUserDao proxy = (IUserDao) new UserProxy(target).getProxyInstance();
    System.out.println("代理对象信息:" + proxy.getClass());
    //执行代理方法
    proxy.insert();
    }
    }
    

    UserProxy.java

    package com.baidu.aduhdmap.platform.common;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    public class UserProxy {
    
        private Object target; //目标对象
        public UserProxy(Object target) {
    
            this.target = target;
        }
        /** * 利用JDK API获取到代理对象 * @return */
        public Object getProxyInstance() {
    
    //目标对象的加载器
            ClassLoader loader = target.getClass().getClassLoader();
    //目标对象的实现接口类型
            Class<?>[] interfaces = target.getClass().getInterfaces();
    //InnvocationHandler事件处理器实例对象
            InvocationHandler h = new InvocationHandler() {
    
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
                    System.out.println("添加数据前:手动开启事务");
    // 执行目标对象方法
                    Object value = method.invoke(target, args);
                    System.out.println("添加数据后:手动提交事务");
                    return null;
                }
            };
    //传入3个参数,创建代理类的实例对象,并返回
            return Proxy.newProxyInstance(loader, interfaces,h);
        }
    }
    

    参考 https://javaforall.cn/172696.html

    相关文章

      网友评论

          本文标题:Java反射机制

          本文链接:https://www.haomeiwen.com/subject/yfdonrtx.html