考虑在spring BOOT中的配置中定义一个类型为Repository的bean

时间:2018-02-25 13:39:16

标签: spring spring-boot spring-data-jpa

运行主

时出错

申请失败

描述: com.innodata.service.TopicService中的字段topicRepository需要一个类型为' com.innodata.dao.TopicRepository'的bean。无法找到。 行动: 考虑定义一个类型为' com..data.dao.TopicRepository'的bean。在你的配置中。**

配置bootstrap类

package com.innodata.bootstrap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.innodata")
public class BootstrapAplication {
    public static void main(String... strings) {
        SpringApplication.run(BootstrapAplication.class, strings);
    }
}

控制器

package com.innodata.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.innodata.modal.Topic;
import com.innodata.service.TopicService;

@RestController
public class TopicController {
    @Autowired
    private TopicService topicService;

    @RequestMapping(value = "/topics")
    public List<Topic> getAllTopics() {
        return topicService.getAllTopic();
    }

    @RequestMapping(method = RequestMethod.POST, value = "/topics")
    public void addTopic(@RequestBody Topic topic) {
        topicService.addTopic(topic);
    }
}

服务

package com.innodata.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.innodata.dao.TopicRepository;
import com.innodata.modal.Topic;

@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

    public List<Topic> getAllTopic() {
        List<Topic> topics = new ArrayList<Topic>();
        topicRepository.findAll().forEach(topics::add);
        return topics;
    }

    public void addTopic(Topic topic) {
        topicRepository.save(topic);
    }
}

模态是

package com.innodata.modal;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Topic implements Serializable {
    @Id
    private Integer id;
    private String name;
    private String desription;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Topic() {

    }

    public Topic(Integer id, String name, String desription) {
        super();
        this.id = id;
        this.name = name;
        this.desription = desription;
    }

    public String getDesription() {
        return desription;
    }

    public void setDesription(String desription) {
        this.desription = desription;
    }
}

存储库层是

package com.innodata.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.innodata.modal.Topic;
@Repository
public interface TopicRepository extends CrudRepository<Topic,Integer> {

}

Pom是

<project xmlns="http://maven.apache.org/POM/4.0.0" 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>com.innodata</groupId>
    <artifactId>springbootJPA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.10.RELEASE</version>
        </dependency>


    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

请帮助我。

0 个答案:

没有答案