spring 引导注释自动装配的问题

Problem with spring boot annotation autowired

我有以下问题。我试图用 Hybernate 启动一个 Spring 启动应用程序和 DB2 数据库。所以我创建了一个存储库并使用 @Autowired 注释从数据库中获取一些数据。问题是当我 运行 应用程序时,我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studenteRepository in com.ibm.snam.ai4legal.controller.HelloWorldController required a bean of type 'com.ibm.snam.ai4legal.repositories.StudenteRepository' that could not be found.


Action:

Consider defining a bean of type 'com.ibm.snam.ai4legal.repositories.StudenteRepository' in your configuration.

这是应用程序的 classes

申请class:

package com.ibm.snam.ai4legal.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.ibm"})
public class SBApplication {

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

存储库class:

package com.ibm.snam.ai4legal.repositories;

import org.springframework.data.repository.CrudRepository;

import com.ibm.snam.ai4legal.model.Studente;

public interface StudenteRepository extends CrudRepository<Studente, Integer>{

}

型号class:

package com.ibm.snam.ai4legal.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Studente{
    @Id
    @GeneratedValue
    private int id;
    private String nome;
    private String cognome;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getnome() {
        return nome;
    }
    public void setnome(String nome) {
        this.nome = nome;
    }
    public String getcognome() {
        return cognome;
    }
    public void setcognome(String cognome) {
        this.cognome = cognome;
    }
}

控制器class:

package com.ibm.snam.ai4legal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ibm.snam.ai4legal.repositories.StudenteRepository;

@RestController
public class HelloWorldController {

    @Autowired
    StudenteRepository studenteRepository;

    @GetMapping(value = "/home")
    public ModelAndView helloworld() {

        ModelAndView hello = new ModelAndView("helloworld");
        return hello;
    }
}

这里是 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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>projects</groupId>
    <artifactId>springwebapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>4.26.14</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

<!--  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>-->  

    <repositories>
        <repository>
            <id>repo</id>
            <url>file://${project.basedir}/lib</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <packaging>war</packaging>
</project>

在互联网上我发现我应该在一些配置文件中插入 <context:annotation-config/> 但我不知道我必须把它放在哪个文件中。有人可以帮忙吗?

您必须使用@ComponentScan 注释。试试下面的代码。

@ComponentScan({"com.ibm.*"})
@SpringBootApplication
public class SBApplication {

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

同时在 StudenteRepository 中提及 @Repository 注解 class。

因为你正在使用 spring-boot-starter-data-jpa 你需要提供注释 @EnableJpaRepositories 来告诉 springboot要自动配置 everything.So,您可能需要使用 springboot.The 的自动配置功能 @EnableJpaRepositories 注释对于自动配置 spring-data-jpa 不是强制性的,但在某些情况下,如果 spring组件扫描无法识别 class 路径中的 spring-data-jpa 您将不得不使用此注释告诉 spring 自动配置它。

@EnableJpaRepositories 将为 Spring 数据 JPA 启用自动配置支持,以了解 JPA 存储库的路径。默认情况下,它将仅扫描主应用程序包及其子包以检测 JPA repositories.So 注意将主应用程序 class 放在应用程序的根包中.

@EnableJpaRepositories(basePackages ="com.ibm")
@SpringBootApplication(scanBasePackages = {"com.ibm"})
public class SBApplication {

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

此外,如果您的实体 class 不在同一个包中,那么您可以使用 @EntityScan 注释来指定基础包。 在你的情况下,你没有在你的界面上指定@Repository注解,这将告诉spring-boot为你的interface.If创建默认实现,然后interface.If =40=] 将忽略接口,bean 的创建将不会 happen.You 无法自动装配它。因此提供它并在您的接口中声明方法,spring-bot 将处理休息。

@Repository
public interface StudenteRepository extends CrudRepository<Studente, Integer>{

    //If to find a student record by the id attribute
    public Studente findById();
}

要么将 SBApplication 移动到 com.ibm.snam.ai4legal 包,以便它可以从默认组件扫描中受益,要么添加以下注释以指定要为实体和存储库扫描的包。

@SpringBootApplication(scanBasePackages = {"com.ibm"})
@EnableJpaRepositories(basePackages = {"com.ibm"})
@EntityScan(basePackages = {"com.ibm"})
public class SBApplication {

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