Replacement method implementation of Spring method injection in JSP development

韦小宝
Release: 2018-01-18 09:53:12
Original
1633 people have browsed it

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; }
Copy after login

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; } } 
Copy after login

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; } } }
Copy after login

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

attributes, name and replacer. name is used to specify the name of the method that needs to be replaced, and replacer is used to specify the bean corresponding to the MethodReplacer to be replaced. Therefore, at this time our beanA should be defined as follows:

   
Copy after login

If the method that our MethodReplacer will replace belongs to the

overloadedtype method in the corresponding bean, that is, there are multiple When there are two methods with the same method name, we also need to define the type of the corresponding method parameter through the arg-type element under the replaced-method element, so that we can distinguish which method needs to be replaced. Therefore, for the above example, we can also define it as follows:

      
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!