Spring Boot注释自动接线的问题

时间:2019-07-14 17:19:58

标签: java spring spring-boot 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/>,但是我不知道该把文件放在哪个文件中。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:3)

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

df[df.region == 'ASIA'].set_index('period')\
    .groupby([pd.Grouper(freq='Y'), 'cor', 'moa']).sum()

还要在StudenteRepository类中提及@Repository注释。

答案 1 :(得分:0)

由于您正在使用 spring-boot-starter-data-jpa ,因此需要提供注释 @EnableJpaRepositories 来告诉springboot自动配置所有内容。因此,您可能需要使用springboot的自动配置功能。@EnableJpaRepositories注释对于自动配置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();
}

答案 2 :(得分:0)

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