2017-05-26 2 views
0

J'ai des problèmes avec le haricot correspondant. Mon projet est basé sur le projet de "printemps en action"Spring Aucun bean de type correspondant trouvé pour la dépendance: attendu au moins 1 bean

No matching bean of type [com.kmazur.data.BoardRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'boardController' defined in file [C:\Users\lenovo\Documents\workspace-sts-3.8.4.RELEASE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MVCWebApi\WEB-INF\classes\com\kmazur\web\BoardController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.kmazur.data.BoardRepository]: : No matching bean of type [com.kmazur.data.BoardRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.kmazur.data.BoardRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

Et servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory --> 
<beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 



<context:component-scan base-package="com.kmazur.*" /> 

web.xml

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

Classes

public interface BoardRepository { 

List<Board> findUsers(long max, int count); 

Board findOne(long boardId); 
} 

Et

public class Board { 
private final Long id; 
private final String message; 
private final Date time; 
private Double latitude; 
private Double longitude; 

Contrôleur

@Controller 
@RequestMapping("/boards") 
public class BoardController { 

private static final String MAX_LONG_AS_STRING = "9223372036854775807"; 


private BoardRepository boardRepository; 


@Autowired 
public BoardController(BoardRepository boardRepository) { 
    this.boardRepository = boardRepository; 
} 

Je l'ai déjà essayé d'ajouter adnotation de différentes manières, mais l'erreur était toujours le même.

+1

Où est votre implémentation concrète de BoardRepository? Et si vous en avez un, a-t-il des annotations de printemps dessus (comme @Component ou @Repository)? – tsolakp

+0

J'essaie de faire comme dans le livre, qui ne fait aucune implémentation de cette interface. D'un autre côté quand j'implémente BoardRepostiry dans la classe Board avec l'annotation, l'erreur est la même. – kkm

Répondre

0

Je changerais le repo à:

public interface BoardRepository extends JpaRepository<Board,Long>{ 
//Methods here 
} 

et annoter la déclaration:

@Autowired 
private BoardRepository boardRepository; 

Et laissez printemps savoir où chercher:

<jpa:repositories base-package="com.repositories" /> 

(Si BoardRepository a été défini dans le paquet com.repositories)

+0

Après avoir fait que j'ai une autre erreur org.springframework.beans.factory.BeanDefinitionStoreException: Exception inattendue analysant le document XML à partir de la ressource ServletContext [/WEB-INF/spring/appServlet/servlet-context.xml]; l'exception imbriquée est java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment() Lorg/springframework/core/env/Environnement; J'ai résolu ceci en ajoutant des haricots de printemps aux dépendances mais alors j'ai seulement 404 quand j'essaye d'exécuter le projet sur le serveur. – kkm

0

Je n'ai pas lu ce livre, mais il ressemble à des données de printemps, for example. Ils créent également une interface sans implémentation.