Imitate the function of SPRING-AOP and use annotations to build your own framework.

Source: Internet
Author: User
Tags reflection

Into the Java pit more than July, but also try to build their own framework, recently on the spring AOP this aspect is fascinated, to record their current small demo, and the subsequent time will continue to improve their demo. Hope the great God to enlighten.

The main use of reflection and Java comes with the proxy class. The theoretical knowledge will not be said, because I am not very clear at the moment, to avoid misleading, or to dismiss it. All right, just follow the code.

Structure:

  

  


Person.java
Public interface Person {    void Say ();}

Interface Implementation Class

Man.java
Public class Mans implements person {    @Override public    Void Say () {        System.out.println ("Men say: ....");    }}

Custom annotations

@interface WATERAOP
@Retention (Retentionpolicy.runtime) @Target ({elementtype.parameter, elementtype.method}) @Documentedpublic @ Interface Wateraop {    enum Method{before,after,afterthrowing}    Method Method () default method.after;    String name () default "class full name";}

Custom annotation Classes

Waterlog.java
public class Waterlog {    @WaterAOP (Name = ' Com.water.aop.attempt3.Man ', method = WaterAOP.METHOD.after)    public void Afteraction () {        System.out.println ("post-Behavior");    @WaterAOP (Name = "Com.water.aop.attempt3.Man", method = WaterAOP.METHOD.before) public    void Beforeaction () {        System.out.println ("predecessor Behavior");}    }

Implement a custom proxy class (which is

The third parameter of the Proxy.newproxyinstance () method is rigged. A java8 lambda expression is used.

Proxyfactory.java

public class Proxyfactory {//maintains a proxied object that implements an interface, followed by a group of objects, and the private person is a person;    Private Waterlog Waterlog;    Private Method Beforemethod=null,aftermethod=null;        Public proxyfactory (Person Person,waterlog waterlog) {This.person=person;    This.waterlog=waterlog; Public Object getproxyinstance () {return proxy.newproxyinstance (Person.getclass (). getclassload                ER (), Person.getclass (). Getinterfaces (),//The first parameter is the agent, if you want to do some action on the agent can use this parameter;                The second is the method to be executed,//The third is the parameter required to execute the method. (Object proxyobj, Method method,object[] args)->{//If no incoming AOP directly returns empty if (waterlog==                    NULL) {return null;                    } Class Aop=waterlog.getclass ();                    Class C = Person.getclass ();           Gets the annotations of the methods of the AOP class and assigns them to some custom variables, depending on whether the variables have values to determine whether there are annotations getannotation (AOP,C);         if (beforemethod!=null) {beforemethod.invoke (waterlog);                    }//proxy object execution method and get return value of Object Returnvalue=method.invoke (Person,args);                    if (aftermethod!=null) {aftermethod.invoke (waterlog);                } return returnvalue;    }        ); } private void Getannotation (class Aop,class proxy) {//If there is an AOP class if (waterlog!=null) {//gets all of the slice classes            Method method[] Methodsaop=aop.getmethods (); If the method of the cut-in log class is not NULL if (Methodsaop!=null) {for (Method LOGMETHOD:METHODSAOP) {//                    The method of obtaining Waterlog class WATERAOP annotation wateraop wateraop=logmethod.getannotation (wateraop.class); if (wateraop!=null) {//If the annotations on AOP are consistent with the passed-in class name if (Proxy.tostring (). SUBSTRING (6                        ). Equals (Wateraop.name ())) {    if (wateraop.method () = = WaterAOP.METHOD.before) {//assignment, followed by                            Beforemethod=logmethod;                            }else if (wateraop.method () = = WaterAOP.METHOD.after) {Aftermethod=logmethod; }                        }                    }                }            }        }    }}

Zhuanzi https://www.cnblogs.com/water-zmh/p/8427877.html

Test class

Test.java (JUnit is a test package or can be used directly with the Main method)

public class Test {    @org. junit.test public    void Wateraop () {person        person=new man ();        Person proxyperson= (person) New Proxyfactory (Person,new Waterlog ()). Getproxyinstance ();        Proxyperson.say ();    }}

The approximate process is to pass in the class to be proxied and the custom annotation class, use reflection to get the value of the annotation attribute on the method in the annotation class, then make the comparison, then carry on the corresponding operation.

Imitate the function of SPRING-AOP and use annotations to build your own framework.

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.