我开始在一个简单的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(所需=真)}
项目结构如下,
我有以下服务代码,
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
文件。我现在应该怎么做 ?
您的BlogEntryService实现类在哪里? – pleft
你应该用'@ Service'注释来注释你的服务。 – dty
@elefasGR我需要实现的类或接口应该工作正常吗? – Chaklader