Understanding of Spring Framework AOP (aspect-oriented)

Source: Internet
Author: User
Tags aop

AOP: Aspect-oriented to take the horizontal extraction mechanism, replacing the traditional vertical inheritance system repetitive code (performance monitoring, transaction management, security check, caching)

AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

Several core concepts of AOP ":

1, crosscutting concerns

What methods to intercept and how to deal with them, these concerns are called crosscutting concerns.

2, Slice

A class is an abstraction of an object's characteristics, and a facet is an abstraction of a crosscutting concern.

3, Connection point (joinpoint)

The point that is intercepted, because spring only supports connection points of the method type, so the connection point in spring refers to the method being intercepted, in fact the connection point can also be a field or a constructor

All methods in a service implementation class are likely to become connection points

4. Entry point (pointcut)

Definition of interception of connection points

The so-called entry point is the definition of which joinpoint we want to intercept.

5. Notice (advice)

The so-called notice refers to the interception to Joinpoint after the thing to do is to inform. Notification is divided into pre-notification, post-notification, exception notification, final notification, surround notification (slice to complete the function)

6. Target Object

Target object for the agent

7. Weave in (weave)

The process of applying a slice to the target object and causing the proxy object to be created

8. Introduction (Introduction)

Without modifying the code, the introduction can dynamically add some methods or fields to the class at run time

Configuration of Spring Framework AOP

1, Create the Javaweb project, introduce the specific development of the jar package
* Introduce the basic development package developed by Spring Framework first
* Re-introduction of the Spring Framework AOP development Package
* Spring's traditional AOP development package
* Spring-aop-4.2.4.release.jar
* Com.springsource.org.aopalliance-1.0.0.jar

* ASPECTJ's Development Kit
* Com.springsource.org.aspectj.weaver-1.6.8.release.jar
* Spring-aspects-4.2.4.release.jar

Specific package versions are introduced according to actual requirements

2. Create a spring configuration file that introduces the specific AOP schema constraint '

<beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:mvc= "Http://www.springframework.org/schema/mvc"xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.2.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd           HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  ">

3, Create the package structure, write the specific interface and implementation class

is the service interface and implementation class in the project

4, the target class is configured in spring (the corresponding interface implementation class is injected into spring) This step can be omitted in the SSM framework

  <bean id= "Customerdao" class= "Com.itheima.demo3.CustomerDaoImpl"/>

Target class: An implementation class for an interface

5, defining the Slice class

6, defining the slice class in the configuration file

<bean id= "Logaspect" class= "Com.meyki.common.log.LogAspect"/>

7. complete the configuration of AOP in the configuration file

<!--define interface mode, proxy-specified class-    <aop:config>               // introduce slice class        <aop:aspect ref= " Logaspect ">                 // define notification type, Pointcut determination where @annotation in Applicationcontext.xml configuration folder
                  <aop:around method= "Doaround" pointcut= "@annotation (com.meyki.common.log.ActionLog)"/>
</aop:aspect>
</aop:config>

When you configure the pointcut, you need to define the expression, with the emphasis in the following format: Execution (Public * * (..)), specifically:
* The format of the pointcut expression is as follows:
* Execution ([modifier] Returns the value type package name. Class Name. Method name (parameter))

<aop:config>        <aop:pointcut id= "Pointcutmethod" expression= "Execution (* com.*.*.*.service.*.* (..))" />        <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Pointcutmethod"/>    </aop:config>

turn on automatic Proxy in the configuration file

<aop:aspectj-autoproxy/>

Understanding of Spring Framework AOP (aspect-oriented)

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.