This article mainly introduces JSP information related to the replacement method implementation of Springmethod injectionin JSP development. Friends who are interested in JSP can refer to this article
JSP development of Spring method injection replacement method implementation
Spring provides a mechanism for replacement method implementation, which allows us to change the implementation of a certain bean method. For example, we have a bean with an add() method that can be used to calculate the sum of two integers, but this time we want to change its implementation logic to multiply the two integers if they have the same value. Otherwise, add them together. We can achieve this requirement through the replacement method implementation mechanism provided by Spring without changing or unable to change the source code.
The core of the replacement method implementation mechanism is the MethodReplacer interface, which defines a reimplement () method. The main logic of our replacement method implementation is implemented in this method,
The specific definition is as follows:
public interface MethodReplacer { /** * Reimplement the given method. * @param obj the instance we're reimplementing the method for * @param method the method to reimplement * @param args arguments to the method * @return return value for the method */ Object reimplement(Object obj, Method method, Object[] args) throws Throwable; }
We can see that the reimplement() method will receive three parameters, where obj represents the beanobject that needs to be replaced by the method implementation, and method needs to be replaced. , args represents the corresponding method parameters. For the previous example, suppose we have a bean corresponding to the following class definition.
public class BeanA { public int add(int a, int b) { return a+b; } }
public class BeanAReplacer implements MethodReplacer { /** * @param obj 对应目标对象,即beanA * @param method 对应目标方法,即add * @param args 对应目标参数,即a和b */ public Object reimplement(Object obj, Method method, Object[] args) throws Throwable { Integer a = (Integer)args[0]; Integer b = (Integer)args[1]; if (a.equals(b)) { return a * b; } else { return a + b; } } }
How to test the JSP page outside the container
How to use JSP to connect to the MySQL database
JSP basic knowledge points summary
The above is the detailed content of Replacement method implementation of Spring method injection in JSP development. For more information, please follow other related articles on the PHP Chinese website!