最简单的Spring AOP实例

前言:这篇博客转载自李阿昀,为了梳理一下自己的思路

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

  • 基于XML配置方式声明切面。
  • 基于注解方式声明切面。
下面先说下基于Spring注解的方式声明切面。

1.导入jar包

AOP所需要的jar包有以下几个:

  • com.springsource.org.aopalliance-1.0.0.jar
  • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • spring-aop-4.2.5.RELEASE.jar
  • spring-aspects-4.2.5.RELEASE.jar
2.建一个类用作切入点

@Aspect
public class MyInterceptor {
	@Pointcut("execution (* it.cast.service.impl.PersonServiceImpl.*(..))")
    private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称
    }
}
3.我们首先在Spring配置文件中引入用于声明事务的tx命名空间:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

</beans>

最典型的切入点表达式是根据方法的签名来匹配各种方法:

  • execution (* cn.itcast.service.impl.PersonServiceImpl.*(..)):匹配PersonServiceImpl类中声明的所有方法。第一个*代表任意修饰符及任意返回值类型,第二个*代表任意方法,..匹配任意数量任意类型的参数,若目标类与该切面在同一个包中,可以省略包名。
  • execution public * cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中的所有公有方法。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中返回值类型为double类型的所有公有方法。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, ..):匹配PersonServiceImpl类中第一个参数为double类型,后面不管有无参数的所有公有方法,并且该方法的返回值类型为double类型。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, double):匹配PersonServiceImpl类中参数类型为double,double类型的,并且返回值类型也为double类型的所有公有方法。
4.声明前置通知方法。 
前置通知方法在目标方法开始之前执行。
@Aspect
public class MyInterceptor {
    @Pointcut("execution (* cn.itcast.service.impl.PersonServiceImpl.*(..))")
    private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称

    // 声明该方法是一个前置通知:在目标方法开始之前执行 
    @Before("anyMethod()")
    public void doAccessCheck() {
        System.out.println("前置通知");
    }
}
注意:若是将一个类声明为一个切面,那么需要把该类放到IOC容器管理

5.在Spring配置文件中配置数据源

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <aop:aspectj-autoproxy />
    <bean class="cn.itcast.service.MyInterceptor" />

</beans>

6.运行junit测试类










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值