2017-05-26 50 views
0

我遇到了匹配bean的問題。我的項目是基於該項目由 「春天行動」Spring沒有匹配bean的類型...找到依賴項:預計至少有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: {} 

和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> 

public interface BoardRepository { 

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

Board findOne(long boardId); 
} 

而且

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

控制器

@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; 
} 

我已經嘗試用不同的方式加入adnotation,但錯誤總是相同的。

+1

BoardRepository的具體實現在哪裏?如果你有它,它是否有彈簧註釋(如@Component或@Repository)? – tsolakp

+0

我試圖讓它像書中一樣,它並沒有對這個接口做任何實現。另一方面,當我在Board類中使用註釋實現BoardRepostiry時,錯誤是一樣的。 – kkm

回答

0

我會改變的回購協議:

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

和註釋聲明:

@Autowired 
private BoardRepository boardRepository; 

而且讓Spring知道去哪裏找:

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

(如果BoardRepository定義在com.repositories包中)

+0

之後,我有另一個錯誤 org.springframework.beans.factory.BeanDefinitionStoreException:意外的異常從ServletContext資源解析XML文檔[/WEB-INF/spring/appServlet/servlet-context.xml];嵌套異常是java.lang.NoSuchMethodError:org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg/springframework/core/env/Environment; 我通過向依賴添加spring-beans解決了這個問題,但是當我嘗試在服務器上運行項目時,我只有404。 – kkm

0

我還沒有讀過這本書,但它看起來像春天的數據, for example。 他們還創建接口,而無需實現。

相關問題