2017-07-18 2 views
2

Hier ist meine Klasse:"erforderlich, um eine einzelne Bohne, aber 2 wurden gefunden" - Frühling

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 meinem Projekt, ich bin mit Feder-boot/Hibernate/java-basierte Konfiguration und zu versuchen, Implementieren Sie einen Ausnahmebehandlungsmechanismus. Ich verwende einen Beispielcode, den ich von StackOverflow erhalten habe, und lerne, wie man im Frühjahr am besten mit Ausnahmen umgeht. Aber wenn ich den Code ausführen, erhalte ich diesen Fehler. Aber als ich aufhörte, "MessageSourceAccessor" zu verwenden, verschwindet der Fehler.

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 

Wie kann ich das beheben?

+0

@ MickaëlB wo mache ich das? Da ich diese Bohnen nicht kreiert habe, wie gehe ich damit um? – Harshakj89

Antwort

4

Sie verwenden sowohl Konstruktorinjektion als auch Feldinjektion.

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

und

@Autowired 
    private final MessageSourceAccessor messageSource; 

Wählen Sie nur eine.

+0

Ich hielt @Autowired private letzte MessageSourceAccessor messageSource; und entfernte den Konstruktor. Aber dann sagt Eclipse "Die leere letzte Feld messageSource wurde möglicherweise nicht initialisiert" – Harshakj89

+0

da Sie '@ ControllerAdvice' verwenden gibt es keine Notwendigkeit, erneut Konstruktor –

+0

In meinem Fall musste ich eine' @ Bean' Annotation entfernen. –

Verwandte Themen