简体   繁体   English

Spring Boot注释自动接线的问题

[英]Problem with spring boot annotation autowired

I have the following problem. 我有以下问题。 I trying to start a Spring Boot application with the DB2 database with Hybernate. 我试图使用Hybernate使用DB2数据库启动Spring Boot应用程序。 So i created a repository and used the @Autowired annotation to get some data from the DB. 因此,我创建了一个存储库,并使用@Autowired批注从数据库中获取了一些数据。 The problem is that when I run the application i receive the following error: 问题是,当我运行应用程序时,出现以下错误:

***************************
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.

Here are the classes of the application 这是应用程序的类

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

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

}

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

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

and here the pom.xml file 这里是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>

On the internet I found that I should insert <context:annotation-config/> in some configuration file but I have no idea in which file I have to put it. 在互联网上,我发现我应该在某些配置文件中插入<context:annotation-config/> ,但是我不知道该把它放在哪个文件中。 Someone can help? 有人可以帮忙吗?

You have to use @ComponentScan annotation. 您必须使用@ComponentScan批注。 Try the below code. 试试下面的代码。

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

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

Also mention @Repository annotation in StudenteRepository class. 还要在StudenteRepository类中提及@Repository批注。

Since you are using spring-boot-starter-data-jpa you need to provide the annotation @EnableJpaRepositories to tell springboot to autoconfigure everything.So you might want to use the auto configuration feature of springboot.The @EnableJpaRepositories annotation is not mandatory for auto configuring the spring-data-jpa but in some cases if spring component scan didn't recognize spring-data-jpa in classpath you will have to use this annotation to tell spring to autoconfigure it. 由于您正在使用spring-boot-starter-data-jpa ,因此需要提供注释@EnableJpaRepositories来告诉springboot自动配置所有内容。因此,您可能希望使用springboot的自动配置功能。 @EnableJpaRepositories注释对于auto不是必需的配置spring-data-jpa但在某些情况下,如果spring组件扫描未在类路径中识别出spring-data-jpa ,则必须使用此批注告诉spring自动配置它。

@EnableJpaRepositories will enabling auto configuration support for Spring Data JPA required to know the path of the JPA the repositories. @EnableJpaRepositories将启用对Spring Data JPA的自动配置支持,这需要知道JPA存储库的路径。 By default, it will scan only the main application package and its sub packages for detecting the JPA repositories.So take care to put the main application class at the root package of your application . 默认情况下,它将仅扫描主应用程序包及其子包以检测JPA存储库,因此请注意将主应用程序类放在应用程序的根包中

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

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

Also, if your entity classes are not in the same package then you can use the @EntityScan annotation to specify the base packages. 另外,如果您的实体类不在同一包中,则可以使用@EntityScan批注指定基本包。 In your case you have not specifies the @Repository annotation on your interface which will tell spring-boot to create default implementations for your interface.If that annotation is not provided then spring will just ignore the interface and the bean creation will not happen.You won't be able to autowire it .So provide that and have methods declared in your interface and spring-bot will take care of the rest. 在您的情况下,您没有在接口上指定@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();
}

Either move SBApplication to com.ibm.snam.ai4legal package so it can benefit from default component scanning or add the following annotations to specify packages to be scanned for entities and repositories. 可以将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