标签:pre 添加 auto 类型 需要 标识 属性表 around 条件
AOP的好处:
- 每个事物逻辑位于同一层,代码不分散,便于维护和升级。
- 业务模块更简洁,只包含核心业务代码。
切点(pointcut):每个类都拥有多个连接点。AOP通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条件。
Java社区里最完善最流行的AOP框架。
在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。
当Spring IOC容器侦测到Bean配置文件中的
@Aspect
public class CalculatorLoggingAspect{
private Log log = LogFactory.getLog(this.getClass());
@Before("execution(* ArithmenticCalculator.add(..))")
public void logBefore(){
log.info("The method add() begins");
}
}
参数列表中的 . . 表示匹配任意数量的参数。
execution public double com.desperado.spring.ArithmeticCalculator.*(double,double):匹配ArithmeticCalculator接口的所有参数类型为double,double的公共方法。
@Pointcut("execution(* *.add(int,..)) || execution(* *.sub(int,..))")
private void loggingOperation(){}
@Before("loggingOperation()")
public void logBefore(JoinPoint joinPoint){
log.info("");
}
可以在通知方法中声明一个类型为JoinPoint的参数,然后就能访问连接细节,如方法名称和参数。
@Aspect
public class CalculatorLoggingAspect{
private Log log = LogFactory.getLog(this.getClass());
@After("execution(* ArithmenticCalculator.add(..))")
public void logBefore(){
log.info("The method add() begins");
}
}
无论连接点是正常返回还是抛出异常,后置通知都会执行,如果只想在连接点返回的时候记录日志,应使用返回通知大厅后置通知。
@Aspect
public class CalculatorLoggingAspect{
private Log log = LogFactory.getLog(this.getClass());
@AfterReturning("execution(* ArithmenticCalculator.add(..))")
public void logBefore(){
log.info("");
}
}
@AfterReturning(pointcut="execution(* *.*(..))",returning ="result")
public void logAfterReturning(JoinPoint joinPoint,Object result){
log.info("The method "+joinPoint.getSignature().getName()+" () ends with "+ result);
}
@AfterThrowing(pointcut="execution(* *.*(..))",throwing ="e")
public void logAfterReturning(JoinPoint joinPoint,ArithmeticExection e){
log.info("A exception "+e+" has been throwing ");
}
@Around(pointcut="execution(* *.*(..))")
public void logAround(proceedingJoinPoint joinPoint) throws Throwable{
log.info("A method begins");
try{
joinPoint.proceed();
log.info("The methods ends");
}catch(Throwable e){
log.info("An Exception has been throwing");
throw e;
}
}
@Aspect
@Order(0)
public class Aspect1{}
@Aspect
@Order(1)
public class Aspect2{}
@Pointcut("execution(* *.*(..))")
private void loggingOperation(){}
@Before("loggingOperation()")
public void logBefore(JoinPoint joinPoint){
log.info("The method begins ");
}
@AfterReturning("loggingOperation()",returning="result")
public coid logAfterReturning(JoinPoint joinPoint,Object result){
log.info("The method ends");
}
@AfterThrowing("loggingOperation()",throwing="result")
public coid logAfterReturning(JoinPoint joinPoint,Object result){
log.info("An exception has been throwing");
}
引入通知是一种特殊的通知类型,它通过为接口提供实现类,允许对象动态地实现接口,就像对象已经在运行时扩展了实现类一样。
public class CalculatorLoggingAspect implements Ordered {
private Log log = LoggerFactory.getLog(this.getClass());
@DeclareParents(value="* *.Arithmetic",defaultImpl=MaxCalculatorImpl.class)
private MaxCalculator maxCalculator;
@DeclareParenta(value="* *.Arithmetic",defaultImpl=MinCalculatorImpl.class)
private MinCalculator minCalculator;
MinCalculator minCalculator = (MinCalculator) ctx.getBean("airthmeticCalculator");
minCalculator.min(1,2);
}
<bean id="aspect1" class="com.desperado.Aspect1"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect1"></aop:aspect>
</aop:config>
基于XML ---- 声明切入点
<aop:config>
<aop:pointcut id="testOperation" expression="execution(* com.desperado.bean.Arithmetic*.*(..))"/>
</aop:config>
基于XML-----声明通知
<aop:config>
<aop:pointcut id="testOperation" expression="execution(* com.desperado.bean.Arithmetic*.*(..))"/>
<aop:aspect id="loggingAspect" ref="calculatorLOggingAspect">
<aop:after method="logBefore" pointcut-ref="textOperation">
</aop:aspect>
</aop:config>
声明引入
可以利用
<aop:config>
<aop:aspect id="loggingAspect" ref="calculatorLOggingAspect">
<aop:after method="logBefore" pointcut-ref="textOperation">
<aop:declare-parents
types-matching="com.desperado.Arithmetic*"
implement-interface="com.desperado.Mincalculator"
default-impl="com.desperado.MinCalculatorImpl"/>
</aop:aspect>
</aop:config>
标签:pre 添加 auto 类型 需要 标识 属性表 around 条件
原文地址:https://www.cnblogs.com/jack1995/p/10952954.html