2017-07-18 51 views
2

這裏是我的類:「需要一個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); 
     } 
} 

在我的項目,我使用的是彈簧啓動/休眠/基於Java的配置,並試圖實現一個異常處理機制。我使用從StackOverflow得到的示例代碼,並瞭解在春季處理異常的最佳方法。 但是當我運行代碼時,我得到這個錯誤。但是當我停止使用「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 

我該如何解決?

+0

@MickaëlB我該怎麼做?因爲我沒有創建這些bean,我該如何處理? – Harshakj89

回答

4

您正在使用構造函數注入和字段注入。

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

@Autowired 
    private final MessageSourceAccessor messageSource; 

只選擇一個。

+0

我保留@Autowired私人最終MessageSourceAccessor messageSource;並刪除了構造函數。但是然後eclipse說「空白的final字段messageSource可能沒有被初始化」 – Harshakj89

+0

,因爲你使用的是@ ControllerAdvice,所以不需要再添加構造函數 –

+0

在我的情況下,我需要刪除一個'@Bean'註解。 –

相關問題