未调用的Spring方面
给定一个带有 AspectJ配置:未调用的Spring方面,spring,aop,aspectj,spring-aop,Spring,Aop,Aspectj,Spring Aop,给定一个带有 AspectJ配置: @Configuration @EnableAspectJAutoProxy public class AspectJConfiguration { } 方面: @Aspect public class DataAccessExceptionAspect implements ThrowsAdvice { @AfterThrowing(pointcut = "execution(* com.acme.dao.*(..))", throwing =
@Configuration
@EnableAspectJAutoProxy
public class AspectJConfiguration {
}
方面:
@Aspect
public class DataAccessExceptionAspect implements ThrowsAdvice {
@AfterThrowing(pointcut = "execution(* com.acme.dao.*(..))",
throwing = "e")
public void afterThrowing(JoinPoint joinPoint,
DataAccessException e) throws Throwable {
// do something
throw new AppSpecificCustomException();
}
}
道:
NamedParameterJdbcTemplate.queryForObject引发DataAccessException
我希望当getWidget()被传递一个不存在的widgetId时,会抛出一个DataAccessException(特别是EmptyResultDataAccessException),并且方面会捕获它。异常被抛出,但方面从未看到它。我已尝试将特性的签名更改为:
public void afterThrowing(JoinPoint joinPoint,
EmptyResultDataAccessException e) throws Throwable
这没有什么区别。我通过做两个更改来解决这个问题 1) 方面需要由Spring管理。添加@Component注释可以解决这个问题 2) 切入点的定义是错误的。它应该是com.acme.dao.WidgetDAO*
@Component // @Component annotation is required
@Aspect
public class DataAccessExceptionAspect implements ThrowsAdvice {
@AfterThrowing(pointcut = "execution(* com.acme.dao.WidgetDAO.*(..))",
throwing = "e")
public void afterThrowing(JoinPoint joinPoint,
DataAccessException e) throws Throwable {
// do something
throw new AppSpecificCustomException();
}
}
我做了两个改变来解决这个问题 1) 方面需要由Spring管理。添加@Component注释可以解决这个问题 2) 切入点的定义是错误的。它应该是com.acme.dao.WidgetDAO*
@Component // @Component annotation is required
@Aspect
public class DataAccessExceptionAspect implements ThrowsAdvice {
@AfterThrowing(pointcut = "execution(* com.acme.dao.WidgetDAO.*(..))",
throwing = "e")
public void afterThrowing(JoinPoint joinPoint,
DataAccessException e) throws Throwable {
// do something
throw new AppSpecificCustomException();
}
}