Home >Web Front-end >JS Tutorial >Replacement method implementation of Spring method injection in JSP development
This article mainly introduces JSP information related to the replacement method implementation of Springmethod injection in 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 bean object 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; } } <bean id="beanA" class="com.app.BeanA"/>If we need to replace the implementation of the add() method by multiplying a and b when they are equal, otherwise adding them, we can provide a corresponding MethodReplacer implementation class for this method, The specific implementation is as follows.
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; } } }Afterwards, you need to specify the use of BeanAReplacer to replace the add() method implementation of beanA when defining beanA, which is specified through the replaced-method element. It requires specifying two
<bean id="beanAReplacer" class="com.app.BeanAReplacer"/> <bean id="beanA" class="com.app.BeanA"> <replaced-method name="add" replacer="beanAReplacer"/> </bean>If the method that our MethodReplacer will replace belongs to the
<bean id="beanAReplacer" class="com.app.BeanAReplacer"/> <bean id="beanA" class="com.app.BeanA"> <replaced-method name="add" replacer="beanAReplacer"> <arg-type match="int"/> <arg-type match="int"/> </replaced-method> </bean>When there is only one method corresponding to the method name, arg-type will not work, that is, Spring will not use arg-type at this time. Get the corresponding method for replacement, or in other words, when there is only one method with the name specified by replaced-method, it is okay regardless of how arg-type is defined. Related recommendations:
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!