Cainiao has a basic understanding of spring aop. And applications, springaop

Source: Internet
Author: User

Cainiao has a basic understanding of spring aop. And applications, springaop

The first time I wrote a blog. I am a cainiao. Correct the error. I posted this blog to organize my learning process. And some ideas.

Let's start with the text below: (these things are taken from many blogs and summarized by myself. Please advise if there is anything wrong .)

Oop is a vertical programming from top to bottom, and aop is a horizontal cutting.

OOP allows you to define the relationship from top to bottom, but it is not suitable for defining the relationship from left to right. For example, log function. The Log Code is horizontally distributed across all object layers, but it has nothing to do with the core functions of the objects it spreads, resulting in a lot of code duplication.
Aop anatomy of the encapsulated object, and encapsulate the public behaviors that affect multiple classes into a reusable module, and name it "Aspect", that is, "Aspect '. It can reduce the coupling between modules and facilitate the modification and maintenance of future functions.

All in all: it is convenient to extract public code (logs, permissions) and put them in one segment.

The technology for Implementing AOP is mainly divided into two categories: one is dynamic proxy technology jdk or cglib, and the other is static weaving, enable the compiler to weave "aspect" code during compilation. I usually use dynamic proxies in projects.


Configuration File-form aop can be used for log and transaction control

<! -- System Service component validation Bean -->
<Bean id = "serviceAspect" class = "cn. ysh. studio. spring. aop. aspect. ServiceAspect"/>
<! -- AOP configuration -->
<Aop: config>
<! -- Declare a plane and inject the Bean of the plane, which is equivalent to @ Aspect -->
<Aop: aspect id = "simpleAspect" ref = "serviceAspect">
<! -- Configure an entry point, which is equivalent to @ Pointcut -->
<Aop: pointcut expression = "execution (* cn. ysh. studio. spring. aop. service... * (..)" id = "simplePointcut"/>
<! -- Configure the notification, which is equivalent to @ Before, @ After, @ AfterReturn, @ Around, @ AfterThrowing -->
<Aop: before pointcut-ref = "simplePointcut" method = "before"/>
<Aop: after pointcut-ref = "simplePointcut" method = "after"/>
<Aop: after-returning pointcut-ref = "simplePointcut" method = "afterReturn"/>
<Aop: after-throwing pointcut-ref = "simplePointcut" method = "afterThrow" throwing = "ex"/>
</Aop: aspect>
</Aop: config>

 

Annotation-based aop
Declare in the configuration file to activate the automatic scanning component function, and activate the automatic proxy Function

<! -- Activate the component scanning function. The component configured by annotation is automatically scanned under the cn. ysh. studio. spring. aop package and its sub-package -->
<Context: component-scan base-package = "cn. ysh. studio. spring. aop"/>
<! -- Activate the automatic proxy function -->
<Aop: aspectj-autoproxy proxy-target-class = "true"/>


// Declare that this is a component
@ Component

// Declare that this is a validation Bean
@ Aspect

// Configure the entry point. This method does not have a method body and is mainly used to facilitate other methods of the same type.
@ Pointcut ("execution (* cn. ysh. studio. spring. aop. service ..*(..))")
Public void aspect (){}


/*
* Configure the pre-notification and use the entry point registered on method aspect ().
* The JoinPoint entry point object is accepted at the same time. This parameter does not exist.
*/
@ Before ("aspect ()")


// Configure post-notification to use the entry point registered on method aspect ()
@ After ("aspect ()")


// Configure the surround notification to use the entry point registered on method aspect ()
@ Around ("aspect ()")


// Configure the post-return notification to use the entry point registered on method aspect ()
@ AfterReturning ("aspect ()")

// Notification after the configuration throws an exception, using the entry point registered on method aspect ()
@ AfterThrowing (pointcut = "aspect ()", throwing = "ex ")

 

 

  

 

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.