본문 바로가기

Programing/Framework

[spring] stream 사용 예, 하지만 없어졌지...

ControllerAdviceBean#findAnnotatedBeans

~v5.0.9.RELEASE

v5.0.9.RELEASE 까지는 enhanced for 로 구성이 되어 있었다.

package org.springframework.web.method;

public class ControllerAdviceBean implements Ordered {
	// ...
	/**
	 * Find the names of beans annotated with
	 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
	 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
	 */
	public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
		List<ControllerAdviceBean> beans = new ArrayList<>();
		for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
			if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
				beans.add(new ControllerAdviceBean(name, applicationContext));
			}
		}
		return beans;
	}

v5.1.0.RELEASE

그러더니 5.1.0부터 stream으로 바뀌었다.

커미터: Rossen Stoyanchev at 2018-06-07

  [SPR-16336] Extract HandlerTypePredicate from ControllerAdviceBean

public class ControllerAdviceBean implements Ordered {
	// ...
	public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
		return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class))
				.filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null)
				.map(name -> new ControllerAdviceBean(name, context))
				.collect(Collectors.toList());
	}

v5.2.0.RELEASE

하지만 2019년 6월 21일 Sam Brannen에 의해 다시 바뀌었다는...


커밋 로그가 "Avoid use of Stream API in ControllerAdviceBean" 이다.

public class ControllerAdviceBean implements Ordered {
	// ..
	public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
		List<ControllerAdviceBean> adviceBeans = new ArrayList<>();
		for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) {
			ControllerAdvice controllerAdvice = context.findAnnotationOnBean(name, ControllerAdvice.class);
			if (controllerAdvice != null) {
				// Use the @ControllerAdvice annotation found by findAnnotationOnBean()
				// in order to avoid a subsequent lookup of the same annotation.
				adviceBeans.add(new ControllerAdviceBean(name, context, controllerAdvice));
			}
		}
		OrderComparator.sort(adviceBeans);
		return adviceBeans;
	}

피보탈도 스트림 API를 지양하고 있다는 느낌을 받았다.