只展示配置文件,相关测试代码参见:https://gitee.com/touchvan/spring-aop-example
pom.xml 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式1
使用实现Spring原生API接口配置
spring.xml 资源文件
<?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">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.example.aop.service.UserServiceImpl"/>
<!-- 配置aop -->
<!-- 方式1 实现Spring接口-->
<bean id="beforelog" class="com.example.aop.log.BeforeLog"/>
<bean id="afterlog" class="com.example.aop.log.AfterLog"/>
<aop:config>
<!-- 增加切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.example.aop.service.UserServiceImpl.*(..))"/>
<!-- 执行切入 -->
<aop:advisor advice-ref="beforelog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
方式2
使用自定义类的配置
<?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">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.example.aop.service.UserServiceImpl"/>
<!-- 配置aop -->
<!-- 方式2 自定义切面类 -->
<bean id="diypoint" class="com.example.aop.config.DiyPointCut"/>
<aop:config>
<!-- 执行切入 ref要引用的类的id-->
<aop:aspect ref="diypoint">
<!-- 增加切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.example.aop.service.UserServiceImpl.*(..))"/>
<!-- 执行切入 -->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
方式3
使用自定义类的配置
<?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">
<!-- 注册bean -->
<bean id="userServiceImpl" class="com.example.aop.service.UserServiceImpl"/>
<!-- 配置aop -->
<!-- 方式3 注解方式切面类 -->
<bean id="annotationPointCut" class="com.example.aop.config.AnnotationPointCut"/>
<!-- 开启注解支持 -->
<aop:aspectj-autoproxy/>
</beans>