org.aopalliance.aop.Advice Java Examples

The following examples show how to use org.aopalliance.aop.Advice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RepeatAdvisor.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
		}
	};
}
 
Example #2
Source File: DurationLogAdvisor.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	};
}
 
Example #3
Source File: DefaultAdvisorAdapterRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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]);
}
 
Example #4
Source File: AsyncTimeoutAdvisor.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
	};
}
 
Example #5
Source File: AopProxyUtils.java    From phone with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: RabbitMqBeanPostProcessor.java    From java-spring-rabbitmq with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: ParamLogAdvisor.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
	};
}
 
Example #8
Source File: DefaultAdvisorAdapterRegistry.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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()]);
}
 
Example #9
Source File: AspectJAwareAdvisorAutoProxyCreator.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example #10
Source File: LimitUserAdvisor.java    From BlogManagePlatform with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
	};
}
 
Example #11
Source File: AdvisedSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #12
Source File: AsyncValidationAdvisor.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
	};
}
 
Example #13
Source File: AbstractPointcutAdvisor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #14
Source File: ExecutorBeanPostProcessor.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: AdvisedSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #16
Source File: ValidationAdvisor.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	};
}
 
Example #17
Source File: AdvisedSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeAdvice(Advice advice) throws AopConfigException {
	int index = indexOf(advice);
	if (index == -1) {
		return false;
	}
	else {
		removeAdvisor(index);
		return true;
	}
}
 
Example #18
Source File: AdvisedSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #19
Source File: ResultLogAdvisor.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example #20
Source File: ExecutorBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: CatchAndReturnAdvisor.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@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();
		}
	};
}
 
Example #22
Source File: ExceptionQueueContextConfig.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * リトライインタセプタ実行のアドバイスを指定し、DIコンテナに登録します。
 * @return {@link #statefulRetryOperationsInterceptor()}
 */
@Bean
public Advice[] advice() {
    return new Advice[] {
        statefulRetryOperationsInterceptor()
    };
}
 
Example #23
Source File: ProxyFactoryBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #24
Source File: AspectJAopUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #25
Source File: AdvisedSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example #26
Source File: SpringRabbitTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
/** 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;
}
 
Example #27
Source File: InstantiationModelAwarePointcutAdvisorImpl.java    From spring-analysis-note with MIT License 4 votes vote down vote up
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);
}
 
Example #28
Source File: AdvisorAdapterRegistrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsAdvice(Advice advice) {
	return (advice instanceof SimpleBeforeAdvice);
}
 
Example #29
Source File: TransactionAttributeSourceAdvisor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Advice getAdvice() {
	Assert.state(this.transactionInterceptor != null, "No TransactionInterceptor set");
	return this.transactionInterceptor;
}
 
Example #30
Source File: PersistenceExceptionTranslationAdvisor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Advice getAdvice() {
	return this.advice;
}