2017-05-27 1 views
0

J'utilise Intellij Ultimate Edition + Tomcat, je l'ai essayé de faire une petite application web, mais je tombe sur cette erreur lorsque je lance le serveur tomcat:WebApp avec Spring ne peut pas trouver le haricot

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

et voici mon code:

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"; 

} 
} 

Modèle:

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

Repository:

package com.testing.repositories; 

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

public interface TicketRepository extends JpaRepository<Ticket,Long> { 

} 

Services:

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

Je sais qu'il est un problème de haricot, mais je l'ai autowired tout, je ne sais pas où le problème pourrait être. C'est ma première application web et je suppose que c'est peut-être une chose stupide. Quoi qu'il en soit, voici mon fichier 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> 

Dois-je ajouter quelque chose à mon fichier servlet.xml? La numérisation des composants semble bien

Modification ultérieure: Ajout du jpa mais maintenant je reçois la même erreur et plus, quelque chose à propos de entitymanagerfactory introuvable.

Répondre

4

Ajouter

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

dans le fichier contexte d'application où jpa est défini dans la balise 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 
... 
+0

Je ne sais pas où ajouter cela? J'ai 3 fichiers, application.properties (j'ai les informations d'identification DB), mon -servlet.xml et mon web.xml. J'ai essayé de l'ajouter dans chacun d'entre eux, mais aucun d'entre eux ne semble savoir ce que signifie jpa? – Gimv13

+0

Ajoutez-le à 'servlet.xml' et définissez l'espace de noms' jpa' dans la section 'beans' – Reimeus

+0

Terminé, mais maintenant je reçois une autre erreur: org.springframework.beans.factory.NoSuchBeanDefinitionException: Aucun bean nommé 'entityManagerFactory' disponible – Gimv13