Spring AOP Dynamic Agent Bulk Call method instance

Source: Internet
Author: User
Tags throw exception

Today the project manager sends out a task and needs to test 20 interfaces to see if the push and receive data are normal. Since the data in the docking transfer is a string in XML format, I take the ready-made data, Sir, to push past data and store it in text to verify that the data is correct, and to call these same-name methods in bulk, I think this can evolve into potential code.

Push better to do data, teammates have written code, but there is a problem, the method to the values are roughly the same, the way the package is inconsistent, many people develop, some packaging objects inside, some directly use Map.get (), alas, 1000 people 1000 Hamlet Well, have to use reflection and dynamic agent to save their own code volume, And this way of practicing practiced hand, looks like a dick, I love it.

Docking is the use of WebService-based CXF with each other's axis2 to do SOA docking. There is a problem with the compatibility of the two frameworks, but fortunately the colleague has solved it. The URL of the class is already encapsulated, written in the configuration file properties, to transfer the entity class Bean as the key, the URL of the other party is a key-value pair.

Ideas
    • To output to a file according to the existing data of the system, it is convenient to check the document by one.
    • They all implement the same interface. (Spring implements one or more interfaces to use the JDK dynamic agent, otherwise use cglib)
    • That can determine whether to implement the same interface to get the Service.
    • To make such a judgment, use reflection and dynamic proxy to invoke the method.
    • You can use the class name to get the spring-managed classes, which you can do with string arrays or collections.
    • Gets the proxy class, which is strongly converted to the business interface class and then calls the method.
    • The calling method requires a formal parameter, which is necessary to recreate, and the data is roughly the same, you can use the same member variable.
    • Go to the system first to see which batch of data is more plentiful.
    • Declare these common data.
    • Start writing.
Interface Description:
    • Interfaces inherit a top-level interface Ibasetradingcenter (tradingcenter = Trading Center)
    • The interface is divided into push ibasetradingcenterclient and received as Ibasetradingcenterserver
    • There is only one interface method synch
    • Accepts two parameters Object and map<string, object>
    • Object is an entity that assigns values to a transport bean, and MAP complements other values
    • No return value
    • Throw exception
    • Classes that implement this interface are all in spring management
The practice is this:
    • The class names of the 20 classes that implement the interface (for example: Tenderreturnservice.java) are stored directly using a string array, including push and receive (not distinguished, later differentiated), using string truncation and first character case processing.
    • Iterate through the array, get the incoming string using Spring's webapplicationcontextutils, get the proxy class for 20 Service $Proxy 0.
    • Use instanceof to determine whether the proxy class implements the Ibasetradingcenterclient (push) interface, and if so, to use ibasetradingcenterclient to force the transformation and store it to list< The ibasetradingcenterclient>.
    • Approximately 10 push classes are available. (Save to list can be omitted, but code can be read or not saved)
    • Traverse the list<ibasetradingcenterclient>, the actual inside is the proxy object.
    • You need to determine whether the original object of the proxy object is equivalent to our business class.
    • Look up a bit of information, looked at the aoputils source code, there are several methods can be used, spring AOP has encapsulated its own proxy mode.
    • Aoputils.isproxy () Determines whether the proxy class, Aoputils.gettargetclass () Gets the class of the target object (the original class) of the proxy classes, which I can use.
    • Aoputils.gettargetclass () Gets the class of the original classes, and then compares them directly with "= =" with the class of the business class, which is true for the original class.
    • Constructor parameter object and map, the incoming proxy class method $Proxy. Synch (object, map).
    • Write the file using fileutils.writestring (str, data) in the Bean2xmlutils tool class.
    • Check the file generation situation.
    • Complete.

Spring AOP Dynamic Agent Bulk Call method instance

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.