java - 使用Spring的WebApp找不到bean

标签 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)}

这是我的代码:

Controller :

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 问题,但我已经 Autowiring 了所有内容,我不知道问题可能出在哪里。这是我的第一个网络应用程序,我想这可能是一件愚蠢的事情。无论如何,这是我的 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的内容。

最佳答案

添加

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

到应用程序上下文文件,其中 jpabeans 标记中定义

<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
...

关于java - 使用Spring的WebApp找不到bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220037/

相关文章:

java - 从 java swing 导出和导入数据库 mysql

java - jedis llen 结果不等于 redis llen

java - GWT 多模块导致 '$wnd.alert is not a function'

java - 如何使用 Spring webflux 流式传输固定列表?

java - Spring 将单例 bean 注入(inject)原型(prototype) bean 导致单例重新创建

java - 使用 Spring Boot 2 WebClient 从响应中获取 header

javascript - 使用 Selenium Webdriver 的 ExtJS 测试自动化

java - map 的序列化

Spring Boot 注解 @GetMapping 无法解析为类型

Java Spring注解错误