5

A bit of Details I am having some problem while running the code given below. I am getting the following exception. When i am trying the sample code of [CrudRepository for Spring Data][1].

I have an Interface:

package com.joydeep.springboot; 

import org.springframework.data.repository.CrudRepository; 
import com.joydeep.springboot.vo.Message; 

public interface Test1 extends CrudRepository<Message, String> { }

VO Class:

package com.joydeep.springboot.vo;

import java.util.Date;    
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Message {
    @Id
    private String id;
    private String text;
    private String author;
    private Date created;

    public Message() {
        super();
    }
    public Message(String id, String text, String author, Date created) {
        super();
        this.id = id;
        this.text = text;
        this.author = author;
        this.created = created;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    }

Controller class:

package com.joydeep.springboot; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController;  

@RestController 
public class HelloCntrl { 

   @Autowired 
   Test1 test; 

   @RequestMapping("/hello")
   public String sayHi(){
      return "Hi"; 
   } 

}

Initializer class:

package com.joydeep.springboot; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class CourseAPIStarter { 
   public static void main(String[] args) {
      SpringApplication.run(CourseAPIStarter.class); 
   } 
}

pom.xml:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.springboot</groupId>
    <artifactId>rest-course-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Rest service course API</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
</project>

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloCntrl': Unsatisfied dependency expressed through field 'test'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.joydeep.springboot.Test1' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations {@org.springframework.beans.factory.annotation.Autowired(required=true)}

The example i am referring is from this link.

https://spring.io/guides/gs/accessing-data-jpa/
viruskimera
  • 193
  • 16
Joydeep
  • 401
  • 2
  • 6
  • 15
  • 3
    Please format your code properbly – Jens Jan 10 '17 at 07:56
  • The errors says you don't have a Test1 class in the Spring container. – Essex Boy Jan 10 '17 at 07:58
  • Just by looking at it you have unsatisfied dependencies. Check your pom.xml or whatever build tool you are using! – jojo_Berlin Jan 10 '17 at 07:58
  • Please format your code in a proper way and give more details about what you are doing... – questionaire Jan 10 '17 at 08:18
  • Added more details , format is now a bit better . – Joydeep Jan 10 '17 at 09:00
  • I have used spring boot and maven to configure the project. – Joydeep Jan 10 '17 at 09:02
  • @Joydeep, please help us by writing a proper question. If you are free please refer this link for the next question you post. http://stackoverflow.com/help/how-to-ask – umar faraz Jan 10 '17 at 10:30
  • try addiing @EnableJpaRepositories(basePackages = {"joydeep.com"}) into Test1 – kamokaze Jan 10 '17 at 11:00
  • Strangely with 1.4.2.RELEASE I am unable to find EnableJpaRepositories. With Spring boot is it not so that EnableJpaRepository is enabled automatically. – Joydeep Jan 10 '17 at 11:20
  • it in import org.springframework.data.jpa.repository.config.EnableJpaRepositories; – kamokaze Jan 10 '17 at 11:57
  • One more thing from the spec -> By default, Spring Boot will enable JPA repository support and look in the package (and its subpackages) where SpringBootApplication is located. If your configuration has JPA repository interface definitions located in a package not visible, you can point out alternate packages using EnableJpaRepositories and its type-safe basePackageClasses=MyRepository.class parameter. – Joydeep Jan 10 '17 at 11:59
  • It seems that mport org.springframework.data.jpa.repository.config.EnableJpaRepo‌​sitories is missing. – Joydeep Jan 10 '17 at 12:07

2 Answers2

14

You don't need to put @Repository annotation on Test1. Put these 3 annotation on your CourseAPIStarter class your application will run like hot knife in butter.

@ComponentScan(basePackages={"com.joydeep.springboot"}) 

@EntityScan(basePackages={"com.joydeep.springboot.vo"}) 

@EnableJpaRepositories(basePackages={"com.joydeep.springboot"}) 

@ComponentScan will scan your objects, and will register them with Spring.

@EnableJpaRepositories will enable all your Spring JPA repositories to be used in your application.

@EntityScan: As you added @Entity annotation on your model object class, Spring will assume that a table will exists with name Message. So to register this class with Spring as Entity you need to use this annotation.

Sadeq Dousti
  • 3,346
  • 6
  • 35
  • 53
Angshuman
  • 225
  • 3
  • 17
  • Why is @SpringBootApplication not enough? Thanks – andrew0007 Sep 15 '19 at 14:56
  • 1
    @andrew0007 When you annotate your application class with `@SpringBootApplication` annotation it internally enables `@EnableAutoConfiguration`, `@ComponentScan`, `@ConfigurationPropertiesScan` etc. Now when `@EnableAutoConfiguration` is included Spring ties to guess the application behavior and adds the required configuration by reading the jar dependency you have added and if it founds any datasource is added in the project it will automatically configure these annotations with the default value. That means `@SpringBootApplication` alone will work and for these annotations. – Angshuman Oct 28 '19 at 19:27
  • [@SpringBootApplication](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-using-springbootapplication-annotation) – Angshuman Oct 28 '19 at 19:29
0

Spring can't find Test1 bean because you forgot to annotate it with @Repository. You can autowire only Spring managed classes (Controller, Component, Repository, Service ecc). Try with this:

@Repository
public interface Test1 extends CrudRepository<Message, String> { }
amicoderozer
  • 2,046
  • 6
  • 28
  • 44
  • 3
    I was looking at the [link](https://spring.io/guides/gs/accessing-data-jpa/). The code says that @Repository is not needed . – Joydeep Jan 10 '17 at 08:42
  • Also adding the annotation do not help, I am getting the same exception still. – Joydeep Jan 10 '17 at 08:42
  • Typically in a normal Spring configuration (with xml or annotations), this error means that the bean is not annotated correctly or that you haven't include in your web.xml the component-scan line. But since you use spring boot the only thing is missing here is Message class. Try post it please. The other classes seem correct to me. – amicoderozer Jan 10 '17 at 09:08