Container +AOP for dynamic Deployment (iv)

Source: Internet
Author: User

in the previous chapter we introduced the containers and AOP The combination of how the object enhancement service is not too much explained, this will specify how the object enhanced to achieve a one -to-many and many-to-many enhancements

Let's start with the simple way.

/***JDK proxy class, implement dynamic Call object method */public class Jdkdynamicproxy implements Invocationhandler {  /*** ... Omit method *//*** callback usage, execute selection method *       /@Override Public     object Invoke (Object proxy, Method method, object[] args) throws Throwable {          before ();          Object result = Method.invoke (target, args);          After ();          return result;      }       private void Before () {              System.out.println ("before");      }       private void After () {          System.out.println (' after ');      }  }

The above code is converted to graphic

We fixed the specific particles in AOP , which is not a good solution if you want to add service particles to change the code. In order to better decouple the service from AOP , we are going to load the service into a service container. This will have the previous version .

basically what we need, by intercepting the business particles, passing the service particles, the relationship set together to AOP in which AOP to parse. If there are multiple service particles, then we have a modified version.



The service particles on the right are placed in a container, and multiple service particles serve a business object at the same time. If you have multiple service particles, and want this write service to support all the business particles, it becomes

1 The required parameters are passed in first through the constructor function.

Private map<string, object> Aspectbeans; Service container Private map<string, object> businessbeans;//business container private map<string, object> relationbeans;//relationship Container/* * * *  @param target *            is proxy object * @param aspectbeans *            cut container * @param businessbeans *            Business container * @param relationbea NS *            Relationship set */public jdkdynamicproxy (Object target, map<string, object> aspectbeans,map<string, object> Businessbeans, map<string, object> relationbeans) {this.target = Target;this.aspectbeans = AspectBeans; This.businessbeans = Businessbeans;this.relationbeans = Relationbeans;}


2 in the callback function Call Parse relational XML method, make method call

Callback register cut-in method @overridepublic object Invoke (Object proxy, method, object[] args) throws Throwable {List beforelist = (List) Relationbeans.get ("Aspectbefore");//Gets the relationship invokeaspectname (Beforelist, method, args) in the relationship container;// Call the match method in the Slice class object result = Method.invoke (target, args);//Call the proxy class itself method return result; /** * * @Title: Getallmethod * @Description: Executes all methods in a service class, * @param @param clazz Service class * @param set in @param aspectclassaop relationship set The method of execution of the interception * @param @param parameters of the args intercepted object * @return void return type * @throws */public void Getallmethod (Class clazz, String aspec Tclass, object[] args) throws Illegalaccessexception, Illegalargumentexception,invocationtargetexception,  Nosuchmethodexception, SecurityException {//Gets all public methods in the service class method[] methods = Clazz.getdeclaredmethods (); for (int j = 0; j < Methods.length; J + +) {//Reflection gets each method name in the service class, gets the service class method Jinectmethod = Clazz.getmethod (Methods[j].getname (), object.class);// Reflection calls a method in the service class Jinectmethod.invoke (Aspectbeans.get (aspectclass), args = = null? New Object[1]: args);}} 

 Summary:

The above is the basic interpretation of the relationship between the AOP , step by step gradually evolved, is not an overnight, so back to study, is not a study can be complete, to continue to think and summarize repeatedly. Specific source click to connect

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Container +AOP for dynamic Deployment (iv)

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.