Spring AOP切面的3种实现方式

只展示配置文件,相关测试代码参见: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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值