Spring aop配置

秒速五厘米 2021-06-11 15:13 564阅读 0赞

为了方便只写了一个service层,首先先导入约束

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd
  10. ">
  11. </beans>

对service进行配置

  1. <bean id="csutomerService" class="com.itheima.service.impl.CustomerServiceImpl"></bean>

下面就开始进行Spring的aop配置,采用的是xml的方式

第一步:先把通知类交给Spring来管理

  1. <bean id="logger" class="com.itheima.service.utils.Logger"></bean>

第二步:导入命名空间,并使用aop:config来进行配置

第三步:使用aop:aspect来进行切面配置

第四步:配置通知类的类型,指定增强的方法何时执行

  1. <!-- 第二步:导入AOP的命名空间,并且使用AOP:config来进行配置 -->
  2. <aop:config>
  3. <!--定义切入点表达式 -->
  4. <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
  5. <!-- 第三步:使用aop:aspect来配置切面 id属性:指的是切面的唯一标识 ref:指的是通知Bean的id -->
  6. <aop:aspect id="logAdvice" ref="logger">
  7. <!-- 第四步:配置通知类的类型,指定增强的方法何时执行 -->
  8. <aop:before method="printLog" pointcut-ref="pt1"/>
  9. </aop:aspect>
  10. </aop:config>

method属性:指定要增强方法的名称
pointcut:用于指定切入点表达式
切入点表达式:
关键字:execution(表达式)、
表达式的写法:访问修饰符 返回值 包名.包名…类名.方法名(参数列表)
全匹配方式:
execution(public void com.itheima.service.impl.CustomerServiceImpl.saveCustomer())
全匹配方式:
execution(* *..*.*(..))
关于切入点表达式:修饰符可以省略不写:
void com.itheima.service.impl.CustomerServiceImpl.saveCustomer()
参数类别可以写基本类,也可以写引用类
基本类型可以直接写类型名称:int
引用类的写法是: 包名.类名. 例如:java.lang.Integer

aop的术语:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0dyYXlfaHVtb3I_size_27_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,564人围观)

还没有评论,来说两句吧...

相关阅读