AOP Implementation Method

Source: Internet
Author: User

Annotation Method

1. Define the Aspect using Aspect

2. Define PointCut and Advice in Aspect

3. Enable AspectJ's support for Annotation and configure the Aspect class and target object to the IOC container.

 

Define a class, indicating that @ Aspect defines a method labeled as Pointcut ("execution (* add * (..)") to define the sequence of method execution before ("pointcutname ").

The final generation is a proxy.

 

AOP:

Cross-cutting concerns: Cross cutting concern

Aspect: Aspect

Specific Method to be implanted: Advice

Entry Point: Pointcut

Joinpoint

Implant: Weave

Target Object: target Object

Proxy: Proxy

Dynamic Method: Introduction

 

 

Configuration file method:

<Aop: config>

<Aop: aspect id = "security" ref = "securityHandler">

<Aop: pointcut id = "alladdmethod" expression = "execution (* cn. synjones. dreams. usermanagerimpl. add * (...)"/>

<Aop: before method = "checkSecurity"/>

 

</Aop: aspect>

</Aop: config>

By default, Aspect does not need to implement interfaces. However, for the target object, interfaces must be implemented by default. If the interface is not implemented, the CGLIB library must be introduced.

If the target object implements the interface, the dynamic proxy of JDK is used by default to implement AOP. You can also use the CGLIB library to implement AOP.

If the target object does not implement an interface, the CGLIB library must be used. Spring will automatically convert between the JDK dynamic proxy and CGLIB.

 

Differences between JDK dynamic proxy and CGLIB bytecode generation:

1. JDK dynamic proxy can only generate a proxy for the class that implements the interface, rather than the class.

2. CGLIB is the proxy for class implementation. It mainly generates a subclass for the specified class and overwrites the method. Because it is integrated, it is best not to declare the method as final.

 

How to force the use of CGLIB to implement AOP?

1. Introduce the CGLIB library,/cglib/*. jar

<Aop: aspectj-autoproxy proxy-target-class = "true"/>

 

Joinpoint

You can use the joinpoint parameter in Advice to obtain the parameter of the execution method: joinpint. getArgs ();

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.