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

Introduction and usage of SpringAOP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "introduction and use of SpringAOP". In daily operation, I believe many people have doubts about the introduction and use of SpringAOP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "introduction and use of SpringAOP". Next, please follow the editor to study!

1. Introduction of AOP

AOP, aspect-oriented programming, at runtime, the programming idea of dynamically cutting the code into the specified method and location of the class is aspect-oriented programming. In fact, the code is run and wrapped, such as interception or enhancement before the method is executed, after the method returns, or after the method throws an exception.

First, let's talk about the relationship between AspectJ and Spring AOP. Many articles on the Internet have misconceptions about AspectJ.

AspectJ:

AspectJ, from the Eclipse Foundation, is an open source project hosted by Eclipse to the Apache Foundation.

It belongs to static weaving, which is achieved by modifying the code, and the timing of weaving can be:

Compile-time weaving: compile-time weaving

Post-compile weaving: that is, if the .class file has been generated, it will be compiled and woven into

Load-time weaving: refers to weaving when a class is loaded

The AspectJ framework is very powerful and is a complete solution for AOP programming. Spring AOP is committed to addressing the most common AOP requirements in enterprise development (method weaving)

AspectJ finishes weaving before the actual code runs, so people will say that the classes it generates have no additional runtime overhead

SpringAOP:

Based on dynamic agent. By default, if you use the interface, use the dynamic proxy provided by JDK, and if there is no interface, use the CGLIB implementation

There is not much relationship between Spring AOP and AspectJ, except that Spring extends the concepts in AspectJ, including using the annotations in the jar package provided by AspectJ, but does not rely on its implementation functionality.

Spring AOP needs to be managed by relying on the IOC container. It can only work on Bean in the Spring container. It is implemented in pure Java code and can only work on bean methods.

The performance of Spring AOP is slightly worse than that of AspectJ.

II. Interpretation of Spring AOP terms

Joinpoint (connection point)

A connection point is a point that can be intercepted. In spring, these points refer to methods, because spring only supports join points of method types (any method can be called a join point)

Pointcut (pointcut)

Pointcut refers to the definition of which Joinpoint we want to intercept (which method to enhance)

Advice (Notification / Enhancement)

Notification means that all you have to do after intercepting Joinpoint is notification. Notification is divided into pre-notification, post-notification, exception notification, final notification, surround notification (function to be completed in the aspect) (what function to add to it)

Target (target object)

The target object of the agent

Weaving (woven in)

Refers to the process of applying enhancement to the target object to create a new proxy object (how to get the proxy object)

Proxy (Agent)

When a class is enhanced by AOP weaving, a result proxy class is generated.

Aspect (section)

It is a combination of pointcut and notification to form an aspect, which can be configured using annotations or xml

III. Use of Spring AOP annotations

1. Configure in xml

There are other ways to open @ AspectJ annotations, which are not introduced here.

two。 Use @ Aspect annotations

Define configuration classes that implement AOP

@ Aspect annotations should be applied to bean

@ Component@Aspectpublic class LogAspect {}

3. Configure Pointcut

Used to define which methods need to be enhanced or intercepted

@ Pointcut ("execution (* com.ljj.service (..)") private void controllerAspect () {/ / TODO Auto-generated method stub} @ Pointcut ("@ annotation (com.ljj.annotation.Log)") private void controllerAspect1 () {/ / TODO Auto-generated method stub} @ Pointcut ("" within (com.ljj.service..*) ") private void controllerAspect2 () {/ / TODO Auto-generated method stub} @ Pointcut (" bean (* Service) ") private Void controllerAspect3 () {/ / TODO Auto-generated method stub}

Execution, regular matching method signature

@ annotation. The method of matching corresponding annotations

Within, which specifies the method under the class or package

Bean (idOrNameOfBean), which matches the name of bean

4. Configure Advice

@ Aspectpublic class AdviceExample {/ / the following method is to write to intercept "dao layer implementation" @ Before ("com.ljj.aop.dataAccessOperation ()") public void doAccessCheck () {/ /. The implementation code} @ Before ("execution (* com.ljj.dao.*.* (..)") public void doAccessCheck () {/ /... The implementation code} @ AfterReturning ("com.ljj.aop.dataAccessOperation ()") public void doAccessCheck () {/ /...} @ AfterReturning (pointcut= "com.ljj.aop.dataAccessOperation ()", returning= "retVal") public void doAccessCheck (Object retVal) {/ / so that when you enter the processing of this method, retVal is the return value of the corresponding method. Is it very convenient / /. The implementation code} / / returned @ AfterThrowing ("com.ljj.aop.dataAccessOperation ()") public void doRecoveryActions () {/ /... The implementation code} @ AfterThrowing (pointcut= "com.ljj.aop.dataAccessOperation ()", throwing= "ex") public void doRecoveryActions (DataAccessException ex) {/ /... The implementation code} / / understands the difference between it and @ AfterReturning, which intercepts normal returns and exceptions @ After ("com.ljj.aop.dataAccessOperation ()") public void doReleaseLock () {/ / is usually used like a finally block to release resources. / / either normal return or abnormal exit will be blocked} / / can either do @ Before or @ AfterReturning @ Around ("com.ljj.businessService ()") public Object doBasicProfiling (ProceedingJoinPoint pjp) throws Throwable {/ / start stopwatchObject retVal = pjp.proceed (); / / stop stopwatchreturn retVal;}}

The above Advice already matches the corresponding PointCut, so there is no need to define PointCut. You can also use the following PointCut + Advice

/ / define log annotations @ Target ({ElementType.PARAMETER, ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface Log {/ / operation name String optName (); / / operation type OperateType optType (); / / operation table name String optTable (); / / operation code String optCode () } @ Pointcut ("@ annotation (com.ljj.annotation.Log)") private void controllerAspect () {/ / TODO Auto-generated method stub} / / Log section @ Around ("controllerAspect ()") public Object around (ProceedingJoinPoint pjp) throws Throwable {try {/ / the operation before method execution result = point.proceed (); / / the operation after method execution} catch (Exception e) {e.printStackTrace () }} at this point, the study on "introduction and use of SpringAOP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report