下面列出了org.springframework.core.annotation.AnnotatedElementUtils#hasAnnotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isSupport(Method method) {
if (method.isSynthetic() || method.isBridge())
return false;
// 如果方法和类上都没有ResponseBody,返回false
if (!AnnotatedElementUtils.hasAnnotation(method, ResponseBody.class) &&
!AnnotatedElementUtils.hasAnnotation(method.getDeclaringClass(), ResponseBody.class))
{
return false;
}
var annotations = method.getAnnotations();
for (var annotation : annotations) {
var annotationType = annotation.annotationType();
if (_classes.contains(annotationType))
return true;
}
return false;
}
@Override
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), ApiController.class)) {
binder.validate();
} else {
Annotation[] annotations = parameter.getParameterAnnotations();
for (Annotation ann : annotations) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
Object[] validationHints = (hints instanceof Object[] ? (Object[])hints : new Object[] {hints});
binder.validate(validationHints);
break;
}
}
}
}
@Override
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
if (handler instanceof String) {
String beanName = (String) handler;
handler = this.getApplicationContext().getAutowireCapableBeanFactory().getBean(beanName);
}
if (AnnotatedElementUtils.hasAnnotation(method, WxAsyncMessage.class) || AnnotatedElementUtils.hasAnnotation(handler.getClass(), WxAsyncMessage.class)) {
return new HandlerMethod(wxAsyncHandlerFactory.createHandler(handler), method);
} else {
return new HandlerMethod(handler, method);
}
}
/**
* Returns {@link WebDelegatingSmartContextLoader} if the supplied class is
* annotated with {@link WebAppConfiguration @WebAppConfiguration} and
* otherwise delegates to the superclass.
*/
@Override
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
if (AnnotatedElementUtils.hasAnnotation(testClass, WebAppConfiguration.class)) {
return WebDelegatingSmartContextLoader.class;
}
else {
return super.getDefaultContextLoaderClass(testClass);
}
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), ApiController.class)){
ApiController ann = AnnotationUtils.findAnnotation(parameter.getContainingClass(), ApiController.class);
if(ann.requestBody()&&!hasSpringAnnotation(parameter)){
return true;
}
}
return super.supportsParameter(parameter);
}
@Override
protected boolean isHandler(Class<?> beanType) {
//避免springmvc 把抽象出来的带有RequestMapping注解的client接口扫描到controller注册
boolean isFeignClient = AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
if(BootCloudUtils.isNetflixFeignClientPresent() && isFeignClient){
if(log.isInfoEnabled()){
log.info("ignore FeignClient: {}", beanType);
}
return false;
}
return super.isHandler(beanType);
}
@Override
public boolean supportsMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
}
public static boolean isBeanAnnotated(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, Bean.class);
}
@Override
protected boolean isHandler(Class<?> beanType) {
return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
}
/**
* {@inheritDoc}
* Expects a handler to have a type-level @{@link Controller} annotation.
*/
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
/**
* {@inheritDoc}
* Expects a handler to have a type-level @{@link Controller} annotation.
*/
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
/**
* {@inheritDoc}
* <p>Expects a handler to have either a type-level @{@link Controller}
* annotation or a type-level @{@link RequestMapping} annotation.
*/
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
@Override
public boolean supportsMethod(Method method) {
return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
}
/**
* {@inheritDoc}
* <p>Expects a handler to have either a type-level @{@link Controller}
* annotation or a type-level @{@link RequestMapping} annotation.
*/
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||
returnType.hasMethodAnnotation(ResponseBody.class));
}
/**
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* should be autowired from the test's {@link ApplicationContext}.
* <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
* that is annotated with {@link Autowired @Autowired} and otherwise delegates to
* {@link ParameterAutowireUtils#isAutowirable}.
* <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
* that is annotated with {@code @Autowired}, Spring will assume the responsibility
* for resolving all parameters in the constructor. Consequently, no other registered
* {@link ParameterResolver} will be able to resolve parameters.
* @see #resolveParameter
* @see ParameterAutowireUtils#isAutowirable
*/
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
int index = parameterContext.getIndex();
Executable executable = parameter.getDeclaringExecutable();
return (executable instanceof Constructor &&
AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
ParameterAutowireUtils.isAutowirable(parameter, index);
}
/**
* Determine if the supplied {@link Parameter} can <em>potentially</em> be
* autowired from an {@link AutowireCapableBeanFactory}.
* <p>Returns {@code true} if the supplied parameter is annotated or
* meta-annotated with {@link Autowired @Autowired},
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
* <p>Note that {@link #resolveDependency} may still be able to resolve the
* dependency for the supplied parameter even if this method returns {@code false}.
* @param parameter the parameter whose dependency should be autowired
* (must not be {@code null})
* @param parameterIndex the index of the parameter in the constructor or method
* that declares the parameter
* @see #resolveDependency
*/
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
Assert.notNull(parameter, "Parameter must not be null");
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
}
/**
* Determine if the supplied {@link Parameter} can potentially be
* autowired from an {@link ApplicationContext}.
* <p>Returns {@code true} if the supplied parameter is of type
* {@link ApplicationContext} (or a sub-type thereof) or is annotated or
* meta-annotated with {@link Autowired @Autowired},
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
* @param parameter the parameter whose dependency should be autowired
* @param parameterIndex the index of the parameter
* @see #resolveDependency
*/
static boolean isAutowirable(Parameter parameter, int parameterIndex) {
if (ApplicationContext.class.isAssignableFrom(parameter.getType())) {
return true;
}
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
}
/**
* Return whether the parameter is declared with the given annotation type.
* @param annotationType the annotation type to look for
* @since 4.3
* @see AnnotatedElementUtils#hasAnnotation
*/
public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationType) {
return AnnotatedElementUtils.hasAnnotation(this.method, annotationType);
}
/**
* 只注册Controller,不注册feign接口
* @param beanType
* @return
*/
@Override
protected boolean isHandler(Class<?> beanType) {
return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
}