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/
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
It extends both CrudRepository and PagingAndSortingRepository. To perform CRUD operations, define repository extending CrudRepository. To perform CRUD as well as batch operations, define repository extends JpaRepository.
The autowiring of classes is done in order to access objects and methods of another class. But inside the service layer we always autowire the repository interface to do all the DML operations on the database.
CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With