sping中AOP

The concept of agent commission:

Delegate class object is behind us when it comes to the "target audience" that is needed [to be] a proxy object target
proxy class object is behind us when it comes to the "proxy objects", the target object is the need for this object as a proxy agent

  According to create a proxy class period, the proxy class can be divided into two kinds.
        Static proxy class:
            before the program is run, the proxy class .class file has existed
        dynamic proxy class:

    JDK dynamic proxy requires that the target class implements an interface can be a proxy
       for the class does not implement the interface, you can use CGLib dynamic agent
            program is running, the agent class is the use of a reflective technology or technology dynamically created bytecode made

 

AOP concepts:

    Aspect Oriented Programming AOP

    aspect cut / slice type (the contents to be cut)

    Joinpoint connection point
            only class methods can be done in the spring connection points of aop, each method may be a point of attachment.
        
    PointCut entry point for
            a set of connection points collection

    advice notification / interceptor (weaving control point)
            for controlling the class in the end section is woven into the front of the entry point, or later when the future Throws.

    adivsor enhancer
            to a method of screening class which is our point of attachment (which methods need to be intercepted).

    target audiences

    proxy proxy object

    wave weaving

the advice (notification) Type:

        Front notification (Before advice):
            some connection points (method is a method interface) (join point) before the notification performed

        after a return notification (After returning advice):
            In some connections point (join point) advice to be executed after the normal completion (method terminates without an exception)

        throws an exception notice (after throwing advice):
            in some connection point (join point) throws notification performed when abnormal exit

        after notification ( After (finally) advice):
            when certain connection point (join point) of the exit notification performed

        around advice (Around Advice):
            notification enclosing a connection point (join point), such as transaction processing, requires such notification because the transaction need to open before the method after method to submit, and the method throws an exception when rollback

Note: in the spring, the connection point (join point) refers to the method of

 

1, aop: config tag ( ':' in front of a namespace to distinguish between labels, to avoid naming conflicts)

Aop using special tags to complete relevant configuration.
Which is mainly used in the operation expression:

expression = "execution(public * com.briup.aop.service.*.*(..))"

Quotes way is to use an expression to define the starting point, as long as this is in line with the requirements of expression, which is a collection of our starting point is to define the connection point, connection point

From left to right: first a *: Any method's return type

     Second *: denotes any class package

    * Third: any of a class method returns the type of

    (..) matches any number of parameters of the method

    (*, String) matches a method takes two arguments, the first may be of any type, the second must be of type String

    () Matches a method takes no parameters

 

2, using the AOP annotation configuration, the label can be removed in the xml configuration aop

 @Component
    the @Aspect
    public class AnnotationHandler {
        / *
         * In a comment on the above method to define the entry point
         * The entry point name is the name of this method of
         * this method itself does not need to have any effect
         significance * This method is this: to the @Pointcut can write a comment place
         * because the notes can only be written in the above methods, properties, class, and method name as an entry point name
         * * /
        @Pointcut ( "Execution (public * com.briup.aop.service * ... * (..)) ")
        public void myPointCut () {}

 

  // entry point method before calling

  @Before("myPointCut()")
        public void beforeTest(JoinPoint p){
            System.out.println(p.getSignature().getName()+" before...");
        }

  // entry point method when an exception is thrown during the execution of, calls this

  @AfterThrowing(value="myPointCut()",throwing="ex")
        public void throwingTest(JoinPoint p,Exception ex){
            System.out.println(p.getSignature().getName()+" is throwing..."+ex.getMessage());
            
        }
        
    }

 

  xml配置:
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.briup.aop"/>

NOTE: <aop: aspectj-autoproxy proxy-target-class = "true" /> is configured such that force the use of proxy CGLIB

Guess you like

Origin www.cnblogs.com/wskb/p/10939434.html
AOP