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 are the three ways to implement springAOP?

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

Share

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

In this issue, Xiaobian will bring you about the three implementations of springAOP. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

SpringAOP implementation

three

Pure XML mode, XML+ annotation, pure annotation mode.

Spring implements AOP ideas using dynamic proxy technology

By default, Spring selects whether to use JDK or CGLIB based on whether the proxied object implements connectivity. When the proxied object is not implemented

Spring selects CGLIB for any connection. When the proxied object implements the interface, Spring selects the JDK official proxy technology, but

We can force Spring to use CGLIB by configuring the formula.

Next we start implementing aop,

Requirements are: crosscutting logic code is a print log, and you want to organize the logic of the print log to a specific location of the target method (service layer transfer method)

pure XML approach

Introduction of aop-related jar packages

org.springframework spring-aop 5.1.12.RELEASE org.aspectj aspectjweaver 1.9.4

TransferServiceImpl.java file:

package com.lagou.edu.service.impl;import com.lagou.edu.dao.AccountDao;import com.lagou.edu.pojo.Account;import com.lagou.edu.service.TransferService;import com.lagou.edu.utils.ConnectionUtils;import com.lagou.edu.utils.TransactionManager;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.ImportResource;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;/** * @author should be */@Service("transferService")public class TransferServiceImpl implements TransferService { //best condition // @Autowired is injected by type, if the object cannot be uniquely locked by type, you can specify a specific id in combination with @Qualifier @Autowired @Qualifier("accountDao") private AccountDao accountDao; @Override public void transfer(String fromCardNo, String toCardNo, int money) throws Exception { /*try{ //open transactions (close automatic commit of transactions) TransactionManager.getInstance().beginTransaction();*/ System.out.println("Execute Transfer Business Logic"); Account from = accountDao.queryAccountByCardNo(fromCardNo); Account to = accountDao.queryAccountByCardNo(toCardNo); from.setMoney(from.getMoney()-money); to.setMoney(to.getMoney()+money); accountDao.updateAccountByCardNo(to); //int c = 1/0; accountDao.updateAccountByCardNo(from); }}

Print Log Util:

package com.lagou.edu.utils;/** * @author should */public class LogUtils { /** * Execution before business logic starts */ public void beforeMethod(JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); for (int i = 0; i

< args.length; i++) { Object arg = args[i]; System.out.println(arg); } System.out.println("业务逻辑开始执行之前执行......."); } /** * 业务逻辑结束时执行(无论异常与否) */ public void afterMethod() { System.out.println("业务逻辑结束时执行,无论异常与否都执行......."); } /** * 异常时时执行 */ public void exceptionMethod() { System.out.println("异常时执行......."); } /** * 业务逻辑正常时执行 */ public void successMethod(Object retVal) { System.out.println("业务逻辑正常时执行......."); }}public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕通知中的beforemethod...."); Object result = null; try{ // 控制原有业务逻辑是否执行 // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); }catch(Exception e) { System.out.println("环绕通知中的exceptionmethod...."); }finally { System.out.println("环绕通知中的after method...."); } return result; } applicationContext.xml -->

测试:

/** * 测试xml aop */ @Test public void testXmlAop() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); TransferService transferService = applicationContext.getBean(TransferService.class); transferService.transfer("6029621011000","6029621011001",100); }

环绕通知不和前置及后置通知一起使用,因为环绕通知可以实现前置和后置的功能,并且可以控制原有业务逻辑是否执行,非常强大。

XML+注解方式

将上面纯XML方式改为注解方式

将applicationContext.xml中的内容取掉,改为类中添加注解:

package com.lagou.edu.utils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;/** * @author 应癫 */@Component@Aspectpublic class LogUtils { @Pointcut("execution(* com.lagou.edu.service.impl.TransferServiceImpl.*(..))") public void pt1(){ } /** * 业务逻辑开始之前执行 */ @Before("pt1()") public void beforeMethod(JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { Object arg = args[i]; System.out.println(arg); } System.out.println("业务逻辑开始执行之前执行......."); } /** * 业务逻辑结束时执行(无论异常与否) */ @After("pt1()") public void afterMethod() { System.out.println("业务逻辑结束时执行,无论异常与否都执行......."); } /** * 异常时时执行 */ @AfterThrowing("pt1()") public void exceptionMethod() { System.out.println("异常时执行......."); } /** * 业务逻辑正常时执行 */ @AfterReturning(value = "pt1()",returning = "retVal") public void successMethod(Object retVal) { System.out.println("业务逻辑正常时执行......."); } /** * 环绕通知 * */ /*@Around("pt1()")*/ public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕通知中的beforemethod...."); Object result = null; try{ // 控制原有业务逻辑是否执行 // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs()); }catch(Exception e) { System.out.println("环绕通知中的exceptionmethod...."); }finally { System.out.println("环绕通知中的after method...."); } return result; }}

在application.xml中配置注解驱动:

纯注解模式

我们只需要替换掉xml+注解模式中的注解驱动的部分即可,

改为 @EnableAspectJAutoProxy //开启spring对注解AOP的⽀持,在项目中添加到任意个配置类上即可。

上述就是小编为大家分享的springAOP的三种实现方式分别是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

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