基于代理的经典AOP
基于代理工厂
1.新建通知对象
2.把通知对象放到spring 容器
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="xxx" /> <!-- 代理bean工厂中完成织入 --> <bean id="名称" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="目标" /> <property name="interceptorNames"> <list> <value>通知对象</value> </list> </property> </bean> </beans>自动代理方式
1.新建通知对象
2.把通知对象放到spring 容器
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="xxx" /> <!-- 自动代理 --> <aop:aspectj-autoproxy /> <!-- Advisor 通知代理者 --> <bean id="名称" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="pattern"> <value>表达式</value> </property> <property name="advice" ref="通知" /> </bean> </beans>基于Aspectj 表达式配置AOP(推荐使用)
基于配置文件
1.添加切面对象(aspect对象)
2.把切面对象交给spring
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="com.chair" /> <aop:config> <!-- 配置切入点 --> <aop:pointcut id="切点id" expression="Aspectj 表达式" /> <!-- 配置切面 把增强用到方法上面 --> <aop:aspect ref="切面对象id"> <!-- 前置通知 --> <aop:before method="切面对象方法" pointcut-ref="切点id" /> <!-- 后置通知 --> <aop:after-returning method="切面对象方法" pointcut-ref="切点id" /> <!-- 异常通知 --> <aop:after-throwing method="切面对象方法" pointcut-ref="切点id" /> <!-- 环绕通知 --> <aop:around method="切面对象方法" pointcut-ref="切点id" /> </aop:aspect> </aop:config> </beans>基于注解配置
1.添加切面对象(aspect对象)(加上注解)
2.把切面对象交给spring
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- spring扫描包 --> <context:component-scan base-package="com.chair" /> <!-- 自动代理 --> <aop:aspectj-autoproxy /> </beans>@Before[MethodBeforeAdvice]: 前置通知, 在方法执行之前执行 @After [AfterReturningAdvice]: 返回通知, 在方法返回结果之后执行 @AfterThrowing [ThrowsAdvice]: 异常通知, 在方法抛出异常之后 @Around[MethodInterceptor]: 环绕通知, 围绕着方法执行
https://blog.csdn.net/xubo_ob/article/details/78182014