springAOP的三种实现方式示例代码
这篇文章给大家介绍了springAOP的实现方式,三种分别是纯XML方式,XML+注解,纯注解方式。
Spring实现AOP思想使⽤的是动态代理技术
默认情况下,Spring会根据被代理对象是否实现接⼝来选择使⽤JDK还是CGLIB。当被代理对象没有实现
任何接⼝时,Spring会选择CGLIB。当被代理对象实现了接⼝,Spring会选择JDK官⽅的代理技术,不过
我们可以通过配置的⽅式,让Spring强制使⽤CGLIB。
接下来我们开始实现aop,
需求是:横切逻辑代码是打印⽇志,希望把打印⽇志的逻辑织⼊到⽬标⽅法的特定位置(service层transfer⽅法)
纯XML方式
引入aop相关的jar包
org.springframework spring-aop 5.1.12.RELEASE org.aspectj aspectjweaver 1.9.4
TransferServiceImpl.java文件:
packagecom.lagou.edu.service.impl;
importcom.lagou.edu.dao.AccountDao;
importcom.lagou.edu.pojo.Account;
importcom.lagou.edu.service.TransferService;
importcom.lagou.edu.utils.ConnectionUtils;
importcom.lagou.edu.utils.TransactionManager;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.ImportResource;
importorg.springframework.stereotype.Component;
importorg.springframework.stereotype.Service;
/**
*@author应癫
*/
@Service("transferService")
publicclassTransferServiceImplimplementsTransferService{
//最佳状态
//@Autowired按照类型注入,如果按照类型无法唯一锁定对象,可以结合@Qualifier指定具体的id
@Autowired
@Qualifier("accountDao")
privateAccountDaoaccountDao;
@Override
publicvoidtransfer(StringfromCardNo,StringtoCardNo,intmoney)throwsException{
/*try{
//开启事务(关闭事务的自动提交)
TransactionManager.getInstance().beginTransaction();*/
System.out.println("执行转账业务逻辑");
Accountfrom=accountDao.queryAccountByCardNo(fromCardNo);
Accountto=accountDao.queryAccountByCardNo(toCardNo);
from.setMoney(from.getMoney()-money);
to.setMoney(to.getMoney()+money);
accountDao.updateAccountByCardNo(to);
//intc=1/0;
accountDao.updateAccountByCardNo(from);
}
}
打印日志Util:
packagecom.lagou.edu.utils;
/**
*@author应癫
*/
publicclassLogUtils{
/**
*业务逻辑开始之前执行
*/
publicvoidbeforeMethod(JoinPointjoinPoint){
Object[]args=joinPoint.getArgs();
for(inti=0;i
applicationContext.xml
-->
测试:
/**
*测试xmlaop
*/
@Test
publicvoidtestXmlAop()throwsException{
ClassPathXmlApplicationContextapplicationContext=newClassPathXmlApplicationContext("classpath:applicationContext.xml");
TransferServicetransferService=applicationContext.getBean(TransferService.class);
transferService.transfer("6029621011000","6029621011001",100);
}
环绕通知不和前置及后置通知一起使用,因为环绕通知可以实现前置和后置的功能,并且可以控制原有业务逻辑是否执行,非常强大。
XML+注解方式
将上面纯XML方式改为注解方式
将applicationContext.xml中的内容取掉,改为类中添加注解:
packagecom.lagou.edu.utils;
importorg.aspectj.lang.JoinPoint;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.*;
importorg.springframework.stereotype.Component;
/**
*@author应癫
*/
@Component
@Aspect
publicclassLogUtils{
@Pointcut("execution(*com.lagou.edu.service.impl.TransferServiceImpl.*(..))")
publicvoidpt1(){
}
/**
*业务逻辑开始之前执行
*/
@Before("pt1()")
publicvoidbeforeMethod(JoinPointjoinPoint){
Object[]args=joinPoint.getArgs();
for(inti=0;i
在application.xml中配置注解驱动:
纯注解模式
我们只需要替换掉xml+注解模式中的注解驱动的部分即可,
将
改为@EnableAspectJAutoProxy//开启spring对注解AOP的⽀持,在项目中添加到任意个配置类上即可。
到此这篇关于springAOP的三种实现方式的文章就介绍到这了,更多相关springAOP实现方式内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!