Spring data JPA - Kein qualifying-bean gefunden für die Abhängigkeit

Ich bin zu haben, ein einfacher test mit spring data jpa.

Habe ich eine einfache pojo eine Schnittstelle , und eine runner Anwendung.

Hier ist mein code:

package aa.bb.cc.repository;

@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}

Und habe ich eine einfache POJO:

@Entity
@Table(name = "content")
public class Content {

public Content(String name, String title, String description) {
    this.name = name;
    this.title = title;
    this.description = description;
}

@NotNull
private String name;

@NotNull
private String title;

@NotNull
private String description;
...
}

Und Application Klasse:

package aa.bb.cc.repository;

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

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

    @Bean
    public CommandLineRunner demo(ContentRepository repository) {
        return (args) -> {
            //save two contents
            repository.save(new Content("name1", "title1", "description1"));

            //fetch all Contents
            log.info("Contents found with findAll():");

            for (Content eachContent : repository.findAll()) {
                log.info(eachContent.toString());
            }
            log.info("");
        };
    }
}

Meine pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.185</version>
</dependency>

Bekomme ich diese exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [aa.bb.cc.repository.ContentRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations: {}

Sah ich einige Probleme, aber dieses problem nicht lösen können.
Was ist die Lösung?

UPDATE

spring-config.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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <jpa:repositories base-package="aa.bb.cc.repository"/>

    <context:annotation-config/>

</beans>
InformationsquelleAutor CVV | 2015-11-30
Schreibe einen Kommentar