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

How to understand Java Spring AOP

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to understand Java Spring AOP, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Preface

For aspect-oriented programming, each part of the business logic can be isolated by using AOP, which reduces the coupling between the various parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

That is, do not change the source code and add new functions, pluggable.

Tip: the following is the main body of this article. The following examples are available for reference.

I. the underlying principle of 1.AOP uses dynamic agents at the bottom.

There is an interface: jdk dynamic proxy, that is, create an interface to implement a class proxy object

No interface: CGLIB dynamic proxy, that is, creating subclass proxy objects

Implementation of jdk dynamic Agent

Create an interface

Package com.vector.spring5;public interface UserDao {public int add (int aline int b); public String update (String id);}

Interface implementation class

Interface to achieve the method of the class, belongs to the source code, with aop ideas to add new functions here can not be moved!

Package com.vector.spring5;public class UserDaoImpl implements UserDao {@ Override public int add (int a, int b) {return astatb;} @ Override public String update (String id) {return id;}}

Add new features by using JDK dynamic proxy object

Package com.vector.spring5;import java.lang.reflect.Array;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Arrays;public class JDKProxy {public static void main (String [] args) {/ / create the interface to implement the class proxy object Class [] interfaces = {UserDao.class}; UserDaoImpl userDao = new UserDaoImpl () UserDao dao= (UserDao) Proxy.newProxyInstance (JDKProxy.class.getClassLoader (), interfaces,new UserDaoProxy (userDao)); int result = dao.add (1Magne2); System.out.println ("result:" + result);}} / / create proxy object class UserDaoProxy implements InvocationHandler {/ / construct enhanced object private Object obj; public UserDaoProxy () {}; public UserDaoProxy (Object obj) {this.obj=obj } / / enhanced logic @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ / System.out.println before method ("execute before method:" + method.getName () + ": parameter passed:" + Arrays.toString (args)) / / enhanced method execution / / enhanced Object res = method.invoke (obj,args) can be selected according to method.getName () judgment; / / System.out.println after method ("execute after method:" + obj); return res;}}

Image Analysis of jdk Agent

II. AOP term 1. Connection point

The methods that can be enhanced in a class are called join points.

two。 Entry point

The method that is actually enhanced in the class becomes the pointcut.

3. Notification (enhanced)

(1) the logical part of the actual enhanced method is called notification (enhancement).

(2) Notification includes pre-notification, post-notification, surround notification, exception notification and final notification.

4. Cut plane

The process of applying enhancement to a pointcut is called a tangent

III. AOP operations (preparation) Spring frameworks generally implement AOP operations based on AspectJ

(1) AspectJ is not a part of Spring. It is an independent AOP framework. Generally, AspectJ and Spirng framework are used together to perform AOP operations.

Maven preparation

Org.aspectj aspectjweaver 1.9.8.RC1 mode 1: use the interface of Spring to achieve additional functions

Realize the combination of combination crud and logging function

ApplicationContext.xml

Log.java

Package com.vector.log;import org.springframework.aop.MethodBeforeAdvice;import org.springframework.stereotype.Component;import java.lang.reflect.Method @ Component ("log") public class Log implements MethodBeforeAdvice {/ / method: method of the target object to be executed / / args: parameter / / target: target object @ Override public void before (Method method, Object [] args, Object target) throws Throwable {System.out.println (target.getClass (). GetName () + "+ method.getName () + executed");}}

UserService.java

Package com.vector.service;public interface UserService {public void add (); public void delete (); public void update (); public void query ();}

UserServiceImpl.java

Package com.vector.service;import org.springframework.stereotype.Service;@Service ("userService") public class UserServiceImpl implements UserService {@ Override public void add () {System.out.println ("add a user");}}

MyTest.java

Public class MyTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); / / dynamic proxy is interface UserService userService = (UserService) context.getBean ("userService"); userService.add ();}}

Method 2: custom class

DiyPoint.java

Package com.vector.diy;import org.springframework.stereotype.Component;@Component ("diyPointCut") public class DiyPointCut {public void before () {System.out.println ("= before method execution = =");} public void after () {System.out.println ("= after method execution = =");}}

UserServiceImpl.java

Package com.vector.service;import org.springframework.stereotype.Service;@Service ("userService") public class UserServiceImpl implements UserService {@ Override public void add () {System.out.println ("add a user");}}

ApplicationContext.xml

MyTest.java

Public class MyTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); / / dynamic proxy is interface UserService userService = (UserService) context.getBean ("userService"); userService.add ();}}

Method 3: fully annotated configuration implementation

UserServiceImpl.java

Package com.vector.service;import org.springframework.stereotype.Service;@Service ("userService") public class UserServiceImpl implements UserService {@ Override public void add () {System.out.println ("add a user");}}

AnnotationPointCut.java

Package com.vector;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.stereotype.Component;// tagging this class is a facet @ Aspect@Component ("annotationPointCut") / / turn on the aop annotation driver @ EnableAspectJAutoProxypublic class AnnotationPointCut {@ Before ("execution (* com.vector.service.UserServiceImpl.* (..)")) Public void before () {System.out.println ("= = before method execution = =");} @ After ("execution (* com.vector.service.UserServiceImpl.* (..)") Public void after () {System.out.println ("= after method execution = =");}}

MyTest.java

Public class MyTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); / / dynamic proxy is interface UserService userService = (UserService) context.getBean ("userService"); userService.add ();}}

On how to understand Java Spring AOP to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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