下面列出了org.aspectj.lang.annotation.AfterThrowing#org.aopalliance.aop.Advice 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Advice advice = this.advisor.getAdvice();
sb.append(ClassUtils.getShortName(advice.getClass()));
sb.append(": ");
if (this.advisor instanceof Ordered) {
sb.append("order ").append(((Ordered) this.advisor).getOrder()).append(", ");
}
if (advice instanceof AbstractAspectJAdvice) {
AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice;
sb.append(ajAdvice.getAspectName());
sb.append(", declaration order ");
sb.append(ajAdvice.getDeclarationOrder());
}
return sb.toString();
}
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
}
private Advice[] getAdviceChainOrAddInterceptorToChain(Advice... existingAdviceChain) {
if (existingAdviceChain == null) {
return new Advice[] {rabbitMqReceiveTracingInterceptor};
}
for (Advice advice : existingAdviceChain) {
if (advice instanceof RabbitMqReceiveTracingInterceptor) {
return existingAdviceChain;
}
}
Advice[] newChain = new Advice[existingAdviceChain.length + 1];
System.arraycopy(existingAdviceChain, 0, newChain, 0, existingAdviceChain.length);
newChain[existingAdviceChain.length] = rabbitMqReceiveTracingInterceptor;
return newChain;
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 检测出耗时过高的方法调用并在日志中告警
* @param JoinPoint AOP切点
* @author Frodez
* @throws Throwable
* @date 2018-12-21
*/
return (MethodInterceptor) invocation -> {
String name = ReflectUtil.fullName(invocation.getMethod());
long threshold = thresholdCache.get(name);
long count = System.nanoTime();
Object result = invocation.proceed();
count = System.nanoTime() - count;
if (count > threshold) {
log.warn("{}方法耗时{}毫秒,触发超时警告!", name, count / times);
}
return result;
};
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 在请求前判断是否存在正在执行的请求
* @param JoinPoint AOP切点
* @author Frodez
* @throws Throwable
* @date 2018-12-21
*/
return (MethodInterceptor) invocation -> {
HttpServletRequest request = MVCUtil.request();
String key = KeyGenerator.servletKey(ReflectUtil.fullName(invocation.getMethod()), request);
try {
if (checker.check(key)) {
log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));
return Result.repeatRequest();
}
checker.lock(key);
return invocation.proceed();
} finally {
checker.free(key);
}
};
}
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[0]);
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 在一定时间段内拦截重复请求
* @param JoinPoint AOP切点
* @author Frodez
* @date 2018-12-21
*/
return (MethodInterceptor) invocation -> {
HttpServletRequest request = MVCUtil.request();
String name = ReflectUtil.fullName(invocation.getMethod());
String key = KeyGenerator.servletKey(name, request);
if (checker.check(key)) {
log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));
return Result.repeatRequest().async();
}
checker.lock(key, timeoutCache.get(name));
return invocation.proceed();
};
}
private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
if(!AopUtils.isAopProxy(proxy)) {
return false;
}
ProxyFactory proxyFactory = null;
if(AopUtils.isJdkDynamicProxy(proxy)) {
proxyFactory = findJdkDynamicProxyFactory(proxy);
}
if(AopUtils.isCglibProxy(proxy)) {
proxyFactory = findCglibProxyFactory(proxy);
}
Advisor[] advisors = proxyFactory.getAdvisors();
if(advisors == null || advisors.length == 0) {
return false;
}
for(Advisor advisor : advisors) {
if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
return true;
}
}
return false;
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 打印参数到日志
* @param JoinPoint AOP切点
* @author Frodez
* @date 2019-01-12
*/
return (MethodBeforeAdvice) (method, args, target) -> {
Parameter[] parameters = method.getParameters();
Map<String, Object> paramMap = new HashMap<>(parameters.length);
for (int i = 0; i < parameters.length; ++i) {
paramMap.put(parameters[i].getName(), args[i]);
}
log.info("{} 请求参数:{}", ReflectUtil.fullName(method), JSONUtil.string(paramMap));
};
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 请求限流
* @param JoinPoint AOP切点
* @author Frodez
* @date 2018-12-21
*/
return (MethodInterceptor) invocation -> {
Pair<RateLimiter, Long> pair = limitCache.get(ReflectUtil.fullName(invocation.getMethod()));
if (!pair.getKey().tryAcquire(pair.getValue(), DefTime.UNIT)) {
return Result.busy();
}
return invocation.proceed();
};
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 打印返回值到日志
* @param JoinPoint AOP切点
* @author Frodez
* @date 2019-01-12
*/
return (AfterReturningAdvice) (returnValue, method, args, target) -> log.info("{} 返回值:{}", ReflectUtil.fullName(method), JSONUtil
.string(returnValue));
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 对参数进行验证
* @author Frodez
* @date 2019-05-10
*/
return (MethodInterceptor) invocation -> {
String msg = ValidationUtil.validateParam(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
return msg == null ? invocation.proceed() : Result.errorRequest(msg);
};
}
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setProxyTargetClass(cglibProxy);
factory.addAdvice(advice);
factory.setTarget(bean);
return getObject(factory);
}
@Override
public int getOrder() {
if (this.order != null) {
return this.order;
}
Advice advice = getAdvice();
if (advice instanceof Ordered) {
return ((Ordered) advice).getOrder();
}
return Ordered.LOWEST_PRECEDENCE;
}
/**
* AOP切点
* @author Frodez
* @date 2019-05-10
*/
@Override
public Advice getAdvice() {
/**
* 对参数进行验证
* @author Frodez
* @date 2019-05-10
*/
return (MethodInterceptor) invocation -> {
String msg = ValidationUtil.validateParam(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
return msg == null ? invocation.proceed() : Result.errorRequest(msg).async();
};
}
@Override
public int indexOf(Advice advice) {
Assert.notNull(advice, "Advice must not be null");
for (int i = 0; i < this.advisors.size(); i++) {
Advisor advisor = this.advisors.get(i);
if (advisor.getAdvice() == advice) {
return i;
}
}
return -1;
}
@Override
public boolean removeAdvice(Advice advice) throws AopConfigException {
int index = indexOf(advice);
if (index == -1) {
return false;
}
else {
removeAdvisor(index);
return true;
}
}
/**
* Is the given advice included in any advisor within this proxy configuration?
* @param advice the advice to check inclusion of
* @return whether this advice instance is included
*/
public boolean adviceIncluded(Advice advice) {
if (advice != null) {
for (Advisor advisor : this.advisors) {
if (advisor.getAdvice() == advice) {
return true;
}
}
}
return false;
}
@Override
public int indexOf(Advice advice) {
Assert.notNull(advice, "Advice must not be null");
for (int i = 0; i < this.advisors.size(); i++) {
Advisor advisor = this.advisors.get(i);
if (advisor.getAdvice() == advice) {
return i;
}
}
return -1;
}
@SuppressWarnings("unchecked")
Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setProxyTargetClass(cglibProxy);
factory.addAdvice(advice);
factory.setTarget(bean);
return getObject(factory);
}
@Override
public Advice getAdvice() {
return (MethodInterceptor) invocation -> {
try {
return invocation.proceed();
} catch (Exception e) {
log.error(StrUtil.concat("[", ReflectUtil.fullName(invocation.getMethod()), "]"), e);
return Result.errorService();
}
};
}
/**
* リトライインタセプタ実行のアドバイスを指定し、DIコンテナに登録します。
* @return {@link #statefulRetryOperationsInterceptor()}
*/
@Bean
public Advice[] advice() {
return new Advice[] {
statefulRetryOperationsInterceptor()
};
}
/**
* Look at bean factory metadata to work out whether this bean name,
* which concludes the interceptorNames list, is an Advisor or Advice,
* or may be a target.
* @param beanName bean name to check
* @return {@code true} if it's an Advisor or Advice
*/
private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) {
Class<?> namedBeanClass = this.beanFactory.getType(beanName);
if (namedBeanClass != null) {
return (Advisor.class.isAssignableFrom(namedBeanClass) || Advice.class.isAssignableFrom(namedBeanClass));
}
// Treat it as an target bean if we can't tell.
if (logger.isDebugEnabled()) {
logger.debug("Could not determine type of bean with name '" + beanName +
"' - assuming it is neither an Advisor nor an Advice");
}
return false;
}
/**
* Return the AspectJPrecedenceInformation provided by this advisor or its advice.
* If neither the advisor nor the advice have precedence information, this method
* will return {@code null}.
*/
public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) {
if (anAdvisor instanceof AspectJPrecedenceInformation) {
return (AspectJPrecedenceInformation) anAdvisor;
}
Advice advice = anAdvisor.getAdvice();
if (advice instanceof AspectJPrecedenceInformation) {
return (AspectJPrecedenceInformation) advice;
}
return null;
}
@Override
public int indexOf(Advice advice) {
Assert.notNull(advice, "Advice must not be null");
for (int i = 0; i < this.advisors.size(); i++) {
Advisor advisor = this.advisors.get(i);
if (advisor.getAdvice() == advice) {
return i;
}
}
return -1;
}
/** Instruments an existing {@linkplain SimpleRabbitListenerContainerFactory} */
public SimpleRabbitListenerContainerFactory decorateSimpleRabbitListenerContainerFactory(
SimpleRabbitListenerContainerFactory factory
) {
Advice[] chain = factory.getAdviceChain();
TracingRabbitListenerAdvice tracingAdvice = new TracingRabbitListenerAdvice(this);
// If there are no existing advice, return only the tracing one
if (chain == null) {
factory.setAdviceChain(tracingAdvice);
return factory;
}
// If there is an existing tracing advice return
for (Advice advice : chain) {
if (advice instanceof TracingRabbitListenerAdvice) {
return factory;
}
}
// Otherwise, add ours and return
Advice[] newChain = new Advice[chain.length + 1];
newChain[0] = tracingAdvice;
System.arraycopy(chain, 0, newChain, 1, chain.length);
factory.setAdviceChain(newChain);
return factory;
}
@Override
public Advice getAdvice() {
return this.advice;
}
@Override
public boolean supportsAdvice(Advice advice) {
return (advice instanceof SimpleBeforeAdvice);
}
private Advice instantiateAdvice(AspectJExpressionPointcut pointcut) {
Advice advice = this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pointcut,
this.aspectInstanceFactory, this.declarationOrder, this.aspectName);
return (advice != null ? advice : EMPTY_ADVICE);
}
@Override
public Advice getAdvice() {
return this.interceptor;
}