繁体   English   中英

Spring Boot注释自动接线的问题

[英]Problem with spring boot annotation autowired

我有以下问题。 我试图使用Hybernate使用DB2数据库启动Spring Boot应用程序。 因此,我创建了一个存储库,并使用@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.

这是应用程序的类

应用类别:

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);
    }
}

存储库类:

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>{

}

型号类别:

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;
    }
}

控制器类:

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批注。

由于您正在使用spring-boot-starter-data-jpa ,因此需要提供注释@EnableJpaRepositories来告诉springboot自动配置所有内容。因此,您可能希望使用springboot的自动配置功能。 @EnableJpaRepositories注释对于auto不是必需的配置spring-data-jpa但在某些情况下,如果spring组件扫描未在类路径中识别出spring-data-jpa ,则必须使用此批注告诉spring自动配置它。

@EnableJpaRepositories将启用对Spring Data JPA的自动配置支持,这需要知道JPA存储库的路径。 默认情况下,它将仅扫描主应用程序包及其子包以检测JPA存储库,因此请注意将主应用程序类放在应用程序的根包中

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

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

另外,如果您的实体类不在同一包中,则可以使用@EntityScan批注指定基本包。 在您的情况下,您没有在接口上指定@Repository批注该批注将告诉spring-boot为您的接口创建默认实现。如果未提供该批注,那么spring只会忽略该接口,并且不会进行Bean创建。将无法对其自动进行布线。因此,请提供该属性并在您的接口中声明方法,而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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM