Spring 使用xml配置AOP的过程详解
1.前言
在之前的学习中,都是使用注解的方式进行AOP的配置.其实使用xml配置文件也可以配置AOP.
2.xml配置AOP
xml配置AOP方法如下:
添加相关依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.29</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies> 相关bean放到Spring容器中
@Service
public class StudentService {
public void insert(){
System.out.println("StudentService中的insert方法");
}
}创建切面类注入到Spring中,我这里使用的是@Component注解,也可以在配置文件中使用Bean标签
@Component
public class Aspect {
@Pointcut("execution(* com.example.service..*.*(..)")
public void pt(){
System.out.println("");
}
public void methodBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
Object target = joinPoint.getTarget();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
System.out.println("Before");
}
}在配置文件中开启组件扫描(因为我在将相应的Bean注入到Spring中时,使用的是注解,如果使用Bean标签,这一步可以省略)
<context:component-scan base-package="com.example"> </context:component-scan>
在配置文件中配置AOP,将切面类(StudentService)中的methodBefore方法设置为前置通知
<aop:config>
<!--定义切面-->
<aop:pointcut id="pt" expression="execution(* com.example.service..*.*(..))"/>
<!--配置切面-->
<aop:aspect ref="aspect">
<!--配置通知类型-->
<!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
<aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
</aop:aspect>
</aop:config>配置通知类型中有两种写法,一种是用pointcut-ref属性,值是定义切面时的id,另一种是使用pointcut属性,需要指定切点方法的全类名
运行结果:

可以看到成功将StudentService中的methodBefore方法设置为前置通知了
接下来讲一下复杂的通知如何配置,如下:
@AfterReturning(value = "point()",returning = "ret")
public void methodAfterReturning(JoinPoint joinPoint, Object ret){
// 方法体
}
@AfterThrowing(value = "point()",throwing = "e")
public void methodAfterThrowing(JoinPoint joinPoint,Throwable e){
// 方法体
}@AfterReturning和@AfterThrowing是有两个参数的
以@AfterReturning为例,在切面类中添加对应的普通方法:
@Component
public class Aspect {
@Pointcut("execution(* com.example.service..*.*(..))")
public void pt(){
System.out.println("");
}
public void methodBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
Object target = joinPoint.getTarget();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
System.out.println("Before");
}
public void methodAfterReturning(JoinPoint joinPoint, Object ret){
System.out.println("AfterReturning: "+ ret);
}
}<aop:aspect ref="aspect">
<!--配置通知类型-->
<!-- <aop:before method="methodBefore" pointcut-ref="pt"/> -->
<aop:before method="methodBefore" pointcut="com.example.aspect.Aspect.pt()"/>
<aop:after-returning method="methodAfterReturning" pointcut-ref="pt" returning="ret"/>
</aop:aspect>需要注意在设置AOP时,标签中有returning这样一个属性
运行结果:

3. 总结
xml配置AOP的重要步骤:
- 定义切面类,定义切点.
- 将目标类和切面类添加到Spring容器中(注解或Bean标签),如果是注解方式,需要添加组件扫描
- 在配置文件中配置AOP
在实际开发中,用注解配置AOP比较多,xml配置AOP了解即可
到此这篇关于Spring 使用xml配置AOP的文章就介绍到这了,更多相关spring配置aop内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决MyEclipse10.7部署报错抛空指针异常问题的方法
这篇文章主要介绍了解决MyEclipse10.7部署报错抛空指针异常问题的方法,需要的朋友可以参考下2015-12-12
Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】
这篇文章主要介绍了Java日期操作方法工具类,结合完整实例形式分析了java针对日期的各种常见操作,包括日期比较大小,相加减,判断,验证,获取年份、天数、星期等,需要的朋友可以参考下2017-11-11
使用IDEA如何打包发布SpringBoot并部署到云服务器
这篇文章主要介绍了使用IDEA如何打包发布SpringBoot并部署到云服务器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
详解eclipse中Maven工程使用Tomcat7以上插件的方法
本篇文章主要介绍了详解eclipse中Maven工程使用Tomcat7以上插件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12


最新评论