java - "required a single bean, but 2 were found"- Spring

标签 java spring javabeans

这是我的类(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);
      }
}

在我的项目中,我正在使用基于 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

我该如何解决?

最佳答案

您正在使用构造函数注入(inject)和字段注入(inject)。

public DefaultExceptionHandler(MessageSourceAccessor messageSource) {
    Assert.notNull(messageSource, "messageSource must not be null");
    this.messageSource = messageSource;
 }

   @Autowired
   private final MessageSourceAccessor messageSource;

只选一个。

关于java - "required a single bean, but 2 were found"- Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45162150/

相关文章:

java - 我的 Euclid 算法运行非常缓慢

java - 如何在表单提交后显示消息并具有幂等性

java - 由于双重上下文 (servlet+ContextLoaderListener),所有 Spring Framework bean 都会重复

java - 向 spring Rest api 发送 post 请求

java - Spring Beans Mule - 接口(interface)/类类型的属性

java - REST 方法 - 在其他方法之前执行 "common"方法调用

java - 使用自定义类对象填充线性布局

java - Transactional(readOnly=false) 仅在从 Servlet 调用时起作用

java - 创建自定义 RestTemplate 时 Spring-boot 出错

java - 带有 addXX 方法的 BeanUtils