Spring aop配置
为了方便只写了一个service层,首先先导入约束
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
</beans>
对service进行配置
<bean id="csutomerService" class="com.itheima.service.impl.CustomerServiceImpl"></bean>
下面就开始进行Spring的aop配置,采用的是xml的方式
第一步:先把通知类交给Spring来管理
<bean id="logger" class="com.itheima.service.utils.Logger"></bean>
第二步:导入命名空间,并使用aop:config来进行配置
第三步:使用aop:aspect来进行切面配置
第四步:配置通知类的类型,指定增强的方法何时执行
<!-- 第二步:导入AOP的命名空间,并且使用AOP:config来进行配置 -->
<aop:config>
<!--定义切入点表达式 -->
<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
<!-- 第三步:使用aop:aspect来配置切面 id属性:指的是切面的唯一标识 ref:指的是通知Bean的id -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 第四步:配置通知类的类型,指定增强的方法何时执行 -->
<aop:before method="printLog" pointcut-ref="pt1"/>
</aop:aspect>
</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的术语:
还没有评论,来说两句吧...