Implementation of replacement methods for Spring method injection in JSP development, jspspring

Source: Internet
Author: User

Implementation of replacement methods for Spring method injection in JSP development, jspspring

Replacement of Spring method injection in JSP development

Spring provides a replacement method implementation mechanism that allows us to change the implementation of a bean method. For example, we have a bean with an add () method that can be used to calculate the sum of two integers, but at this time, we want to change its implementation logic to multiply them if the values of the two integers are the same. Otherwise, we will add them together, without changing or changing the source code, we can use the replacement method implementation mechanism provided by Spring to achieve this requirement.

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. obj indicates the bean object to be replaced by the method implementation, the method to be replaced, and args indicates the corresponding method parameter. For the preceding example, assume that 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 the values when a and B are equal, otherwise the values are added, we can provide a corresponding MethodReplacer implementation class for this method, the specific implementation is as follows.

Public class BeanAReplacer implements MethodReplacer {/*** @ param obj corresponds to the target object, that is, beanA * @ param method corresponds to the target method, that is, add * @ param args corresponds to the target parameter, that is, a and B */public Object reimplement (Object obj, Method method, Object [] args) throws Throwable {Integer a = (Integer) args [0]; integer B = (Integer) args [1]; if (. equals (B) {return a * B;} else {return a + B ;}}}

Then you need to specify BeanAReplacer to replace beanA's add () method when defining beanA, which is specified through the replaced-method element. You must specify two attributes: name and replacer. Name is used to specify the name of the method to be replaced, while replacer is used to specify the bean corresponding to MethodReplacer to be replaced. Therefore, beanA should be defined as follows:

<bean id="beanAReplacer" class="com.app.BeanAReplacer"/> <bean id="beanA" class="com.app.BeanA"> <replaced-method name="add" replacer="beanAReplacer"/> </bean>

If the method to be replaced by our MethodReplacer belongs to the overload type method in the corresponding bean, that is, when multiple methods with the same name exist, we also need to use the arg-type element under the replaced-method element to define the type of the corresponding method parameter, so that we can distinguish the method to be replaced. Therefore, for the above example, we can also define the following:

<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>

If there is only one method corresponding to the method name, arg-type does not work, that is, Spring will not replace the corresponding method according to arg-type at this time, in other words, when the replaced-method specifies only one method, no matter how the arg-type is defined.

The above is an example of implementation of the replacement method for Spring method injection in JSP. I hope it will help you. If you have any questions, please leave a message. Thank you for reading this article and hope it will help you, thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.