java - 为什么 Spring Web 服务中 Autowiring 依赖项的注入(inject)失败?

标签 java spring spring-mvc

我开始在一个简单的 Spring Web 服务中工作,并收到下面提供的以下错误,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogEntryController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private core.services.BlogEntryService
rest.mvc.BlogEntryController.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [core.services.BlogEntryService] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

项目结构如下,

Spring MVC project structure

我有以下服务代码,

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 代码,

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

    }
 }

更新:Dispatcher 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 文件。我现在应该怎么做 ?

最佳答案

正如我在您的问题下的第一条评论中所述,您缺少接口(interface)的实现类。实现类可以是下面的类,但是您必须提供方法的功能:

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

关于java - 为什么 Spring Web 服务中 Autowiring 依赖项的注入(inject)失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39742396/

相关文章:

java - Spring singleton bean 在被代理时在每个方法调用上创建新的实例/bean

spring - 在 Intellij 中运行 Spring MVC 程序

java - Thymeleaf 模板中的迭代不起作用

spring - SimpleUrlHandlerMapping 不适用于带有少量扩展名(如 dsm 或 ds)的 url

java - 灵活的 Swing 输入对话框

java - JSP生成Excel电子表格(XLS)下载

java - 如何让 Spring MVC 在 JUnit 测试中调用验证?

java - Swing 应用程序框架 @Action 注释?做什么的?

java - 在 netbeans 中的尖括号之间拆分传入数据

java - 在 Spring v1.5.14.RELEASE 中模拟返回 List 的 RestTemplate