使用Spring的WebApp找不到bean

时间:2017-05-27 17:56:44

标签: java spring spring-mvc tomcat8

我正在使用Intellij Ultimate Edition + Tomcat,我尝试制作一个小型Web应用程序,但是当我启动tomcat服务器时遇到了这个错误:

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ticketServiceImpl': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.testing.repositories.TicketRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.testing.repositories.TicketRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这是我的代码:

控制器:

package com.testing.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping
public String showIndex(){

    return "index";

}
}

型号:

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="tickets")
public class Ticket implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;

@Column(nullable = false)
private Long event_id;

@Column(length = 50, nullable = false)
private String type;

@Column(nullable = false)
private Long price;

@Column(nullable = false)
private Long ticketsleft;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public Long getEvent_id() {
    return event_id;
}

public void setEvent_id(Long event_id) {
    this.event_id = event_id;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Long getPrice() {
    return price;
}

public void setPrice(Long price) {
    this.price = price;
}

public Long getTicketsleft() {
    return ticketsleft;
}

public void setTicketsleft(Long ticketsleft) {
    this.ticketsleft = ticketsleft;
}
}

存储库:

package com.testing.repositories;

import com.testing.models.Ticket;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TicketRepository extends JpaRepository<Ticket,Long> {

}

服务:

package com.testing.services;


import com.testing.models.Ticket;

public interface TicketService extends CrudService<Ticket>{

}


//////

import com.testing.models.Ticket;
import com.testing.repositories.TicketRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TicketServiceImpl implements TicketService {

@Autowired
private TicketRepository repository;

@Override
public Ticket save(Ticket entity) {
    return this.repository.save(entity);
}

@Override
public List<Ticket> getAll() {
    return this.repository.findAll();
}

@Override
public Ticket getById(Long id) {
    return this.repository.findOne(id);
}

@Override
public void delete(Long id) {
    this.repository.delete(id);
}
}

我知道这是一个bean问题,但我已经自动装配了一切,我不知道问题出在哪里。这是我的第一个Web应用程序,我想这可能是一个愚蠢的事情。无论如何,这是我的servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.testing" />
<jpa:repositories base-package="com.testing.repositories" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>


<!-- Step 5: Define Spring MVC view resolver -->
<bean

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

</beans>

我是否必须向servlet.xml文件添加任何内容?组件扫描似乎很好

后来的编辑:添加了jpa,但现在我得到了同样的错误和更多,有关entitymanagerfactory未找到的内容。

1 个答案:

答案 0 :(得分:4)

添加

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

jpa标记

中定义beans的应用程序上下文文件
<beans 
...
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
...
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
...