【问题标题】:WebApp using Spring can't find bean使用Spring的WebApp找不到bean
【发布时间】:2017-10-28 10:29:57
【问题描述】:

我正在使用 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 的东西没有找到。

【问题讨论】:

    标签: java spring spring-mvc tomcat8


    【解决方案1】:

    添加

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

    【讨论】:

    • 我不确定,在哪里添加这个?我有 3 个文件,application.properties(我有数据库凭据)、我的 -servlet.xml 和我的 web.xml。我尝试在每个人中添加它,但他们似乎都不知道 jpa 是什么意思?
    • 将其添加到 servlet.xml 并在 beans 部分中定义 jpa 命名空间
    • 完成了,但现在我得到另一个错误:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    • 您需要按照this post中的描述定义一个EntityManager bean
    • 非常感谢,它就像一个魅力!同时,我还添加了一个控制器并定义了 transactionManager bean。您知道在此类项目中必须使用的其他 bean 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 2022-11-10
    • 2017-12-13
    • 2018-12-13
    • 2018-10-01
    相关资源
    最近更新 更多