Java Code Examples for org.springframework.web.method.ControllerAdviceBean#findAnnotatedBeans()

The following examples show how to use org.springframework.web.method.ControllerAdviceBean#findAnnotatedBeans() . 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: ExceptionHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for exception mappings: " + getApplicationContext());
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType());
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
			logger.info("Detected @ExceptionHandler methods in " + adviceBean);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(adviceBean.getBeanType())) {
			this.responseBodyAdvice.add(adviceBean);
			logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean);
		}
	}
}
 
Example 2
Source File: ControllerMethodResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initControllerAdviceCaches(ApplicationContext applicationContext) {
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
	AnnotationAwareOrderComparator.sort(beans);

	for (ControllerAdviceBean bean : beans) {
		Class<?> beanType = bean.getBeanType();
		if (beanType != null) {
			Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
			if (!attrMethods.isEmpty()) {
				this.modelAttributeAdviceCache.put(bean, attrMethods);
			}
			Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
			if (!binderMethods.isEmpty()) {
				this.initBinderAdviceCache.put(bean, binderMethods);
			}
			ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
			if (resolver.hasExceptionMappings()) {
				this.exceptionHandlerAdviceCache.put(bean, resolver);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		if (modelSize == 0 && binderSize == 0 && handlerSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + handlerSize + " @ExceptionHandler");
		}
	}
}
 
Example 3
Source File: ExceptionHandlerExceptionResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(adviceBean);
		}
	}

	if (logger.isDebugEnabled()) {
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		int adviceSize = this.responseBodyAdvice.size();
		if (handlerSize == 0 && adviceSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " +
					handlerSize + " @ExceptionHandler, " + adviceSize + " ResponseBodyAdvice");
		}
	}
}
 
Example 4
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void initControllerAdviceCache() {
	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Looking for @MessageExceptionHandler mappings: " + context);
	}
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context);
	AnnotationAwareOrderComparator.sort(beans);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans));
}
 
Example 5
Source File: ControllerMethodResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initControllerAdviceCaches(ApplicationContext applicationContext) {
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
	AnnotationAwareOrderComparator.sort(beans);

	for (ControllerAdviceBean bean : beans) {
		Class<?> beanType = bean.getBeanType();
		if (beanType != null) {
			Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
			if (!attrMethods.isEmpty()) {
				this.modelAttributeAdviceCache.put(bean, attrMethods);
			}
			Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
			if (!binderMethods.isEmpty()) {
				this.initBinderAdviceCache.put(bean, binderMethods);
			}
			ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
			if (resolver.hasExceptionMappings()) {
				this.exceptionHandlerAdviceCache.put(bean, resolver);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		if (modelSize == 0 && binderSize == 0 && handlerSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + handlerSize + " @ExceptionHandler");
		}
	}
}
 
Example 6
Source File: ExceptionHandlerExceptionResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			this.responseBodyAdvice.add(adviceBean);
		}
	}

	if (logger.isDebugEnabled()) {
		int handlerSize = this.exceptionHandlerAdviceCache.size();
		int adviceSize = this.responseBodyAdvice.size();
		if (handlerSize == 0 && adviceSize == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " +
					handlerSize + " @ExceptionHandler, " + adviceSize + " ResponseBodyAdvice");
		}
	}
}
 
Example 7
Source File: WebSocketAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void initControllerAdviceCache() {
	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Looking for @MessageExceptionHandler mappings: " + context);
	}
	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context);
	AnnotationAwareOrderComparator.sort(beans);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans));
}
 
Example 8
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void initExceptionHandlerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for exception mappings: " + getApplicationContext());
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType());
		if (resolver.hasExceptionMappings()) {
			this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @ExceptionHandler methods in " + adviceBean);
			}
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(adviceBean.getBeanType())) {
			this.responseBodyAdvice.add(adviceBean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean);
			}
		}
	}
}
 
Example 9
Source File: WebSocketAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for @MessageExceptionHandler mappings: " + getApplicationContext());
	}
	List<ControllerAdviceBean> controllerAdvice = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(controllerAdvice);
	initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(controllerAdvice));
}
 
Example 10
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	List<Object> requestResponseBodyAdviceBeans = new ArrayList<>();

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
		if (!attrMethods.isEmpty()) {
			this.modelAttributeAdviceCache.put(adviceBean, attrMethods);
		}
		Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
		if (!binderMethods.isEmpty()) {
			this.initBinderAdviceCache.put(adviceBean, binderMethods);
		}
		if (RequestBodyAdvice.class.isAssignableFrom(beanType) || ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			requestResponseBodyAdviceBeans.add(adviceBean);
		}
	}

	if (!requestResponseBodyAdviceBeans.isEmpty()) {
		this.requestResponseBodyAdvice.addAll(0, requestResponseBodyAdviceBeans);
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int reqCount = getBodyAdviceCount(RequestBodyAdvice.class);
		int resCount = getBodyAdviceCount(ResponseBodyAdvice.class);
		if (modelSize == 0 && binderSize == 0 && reqCount == 0 && resCount == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + reqCount + " RequestBodyAdvice, " + resCount + " ResponseBodyAdvice");
		}
	}
}
 
Example 11
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}

	List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(adviceBeans);

	List<Object> requestResponseBodyAdviceBeans = new ArrayList<>();

	for (ControllerAdviceBean adviceBean : adviceBeans) {
		Class<?> beanType = adviceBean.getBeanType();
		if (beanType == null) {
			throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
		}
		Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
		if (!attrMethods.isEmpty()) {
			this.modelAttributeAdviceCache.put(adviceBean, attrMethods);
		}
		Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
		if (!binderMethods.isEmpty()) {
			this.initBinderAdviceCache.put(adviceBean, binderMethods);
		}
		if (RequestBodyAdvice.class.isAssignableFrom(beanType)) {
			requestResponseBodyAdviceBeans.add(adviceBean);
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
			requestResponseBodyAdviceBeans.add(adviceBean);
		}
	}

	if (!requestResponseBodyAdviceBeans.isEmpty()) {
		this.requestResponseBodyAdvice.addAll(0, requestResponseBodyAdviceBeans);
	}

	if (logger.isDebugEnabled()) {
		int modelSize = this.modelAttributeAdviceCache.size();
		int binderSize = this.initBinderAdviceCache.size();
		int reqCount = getBodyAdviceCount(RequestBodyAdvice.class);
		int resCount = getBodyAdviceCount(ResponseBodyAdvice.class);
		if (modelSize == 0 && binderSize == 0 && reqCount == 0 && resCount == 0) {
			logger.debug("ControllerAdvice beans: none");
		}
		else {
			logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize +
					" @InitBinder, " + reqCount + " RequestBodyAdvice, " + resCount + " ResponseBodyAdvice");
		}
	}
}
 
Example 12
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isInfoEnabled()) {
		logger.info("Looking for @ControllerAdvice: " + getApplicationContext());
	}

	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(beans);

	List<Object> requestResponseBodyAdviceBeans = new ArrayList<Object>();

	for (ControllerAdviceBean bean : beans) {
		Set<Method> attrMethods = MethodIntrospector.selectMethods(bean.getBeanType(), MODEL_ATTRIBUTE_METHODS);
		if (!attrMethods.isEmpty()) {
			this.modelAttributeAdviceCache.put(bean, attrMethods);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @ModelAttribute methods in " + bean);
			}
		}
		Set<Method> binderMethods = MethodIntrospector.selectMethods(bean.getBeanType(), INIT_BINDER_METHODS);
		if (!binderMethods.isEmpty()) {
			this.initBinderAdviceCache.put(bean, binderMethods);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @InitBinder methods in " + bean);
			}
		}
		if (RequestBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
			requestResponseBodyAdviceBeans.add(bean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected RequestBodyAdvice bean in " + bean);
			}
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
			requestResponseBodyAdviceBeans.add(bean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected ResponseBodyAdvice bean in " + bean);
			}
		}
	}

	if (!requestResponseBodyAdviceBeans.isEmpty()) {
		this.requestResponseBodyAdvice.addAll(0, requestResponseBodyAdviceBeans);
	}
}
 
Example 13
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void initControllerAdviceCache() {
	if (getApplicationContext() == null) {
		return;
	}
	if (logger.isInfoEnabled()) {
		logger.info("Looking for @ControllerAdvice: " + getApplicationContext());
	}

	List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
	AnnotationAwareOrderComparator.sort(beans);

	List<Object> requestResponseBodyAdviceBeans = new ArrayList<Object>();

	for (ControllerAdviceBean bean : beans) {
		Set<Method> attrMethods = MethodIntrospector.selectMethods(bean.getBeanType(), MODEL_ATTRIBUTE_METHODS);
		if (!attrMethods.isEmpty()) {
			this.modelAttributeAdviceCache.put(bean, attrMethods);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @ModelAttribute methods in " + bean);
			}
		}
		Set<Method> binderMethods = MethodIntrospector.selectMethods(bean.getBeanType(), INIT_BINDER_METHODS);
		if (!binderMethods.isEmpty()) {
			this.initBinderAdviceCache.put(bean, binderMethods);
			if (logger.isInfoEnabled()) {
				logger.info("Detected @InitBinder methods in " + bean);
			}
		}
		if (RequestBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
			requestResponseBodyAdviceBeans.add(bean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected RequestBodyAdvice bean in " + bean);
			}
		}
		if (ResponseBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
			requestResponseBodyAdviceBeans.add(bean);
			if (logger.isInfoEnabled()) {
				logger.info("Detected ResponseBodyAdvice bean in " + bean);
			}
		}
	}

	if (!requestResponseBodyAdviceBeans.isEmpty()) {
		this.requestResponseBodyAdvice.addAll(0, requestResponseBodyAdviceBeans);
	}
}