“需要一个bean,但是找到2个" - 春天 [英] "required a single bean, but 2 were found" - Spring
本文介绍了“需要一个bean,但是找到2个" - 春天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的课程:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.mportal.ec.exception.ApplicationSpecificException;
@ControllerAdvice
public class DefaultExceptionHandler extends ResponseEntityExceptionHandler {
@Autowired
private final MessageSourceAccessor messageSource;
public DefaultExceptionHandler(MessageSourceAccessor messageSource) {
Assert.notNull(messageSource, "messageSource must not be null");
this.messageSource = messageSource;
}
//expected Exceptions
@ExceptionHandler(ApplicationSpecificException.class)
protected ResponseEntity<Object> handleApplicationSpecificException(final RuntimeException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}
//unexpected Exceptions
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleException(final RuntimeException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}
在我的项目中,我正在使用基于spring-boot/hibernate/java的配置并尝试实现异常处理机制.我正在使用从StackOverflow获得的示例代码,并了解处理异常的最佳方法是春天. 但是,当我运行代码时,会出现此错误.但是当我停止使用" MessageSourceAccessor "时,错误消失了.
In my project, I'm using spring-boot/hibernate/ java based configuration and trying to implement an exception handling mechanism.I'm using an example code i got from StackOverflow and learn what's the best way to handle exceptions in spring. But when I run the code, I get this error. But when I stopped using "MessageSourceAccessor" the error goes away.
Description:
Parameter 0 of constructor in com.mportal.ec.error.DefaultExceptionHandler required a single bean, but 2 were found:
- linkRelationMessageSource: defined by method 'linkRelationMessageSource' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
- resourceDescriptionMessageSourceAccessor: defined by method 'resourceDescriptionMessageSourceAccessor' in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.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
我该如何解决?
推荐答案
您正在同时使用构造函数注入和字段注入.
You are using both constructor injection and field injection.
public DefaultExceptionHandler(MessageSourceAccessor messageSource) {
Assert.notNull(messageSource, "messageSource must not be null");
this.messageSource = messageSource;
}
和
@Autowired
private final MessageSourceAccessor messageSource;
仅选择一个.
这篇关于“需要一个bean,但是找到2个" - 春天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文