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 Around enhanced implementation of spring AOP?

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

Share

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

This article mainly introduces "what is the Around enhancement implementation method of spring AOP". In the daily operation, I believe that many people have doubts about the Around enhancement implementation method of spring AOP. 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 of "what is the Around enhancement implementation method of spring AOP?" Next, please follow the editor to study!

One configuration

Dichotomous class

Package org.crazyit.app.aspect;import org.aspectj.lang.annotation.*;import org.aspectj.lang.*;// defines an aspect @ Aspectpublic class TxAspect {/ / matches the execution of all classes and / / all methods under the org.crazyit.app.service.impl package as the pointcut @ Around ("execution (* org.crazyit.app.service.impl.*.* (..)") Public Object processTx (ProceedingJoinPoint jp) throws java.lang.Throwable {System.out.println ("Simulation starts a transaction before executing the target method."); / / get the original call parameter of the target method Object [] args = jp.getArgs (); if (args! = null & & args.length > 1) {/ / modify the first parameter of the target method args [0] = "[added prefix]" + args [0] } / / execute the target method with the changed parameters, and save the return value Object rvt = jp.proceed (args); System.out.println ("after the target method is executed, the simulation ends the transaction."); / / if the type of rvt is Integer, change rvt to its square if (rvt! = null & & rvt instanceof Integer) rvt = (Integer) rvt * (Integer) rvt; return rvt;}

Three interfaces

Hello

Package org.crazyit.app.service;public interface Hello {/ / defines a simple method to simulate the business logic method void foo () in the application; / / defines an addUser () method to simulate the method int addUser (String name, String pass) to add users in the application;}

World

Package org.crazyit.app.service;public interface World {/ / defines a simple method that simulates the business logic method public void bar () in the application.

Four implementation classes

HelloImpl

Package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component ("hello") public class HelloImpl implements Hello {/ / defines a simple method to simulate the business logic method public void foo () {System.out.println ("execute the foo () method of the Hello component") in the application } / / define an addUser () method that simulates the method of adding users in the application public int addUser (String name, String pass) {System.out.println ("addUser adding users for executing Hello components:" + name); return 20;}}

WorldImpl

Package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component ("world") public class WorldImpl implements World {/ / defines a simple method to simulate the business logic method public void bar () {System.out.println ("execute the bar () method of the World component");}}

Five test categories

Package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest {public static void main (String [] args) {/ / create Spring container ApplicationContext ctx = new ClassPathXmlApplicationContext ("beans.xml"); Hello hello = ctx.getBean ("hello", Hello.class); hello.foo (); hello.addUser ("Sun WuKong", "7788"); World world = ctx.getBean ("world", World.class); world.bar () }}

Six test results

Before executing the target method, the simulation starts the transaction. After the foo () method of the Hello component executes the target method, the simulation ends the transaction. Before executing the target method, the simulation starts the transaction. The addUser that executes the Hello component adds the user: [added prefix] after Sun WuKong executes the target method, the simulation ends the transaction. The return value of addUser () is: 400simulates the start of the transaction before executing the target method. After the bar () method of the World component executes the target method, the simulation ends the transaction.

At this point, the study on "what is the Around enhancement implementation of spring AOP" 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

Development

Wechat

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

12
Report