反射 getDeclaredMethod和getMethod的区别以及用法《实例》

版权声明:转载请注明出处 交流请加QQ 156356969 https://blog.csdn.net/weixin_41957098/article/details/88812629

首先介绍案例应用场景

在当前类获取当前类的方法,当获取到相对应的方法时,获取对应的方法!用反射的方法调用执行!

如果方法的修饰符为protected 请用, getDeclaredMethod 方法,为 public 请用 getMethod 方法 !

package cn.day1901.servlet;

import java.io.IOException;

import java.lang.reflect.Method;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Bservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    

    
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 客户端必须传method方法
         * 1,获取参数,用来识别处理方法
         * 2,判断是哪一个方法,是哪一个就调用哪一个
         * 得到方法名,是否可以通过反射来调用方法
         * 得到当前类的Class对象,然后调用安的方法进行查询,得到Method
         * 我们要得到当前类的方法,所以需要得到当前类的Class
         * getDeclaredMethod 可以获取任意方法 像,protected修饰的
         * getMethod 只可以获取 修饰符为 public 的方法
         */
        
        String methodName = request.getParameter("method");
        if(methodName == null || methodName.trim().isEmpty()) {
            throw new RuntimeException("您没有传递method参数,无法确定您想要调用的方法!");
        }
        Method me =null;
        try {
            me = this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);        
        } catch (Exception e) {
            
            throw new RuntimeException(e+"你要调用的方法:"+methodName+"不存在");
        }         
        Object req = null;
        Object resp = null;
        try {
            me.invoke(this, req,resp); //反射调用,this ,后面跟参数
        } catch (Exception e) {            
            throw new RuntimeException(e+"你调用方法内部"+methodName+"冒出的异常");
        }
    }
    
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        System.out.println("addUser");
    }

    
    public void editUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        System.out.println("editUser");
    }
    
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
        System.out.println("deleteUser");
    }

扫描二维码关注公众号,回复: 6021242 查看本文章

}
 

升级修改

为了让后续工作中更加便利,我们可以新建一个抽像类,把这个service 方法 放到抽象类 BaseSservlet 里,当使用时,直接 继承 它即可以,如下图

 

单独的,BaseServlet 类

package cn.day1901.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 客户端必须传method方法
         * 获取参数,用来识别处理方法,判断是哪一个方法,是哪一个就调用哪一个
         * 得到方法名,通过反射来调用方法, 得到当前类的Class对象,然后调用安的方法进行查询,得到Method
         * 我们要得到当前类的方法,所以需要得到当前类的Class
         * getDeclaredMethod 可以获取任意方法 像,protected修饰的
         * getMethod 只可以获取 修饰符为 public 的方法
         */
        
        String methodName = request.getParameter("method");
        if(methodName == null || methodName.trim().isEmpty()) {
            throw new RuntimeException("您没有传递method参数,无法确定您想要调用的方法!");
        }
        Method me =null;
        try {
            me = this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);        
        } catch (Exception e) {            
            throw new RuntimeException(e+"你要调用的方法:"+methodName+"不存在");
        }         
        Object req = null;
        Object resp = null;
        try {
            me.invoke(this, req,resp);
        } catch (Exception e) {            
            throw new RuntimeException(e+"你调用方法内部"+methodName+"冒出的异常");
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/weixin_41957098/article/details/88812629