Preliminary study on AOP

Hello, I'm a java student of Shenzhen Branch of it Academy. I'm an honest, pure and kind java programmer.

Today, I'd like to share with you some knowledge points in deep thinking - spring AOP?

1. Background introduction

1. What is AOP

1) In OOP (object-oriented programming), it is the existence of code (crosscutting code) scattered everywhere and independent of the core function of the object that makes it more difficult to reuse the module. 2) AOP cuts open the encapsulated object, finds out the public behavior that affects multiple objects, and encapsulates it as a reusable module, This module is named "aspect" (aspect 3). The aspect extracts and encapsulates the logic that has nothing to do with the business but is jointly called by the business modules, reduces the repeated code in the system, reduces the coupling degree between modules, improves the maintainability of the system, improves the development efficiency and program reusability.

2. The difference between AOP and OOP

1) AOP can be called OOP (object-oriented programming). OOP introduces the concepts of encapsulation, inheritance, polymorphism, etc. to establish an object hierarchy, which is used to simulate a collection of public behavior. 2) however, OOP allows developers to define vertical relationships, but it is not suitable for defining horizontal relationships. 3) for example, logging functions. Logging code is often distributed horizontally in all object hierarchies, and is not related to it The core function of the corresponding object has nothing to do with it. This is also true for other types of code, such as security, exception handling and transparent persistence. This scattered irrelevant code is called cross cutting. 4) in OOP design, it leads to a large number of code duplication, which is not conducive to the reuse of various modules.

3. The idea of AOP

1) The core idea of AOP is to help us extend functions (Methods) without modifying the source code. 2) it adopts horizontal extraction mechanism to avoid the repetitive and highly coupled code of vertical inheritance system in traditional object-oriented.

2. Knowledge analysis

1. Why use AOP? 1) for convenience, using AOP can make you write less code. 2) for clearer logic, your business logic can focus on your own business without thinking about other things, including security, things, daily records, etc. 3) In order to reduce the high coupling caused by the vertical inheritance system enhancement method in the traditional object-oriented: 4) improve the reusability of the code

2. On the development and principle of AOP thought

Development: modify the source code - adopt OOP vertical inheritance system - adopt AOP horizontal weaving implementation principle: create proxy objects through dynamic proxy to realize it. By default, Java dynamic proxy is used to create AOP proxy. When the class to be proxy is not the proxy interface, spring will switch to cglib proxy

3. AOP terminology

1. Advice: the advice defines what the aspect is and when to use it; inside it is the logic of the enhancement method, which is divided into pre enhancement, post enhancement, surround enhancement, final enhancement and exception enhancement. 2. Connection points (joinpoint): a connection point is a point that can be inserted into the aspect during the execution of an application. This point can be when calling a method, throwing an exception, or even modifying a field. The aspect code can use these points to insert into the normal process of the application and add new behavior. 3. Pointcut (pointcut) *: pointcuts help narrow the range of connection points notified by the slice. (where)

4. Aspect) *: aspect is the combination of notification and pointcut. Notification and pointcut define the whole content of aspect - what it is, when and where to complete its function. 5. Goal (target): the class where the enhanced method is located, that is, the real business logic, which can be woven into the aspect without knowing it. Instead, it focuses on the logic of the business itself. 6. Proxy: the proxy class generated after the target class is enhanced

7. Weaving: the process of applying enhancements to the target class 8: introduction allows us to add new methods or properties to existing classes, so that they have new behavior and state without modifying these existing classes

4. Introduction to notification type

(1) Before: do enhancement processing before the target method is called, @ before only needs to specify the pointcut expression. (2) afterreturning: do enhancement after the normal completion of the target method, @ afterreturning can also specify a return value parameter name returning after specifying the pointcut expression, Represents the return value of the target method (3) afterthrowing: it is mainly used to handle unhandled exceptions in the program. In addition to specifying the pointcut expression, @ afterthrowing can also specify a parameter name of the return value of throwing, which can be used to access the exception object thrown in the target method

(4) After: enhance after the target method is completed, no matter when the target method is successfully completed@ After can specify a pointcut expression (5) around: surround notification, which is enhanced before and after the completion of the target method. Surround notification is the most important notification type, such as transaction and log. Note that the core of programming is a processingjoinpoint

3. Frequently asked questions

1. What are the ways to implement AOP? 2. What are the ways to implement enhancements? 3. After determining the methods to be enhanced, how to locate the pointcut? 4. Use @ aspect to cut into SSM and count the running time.

4. Solutions

1) : what are the ways to implement AOP? Perform AOP operations in sring, AspectJ (AspectJ is a kind of extended Java) is used to implement. AspectJ itself is a separate aspect oriented framework, which defines AOP syntax, not a part of spring. It cooperates with spring to complete the operation of springaop. There are two ways to use AspectJ to implement the operation of springaop: 1. Inherited class (not recommended) 2. XML file based on AspectJ 3. Annotation based on AspectJ

2) : what are the ways to implement enhancements? Weaving in

3) : after determining the method to be enhanced, how to locate the pointcut can be solved by using the execution expression (commonly used) or XML configuration and regexpmethodpointcutadvisor to define the pointcut. 1. Execution (* com. AOP. Add (..)) 2. Match all methods starting with save: execution (* save * (..)

4) : use @ aspect to cut into SSM and count the running time. Talk about it during coding practice.

5. Coding practice

Configuration based on XML configuration file

You need to create AOP objects, set pointcuts, enhance classes, methods, and so on.

Annotation based profile

UserService.java
Operator.java
Test.java

Operation results

Extended thinking

1. Advantages and disadvantages of annotation based and configuration file based Advantages: annotations can reduce the work of configuration, and the combination of configuration information and Java code helps to enhance the cohesion of the program. Compared with XML configuration file, switching between configuration file and program file with annotation can improve the consistency of development and commission development efficiency. Disadvantages: if the program is complex, the XML configuration file is still used, which has strong readability and maintainability.

7. References

8. More discussion

1. What are the advantages and disadvantages of annotation based versus profile based? Advantages: annotations can reduce the work of configuration, and the combination of configuration information and Java code helps to enhance the cohesion of the program. Compared with XML configuration file, switching between configuration file and program file with annotation can improve the consistency of development and commission development efficiency. Disadvantages: if the program is complex, the XML configuration file is still used, which has strong readability and maintainability.

2. What are the ways to implement AOP? Perform AOP operations in sring, AspectJ (AspectJ is a kind of extended Java) is used to implement. AspectJ itself is a separate aspect oriented framework, which defines AOP syntax, not a part of spring. It cooperates with spring to complete the operation of springaop. There are two ways to use AspectJ to implement the operation of springaop: 1. Inherited class (not recommended) 2. XML file based on AspectJ 3. Annotation based on AspectJ

3. What are the ways to implement enhancements? Weaving in

Two ways

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It academy "we believe that everyone can become an engineer. From now on, find a senior brother to get you started, control your learning rhythm, and stop being confused on the way to learning".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me ~ my invitation code:, or you can click this link directly:

Video address: nofollow "> https://v.qq.com/x/page/f07723fu6mn.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>