簡體   English   中英

為什么在Spring Web服務中注入自動關聯依賴項失敗?

[英]Why does the Injection of autowired dependencies failed in Spring web-service?

我開始在一個簡單的Spring網絡服務中工作,並在下面收到以下錯誤消息,

org.springframework.beans.factory.BeanCreationException:創建名稱為'blogEntryController'的bean時出錯:自動連接依賴項的注入失敗; 嵌套的異常是org.springframework.beans.factory.BeanCreationException:無法自動連線字段:私有core.services.BlogEntryService
rest.mvc.BlogEntryController.service; 嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到類型為[core.services.BlogEntryService]的合格Bean。
依賴關系:期望至少有1個bean符合此依賴關系的自動裝配候選條件。 依賴項注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

項目結構如下

Spring MVC項目結構

我有以下服務代碼,

package core.services;

import core.entities.BlogEntry;

public interface BlogEntryService {

    public BlogEntry find(Long id); // Returns the BlogEntry or null if it can't be found
    public BlogEntry delete(Long id); // Deletes the found BlogEntry or returns null if it can't be found
    public BlogEntry update(Long id, BlogEntry data);
}

以及以下控制器代碼,

@Controller
@RequestMapping("/rest/blog-entries")
public class BlogEntryController {

    @RequestMapping("/")
    public String test(){

        return  "view";
    }

    public BlogEntryController() {

    }

    @Autowired
    private BlogEntryService service;

    public BlogEntryController(BlogEntryService service)
    {
        this.service = service;
    }

    @RequestMapping(value="/{blogEntryId}",
            method = RequestMethod.GET)
    public ResponseEntity<BlogEntryResource> getBlogEntry(
            @PathVariable Long blogEntryId) {
        BlogEntry entry = service.find(blogEntryId);
        if(entry != null)
        {
            BlogEntryResource res = new BlogEntryResourceAsm().toResource(entry);
            return new ResponseEntity<BlogEntryResource>(res, HttpStatus.OK);
        } else {
            return new ResponseEntity<BlogEntryResource>(HttpStatus.NOT_FOUND);
        }

    }
 }

更新:分派器Servlet.xml

   <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="rest.mvc"/>

    <mvc:annotation-driven/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我創建了一個空結構,如之前調試器所要求的。 我什至看不到測試方法返回的view.jsp文件。 我現在應該怎么辦 ?

@Autowired
 private BlogEntryService service;

上面幾行說,您正在用@Service注釋類BlogEntryService ,或者在bean標簽中提到了該類,或者在dispatcher-servlet xml中將其作為組件scan(package)。

如果您在任何一個地方都缺少提及,那么您將獲得異常No qualifying bean of type [core.services.BlogEntryService] found for dependency:

因此,您的BlogEntryService接口應為

@Service
public interface BlogEntryService {

更新:在dispathcer-servlet.xml中,您必須提到用於掃描bean的軟件包。在您的情況下是rest.mvc 。由於它是一個接口,因此您應該具有已實現的類 (以@Service注釋), 該類將具有接口方法。

<context:component-scan base-package="Your Service layer Package" />

正如我在問題下方的第一條評論中所述,您缺少接口的實現類。 一個實現類可以是下面的一個,但是您必須提供方法的功能:

@Service
public class BlogEntryServiceImpl implements BlogEntryService {
    public BlogEntry find(Long id) {
        //Do your stuff here
    }
    public BlogEntry delete(Long id) {
        //Do your stuff here
    }
    public BlogEntry update(Long id, BlogEntry data) {
        //Do your stuff here
    }
}

似乎未提供BlogEntryService Bean類。 您是否有一些注釋為@Service的實現類?

您可以為接口提供一個實現類並使用@service對其進行注釋,或者使BlogEntryService接口擴展CrudRepository,Spring會為您的crud提供適當的實現:save,getAll,find ...

似乎缺少服務的實現。 僅僅使用@Service注釋接口不足以使Spring生成Bean,除非您使用某些特定的Spring Data接口(CrudRepository,Repository ...)擴展了接口。 在這種特定情況下,如果您遵守約定,那么Spring將能夠自動為您生成實現。 請查看文檔: http : //docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM