java “需要一个 bean,但找到了 2 个”- Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45162150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"required a single bean, but 2 were found" - Spring
提问by Harsha Jayamanna
Here is my class:
这是我的课:
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);
}
}
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.
在我的项目中,我正在使用基于 spring-boot/hibernate/java 的配置并尝试实现异常处理机制。我正在使用从 StackOverflow 获得的示例代码,并了解在 spring 中处理异常的最佳方法是什么。但是当我运行代码时,我收到了这个错误。但是当我停止使用“ MessageSourceAccessor”时,错误消失了。
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
How do I resolve this?
我该如何解决?
回答by Anupama Boorlagadda
You are using both constructor injection and field injection.
您同时使用构造函数注入和字段注入。
public DefaultExceptionHandler(MessageSourceAccessor messageSource) {
Assert.notNull(messageSource, "messageSource must not be null");
this.messageSource = messageSource;
}
and
和
@Autowired
private final MessageSourceAccessor messageSource;
Choose only one.
只选择一项。