2016-09-28 26 views
0

我开始在一个简单的Spring Web服务工作,并得到下面提供的下列错误,为什么在Spring Web服务中注入自动装载的依赖关系失败?

org.springframework.beans.factory.BeanCreationException: 错误创建名为“blogEntryController”豆: 注射液自动装配依赖性失败;嵌套异常 is org.springframework.beans.factory.BeanCreationException: 无法自动装入字段:private core.services.BlogEntryService
rest.mvc.BlogEntryController.service;嵌套的异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 无类型的排位豆[core.services.BlogEntryService]找到
依赖性:预期至少1豆其 资格作为自动装配候选这种依赖性。依赖 注解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

项目结构如下,

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

+0

您的BlogEntryService实现类在哪里? – pleft

+0

你应该用'@ Service'注释来注释你的服务。 – dty

+0

@elefasGR我需要实现的类或接口应该工作正常吗? – Chaklader

回答

1
@Autowired 
private BlogEntryService service; 

上述各行表示类BlogEntryService注释 与@Service或豆标签或调度员的servlet XML作为组分扫描(包)中提到您有。

如果缺少任何一个地方的提你会得到异常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" /> 
+0

我只是使用'@ Service'注释,这仍然不起作用。 – Chaklader

+0

让你的调度员servlet.xml –

+0

@Chaklader检查更新的答案 –

0

它看起来没有提供BlogEntryService的bean类。你有一些实现类注释机智@Service?

+0

我没有其他实现类用@Service注释。我怎样才能提供'BlogEntryService'的bean类? – Chaklader

0

无论您的界面提供了一个实现类,并与@Service注释,或使BlogEntryService接口扩展CrudRepository和Spring会为您提供为您cruds正确执行:保存,GETALL,发现...

1

正如我在我的第一个评论下面你的问题所述,你错过了接口的实现类。实现类可以是下面的一个,但是您必须提供方法的功能:

@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 
    } 
} 
相关问题