Spring_AOP代码修改_Introductions & Advisors

Introductions

types-matching 指定声明类

implement-interface 指定接口

default-impl 指定接口实现类,将成为生命类的父类





<aop:config>
		<aop:aspect id="moocAspectAOP" ref="moocAspect">
<!-- 			<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/> -->
<!-- 			<aop:before method="before" pointcut-ref="moocPiontcut"/> -->
<!-- 			<aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/> -->
<!-- 			<aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut"/> -->
<!-- 			<aop:after method="after" pointcut-ref="moocPiontcut"/> -->
<!-- 			<aop:around method="around" pointcut-ref="moocPiontcut"/> -->
			
<!-- 			<aop:around method="aroundInit" pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String, int))  -->
<!-- 							and args(bizName, times)"/> -->

				<aop:declare-parents types-matching="com.imooc.aop.schema.advice.biz.*(+)" 
							implement-interface="com.imooc.aop.schema.advice.Fit"
							default-impl="com.imooc.aop.schema.advice.FitImpl"/>
		</aop:aspect>
	</aop:config>




aspect 只支持单例



advisors


           



InvokeService:

import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.stereotype.Service;

@Service
public class InvokeService {
	
	public void invoke() {
		System.out.println("InvokeService ......");
	}
	
	public void invokeException() {
		throw new PessimisticLockingFailureException("");
	}

}

ConcurrentOperationExecutor:

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.dao.PessimisticLockingFailureException;

public class ConcurrentOperationExecutor implements Ordered {

	private static final int DEFAULT_MAX_RETRIES = 2;

	private int maxRetries = DEFAULT_MAX_RETRIES;
	
	private int order = 1;

	public void setMaxRetries(int maxRetries) {
		this.maxRetries = maxRetries;
	}

	public int getOrder() {
		return this.order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
		int numAttempts = 0;
		PessimisticLockingFailureException lockFailureException;
		do {
			numAttempts++;
			System.out.println("Try times : " + numAttempts);
			try {
				return pjp.proceed();
			} catch (PessimisticLockingFailureException ex) {
				lockFailureException = ex;
			}
		} while (numAttempts <= this.maxRetries);
		System.out.println("Try error : " + numAttempts);
		throw lockFailureException;
	}
}

Test:

@Test
	public void testSave() {
		InvokeService service = super.getBean("invokeService");
		service.invoke();
		
		System.out.println();
		service.invokeException();
 	}


配置:

<?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:context="http://www.springframework.org/schema/context"
    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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<context:component-scan base-package="com.imooc.aop.schema"></context:component-scan>

	<aop:config>
		<aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">
			<aop:pointcut id="idempotentOperation"
				expression="execution(* com.imooc.aop.schema.advisors.service.*.*(..)) " />
<!--      			expression="execution(* com.imooc.aop.schema.service.*.*(..)) and -->
<!--         						@annotation(com.imooc.aop.schema.Idempotent)" /> -->
			<aop:around pointcut-ref="idempotentOperation" method="doConcurrentOperation" />
		</aop:aspect>
	</aop:config>
	
	<bean id="concurrentOperationExecutor" class="com.imooc.aop.schema.advisors.ConcurrentOperationExecutor">
		<property name="maxRetries" value="3" />
		<property name="order" value="100" />
	</bean>

 </beans>

结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值