2017-05-27 60 views
0

我使用的IntelliJ終極版+ Tomcat中,我試圖做一個小型Web應用程序,但我遇到了這個錯誤,當我啓動Tomcat服務器:Web應用程序使用Spring找不到豆

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); 
} 
} 

我知道這是一個豆腐的問題,但我已經自動裝配的一切,我不知道哪裏出了問題可能是。這是我的第一個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未找到的東西。

回答

4

添加

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

我不知道定義的應用程序上下文文件,其中加這個?我有3個文件,application.properties(我有DB憑據),我的-servlet.xml和我的web.xml。我試圖在每個人中加入它們,但他們都不知道jpa是什麼意思? – Gimv13

+0

將它添加到'servlet.xml'並在'beans'部分中定義'jpa'命名空間 – Reimeus

+0

完成它,但現在我得到另一個錯誤:org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'entityManagerFactory'的bean可用 – Gimv13

相關問題