Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic concept of Spring AOP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge about "what is the basic concept of Spring AOP". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

What is AOP?

AOP stands for aspect-oriented programming.

Aspects Oriented-Spring provides rich support for aspect-oriented programming, allowing cohesive development by separating the business logic of an application from system-level services such as auditing and transaction management. Application objects only do what they are supposed to do--complete business logic--and that's it. They are not responsible for (or even conscious of) other system-level concerns, such as logging or transaction support.

Basic concepts in AOP

Notification (Adivce)

There are 5 types of notifications:

Before: Called before the method is called

After: Call notification after method completion, regardless of method execution success

After-returning: Call notification after successful method execution

After-throwing: Call notification after method throws exception

Around: notification is good, contains the notified method, and performs custom behavior before and after the notified method is called

Pointcut

Tangents in Spring AOP are methods in the corresponding system. But this method is defined in the facet and is generally used with notifications, which together make up the facet.

Join point

For example: method invocation, method execution, field setting/obtaining, exception handling execution, class initialization, or even a point in the for loop

Theoretically, any point in the execution of a program can be used as an entry point, and all of these execution points are Joint points.

But Spring AOP currently only supports method execution, which can also be understood as a join point where you are ready to execute pointcuts and pointcut notifications in the system (usually a method, a field).

Aspect

An aspect is a collection of pointcuts and notifications, generally treated separately as a class. Notifications and pointcuts together define everything about an aspect, when it is, when and where it is functional.

Introduction

References allow us to add new methods or attributes to existing classes

Weaving

Assemble aspects to create a notified object. This can be done at compile time (for example, using the AspectJ compiler) or at runtime. Spring, like other pure Java AOP frameworks, completes weaving at runtime.

Support for AOP in Spring

First of all, the implementation of AOP ideas is generally based on proxy mode, JDK dynamic proxy mode is generally used in JAVA, but we all know that JDK dynamic proxy mode can only proxy interfaces, if you want to proxy classes, then it will not work. Therefore, Spring AOP switches like this, because Spring AOP supports CGLIB, ASPECTJ, JDK dynamic proxies, and Spring AOP defaults to JDK dynamic proxies when your real objects have implementation interfaces, otherwise it uses cglib proxies.

If the target object's implementation class implements an interface, Spring AOP uses JDK dynamic proxies to generate AOP proxy classes;

If the target object's implementation class does not have an implementation interface, Spring AOP will use CGLIB to generate the AOP proxy class--but this selection process is completely transparent to, and not of concern to, the developer.

code example

Define topic interfaces whose methods can be our connection points

package wokao666.club.aop.spring01;

public interface Subject {

//Login

public void login();

//Download

public void download();

}

Define the implementation class, which is the real proxied in proxy mode (if you are involved in purchasing, this is like your role in purchasing)

package wokao666.club.aop.spring01;

public class SubjectImpl implements Subject {

public void login() {

System.err.println("... ");

}

public void download() {

System.err.println("Download... ");

}

}

Define slices (with tangents and notifications in slices)

package wokao666.club.aop.spring01;

public class PermissionVerification {

/**

* permission check

* @param args Landing parameters

*/

public void canLogin() {

//do some login verification

System.err.println("I'm checking!!! ");

}

/** Wuxi Gynecological Examination Hospital http://www.87554006.com/

* Do some processing after verification (do processing regardless of success)

* @param args Permission Verification Parameters

*/

public void saveMessage() {

//do some post-processing

System.err.println("I'm working on it!!! ");

}

}

SpringAOP.xml file

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

Test Classes and Results

package wokao666.club.aop.spring01;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

ApplicationContext ctx =

new ClassPathXmlApplicationContext("SpringAOP.xml");

Subject subject1 = (Subject)ctx.getBean("SubjectImpl1");

Subject subject2 = (Subject)ctx.getBean("SubjectImpl2");

subject1.login();

subject1.download();

System.err.println("==================");

subject1.login();

subject1.download();

}

}

"Spring AOP basic concept is what" the content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report