웹개발/Micro Service Architecture

[에러]Parameter N of method A in B Class required a single bean, but M were found

수제개발자 2019. 4. 15. 16:18

원인

어떤 클래스의 어떤 메소드중 몇번째 파라미터에서 빈이 1개가 필요한데 여러개가 발견되었다고 하는 메세지.

보통 코드를 잘못짜서 메소드가 어떤 인스턴스를 써야할지 모호함이 발생할때 나는 에러이다.

@Autowired를 해놓고 또 파라미터로 받고 있다든지...

내가 코드에서 그렇다면 수정하면될텐데 스프링 프레임워크의 메소드에서 내가 수정할수 있는 빈과 그렇지 않은 빈 중에 모호함이 발생해서 나는 에러였다.

 

에러메세지

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method BindingHandlerAdvise in org.springframework.cloud.stream.config.BindingServiceConfiguration required a single bean, 
but 2 were found:
	- getValidator: defined by method 'getValidator' in class path resource [com/uracle/hecate/auth/core/configuration/MessageSourceConfig.class]
	- mvcValidator: defined by method 'mvcValidator' in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

 

 

해결

이름도 완전 다른데 Validator부분이 비슷해서 모호하다는건지.. 왜인지는 정확히 모르겠지만, 내가 수정할수 있는 빈을 갖다쓰면 안될 상황이기에 그걸 쓰지 말라고 조치해줘야한다. @lazy도 있고 뭐 @Primary도 있고. 근데 @Primary를 붙여버리면 이걸 갖다쓰라는 거니까 의도랑 반대가 된다.

그래서 내가 수정할 수 있는 빈을 구체화 해줬다.

빈 네임을 "validator"라고 지었다. 

그랬더니 모호함이 제거 되었다.

 

@Configuration
public class MessageSourceConfig {
	
	@Bean
	public MessageSource messageSource() {
	    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
	     
	    //messageSource.setBasename("classpath:message");
	    messageSource.setBasename("http://localhost:8999/message.properties");
	    messageSource.setDefaultEncoding("UTF-8");
	    
	    return messageSource;
	}
	
	@Bean(name="validator")
	public LocalValidatorFactoryBean getValidator() {
	    LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
	    bean.setValidationMessageSource(messageSource());
	    return bean;
	}
	
}