0

I am using Intellij Ultimate Edition + Tomcat , I've tried to make a small web application but I run into this error when I start the tomcat server:

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

And this is my 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";

}
}

Model:

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

I know it's a bean problem, but I have autowired everything, I don't know where the problem might be. This is my first web application and I guess it might be a stupid thing. Anyways, here is my servlet.xml file:

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

Do I have to add anything to my servlet.xml file ? The component scanning seems fine

Later Edit: Added the jpa but now I get the same error and more, something about entitymanagerfactory not found.

Gimv13
  • 153
  • 2
  • 3
  • 15

1 Answers1

4

Add

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

to the application context file where jpa is defined in the beans tag

<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
...
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I'm not sure, where to add this? I have 3 files, application.properties( I have the DB credentials ) , my -servlet.xml and my web.xml . I tried adding it in each of them but none of them seem to know what jpa means ? – Gimv13 May 27 '17 at 18:10
  • Add it to `servlet.xml` plus define the `jpa` namespace in the `beans` section – Reimeus May 27 '17 at 18:11
  • Done it, but now I get another error : org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available – Gimv13 May 27 '17 at 18:23
  • You will need to define an EntityManager bean as described in [this post](https://stackoverflow.com/questions/24520602/spring-data-jpa-no-bean-named-entitymanagerfactory-is-defined-injection-of-a) – Reimeus May 27 '17 at 18:34
  • Thanks a lot, it works like a charm! Meanwhile I've also added a controller and defined the transactionManager bean. Do you know other beans that are mandatory in projects like these? – Gimv13 May 27 '17 at 18:46