“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

πŸ‘¨πŸŽ“ Author: Bug Bacteria

✏️ blog: CSDN, Nuggets, etc

πŸ’Œ public account: Magic House of the Circle of the Apes

🚫 special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

πŸ™ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles πŸ‘. Your likes (collect ⭐️+ pay attention to πŸ‘¨ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon πŸƒπŸ»β™€οΈ, creation stopped πŸ’•, refueling 🏻

One, foreword

In the Spring architecture, there are many ways to implement custom annotations. For example, you can write a public method and call it manually in each Controller.

What I’m going to talk about today is how to implement user-defined annotations using AOP aspect thinking. Do you want to hear it? Are you interested?

As a matter of fact, it doesn’t matter whether I am interested or not. If I write, there will be people who are willing to read it. After all, I am confident in what I write. I want to teach people who haven’t implemented custom annotations before spring AOP. To help them is to help myself. One for all, all for one! As for the boss level, you are also welcome to criticize the article at any time.

Ok, let’s get started.

Second, the body

1. What is AOP?

I’m sure you all know that. Aspect Oriented Programming (AOP) is a technology that realizes unified maintenance of program functions through precompilation and runtime dynamic proxy. Can be said to be OOP (Object-Oriented Programing, Object-Oriented programming) complement and perfect.

2. Application scenarios of AOP

  • Log printing and recording

  • Authority certification

  • Global exception handling interception

  • Return values are processed uniformly

  • Multi-data source Switching

  • Transaction processing

  • . .

In summary, understand that Aop’s role is to separate the various concerns in your system, separating core concerns from crosscutting concerns. As long as it does, then AOP is applicable.

3. How to use AOP?

In principle, we can use Spring’s AOP support directly by introducing the corresponding AOP dependencies in the Spring framework. However, to further facilitate the use of SpringAOP, SpringBoot still “takes the trouble” to provide us with a spring-boot-starter-AOP automatic configuration module.

Spring-boot-starter – AOP automatic configuration behavior consists of two parts:

  • spring-boot-autoconfigureUnder theorg.springframework.boot.autoconfigure.aop.AopAutoConfigurationprovide@ConfigurationConfiguration classes and corresponding configuration items.
  • spring-boot-starter-aopThe module itself provides a target forspring-aop,aspectjrt ε’Œ aspectjweaverRely on.

In general, whenever spring-boot-starter-aop is added to a project dependency, aop’s associative behavior is automatically triggered, including building the corresponding AutoProxyCreator, weaving crosscutting concerns into the corresponding target object, and so on.

However, AopAutoConfiguration still gives us a poor pair of configuration items for limited intervention with aOP-related configurations: spring.aop.auto=true; Spring.aop.proxy-target-class=false Allow us to vote against it, for example by choosing to turn off automatic AOP configuration spring.aop.auto=false, or by enabling an AOP proxy for class rather than interface level.

4. How to implement AOP?

The technologies for implementing AOP fall into two main categories:

  • First, dynamic proxy technology is used to decorate the message by intercepting the message to replace the execution of the original object behavior.
  • The other is static weaving, which introduces special syntax to create “aspects” so that the compiler can weave code about “aspects” at compile time.

However, the technical characteristics of implementing AOP are the same, respectively:

  • 1.JoinpointJoin point: an exact point of execution in a program, such as a method in a class. It is an abstract concept, and there is no need to define a Join point when implementing AOP.
  • 2,PointcutPointcuts: Essentially a structure that captures join points. In AOP, you can define a point cut to capture calls to related methods.
  • 3,Advice(Notification) : is the execution code of point cut, which is the concrete logic to execute the “aspect”.

There are five types of advice:

  1. Pre-notification (Before) : Call notification before the target method is called.
  2. Post notification (After) : The notification is called after the target method has completed, regardless of what the output of the method is.
  3. Return notification (AfterReturning) : Calls notifications after successful execution of the target method.
  4. Exception notification (AfterThrowing) : Calls the notification after the target method throws an exception.
  5. Circular notification (Around) : Notifications wrap notified methods, performing custom behavior before and after a notified method invocation.
  • 4. Aspect: Combined with advice, Point cut is an Aspect, which is similar to a class defined in OOP, but represents more of a horizontal relationship between objects.
  • 5. Introduce: Introduce additional methods or attributes to an object, thus modifying its structure. Some AOP tools refer to them as mixins.

The technical features described above make up the basic AOP techniques that are implemented by most AOP tools. They can also be basic terms for studying AOP technologies.

. .

So, we’ll show you how to configure it in a project.

Three, configuration,

We can use AOP simply by adding spring-boot-starter-AOP to our project dependencies.

1. Pom dependency

<! - AOP -- > < the dependency > < groupId > org. Springframework. Boot < / groupId > < artifactId > spring - the boot - starter - AOP < / artifactId > </dependency>Copy the code

After adding the aop starter, etc dependencies to the POM as described above, and downloading them from the Maven source repository, we are ready to use AOP normally.

2, the application – dev. Yaml

In addition to referring to aop dependencies, we may also involve a configuration. As I mentioned above, allowing us to manually turn off automatic AOP configuration allows us to simply add the following auto and proxy-target-class configuration items to our project configuration files, which we don’t normally need, but we need to be aware of.

Spring: aop: auto: true False: disable proxy-target-class: false # The value of the proxy-target-class attribute determines whether an interface-based or class-based proxy is created. True indicates that class-based proxies will be used,false indicates that Jdk interface-based proxies are used by defaultCopy the code

In the next issue, we will analyze two cases, from simple to profound, with you how to use it in specific projects, with you to experience the unique charm of AOP in the process of use, its crosscutting is so chic, please look forward to it.

. .

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Four, the past popular recommendation

  • Springboot series (15) : AOP implements custom annotations for business logging! Have you ever played?

  • Springboot series (14) : Redis Zero-based teaching, you deserve it!

  • Springboot Series (thirteen) : How to project integrated Swagger online interface documentation, will you?

  • Springboot series (12) : How to code to send email reminders, have you written?

  • . .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

☘️ Be who you want to be, there is no time limit, you can start whenever you want,

πŸ€ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

​

πŸ’“ If this article is helpful to you, please leave a like! (# ^. ^ #);

πŸ’ if you like the article shared by bug fungus, please give bug fungus a point of concern! (ΰΉ‘ ‘α΄— ‡ ΰΉ‘);

πŸ’— if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

πŸ’ž In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

πŸ’• copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you