Spring AOP

AOP

面向切面编程(Aspect Oriented Programming)
在纵向流程中添加横切面,不需要修改原有程序代码

  • 高扩展性
  • 原有功能释放部分逻辑

常用概念

切点 pointcut
前置通知 before advice
实现 MethodBeforeAdvice接口
后置通知 after advice
实现 AfterReturningAdvice 接口
环绕通知 around advice
实现 MethodInteceptor接口
异常通知 throws advice
实现 ThrowsAdvice 接口
所有功能总称 切面
织入 把切面嵌入到原有功能

依赖jar包

aopalliance(1).jar
aspectjweaver.jar

<?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="myBefore" class=""></bean>
	<!-- 切面配置-->
		<aop:config>
			<!--配置切点 -->
			<aop:pointcut expression="execution(* com.jhl.test.Demo.demo2(..))" id="p1"/>
			<aop:advisor advice-ref="myBefore"  pointcut-ref="p1"/>
		</aop:config>

AspectJ

配置信息

<aop:config>
			<aop:aspect ref="">
				<aop:pointcut expression="" id=""/>
				<!-- 异常通知  throwing 异常对象名 与参数名一致-->
				<aop:after-throwing method="" pointcut-ref="" throwing=""/>
			</aop:aspect>
		</aop:config>

注解

<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"
    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.xsd">

    <context:annotation-config/>
    <!--扫描--> 
<context:component-scan base-package=""/>
<!-- true cglib 动态代理     false JDK动态店里  -->
<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
</beans>
  • @Compoent Bean的注解
  • @Pointcut 切点的注解
  • @Aspect 切面
  • @Before 前置通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值