Spring Boot和MariaDB-自动装配问题-找不到Bean

时间:2019-10-21 20:27:14

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

我正在尝试将springboot后端配置为使用数据库'aah4_drugs'中的表'drugs'连接正在运行的mariadb数据库

我正在尝试使用MedicineService和Medicine库来调用现有的外部数据库。

但是,自动装配引起了问题。我添加了@EnableJpaRepositories,现在出现以下错误:


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-21 21:51:14.513 ERROR 31242 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1236)

The following method did not exist:

    javax.persistence.Table.indexes()[Ljavax/persistence/Index;

The method's class, javax.persistence.Table, is available from the following locations:

    jar:file:/Users/arianehine/IdeaProjects/SHProject/config-backend-spring/lib/javax.persistence.jar!/javax/persistence/Table.class
    jar:file:/Users/arianehine/.m2/repository/jakarta/persistence/jakarta.persistence-api/2.2.3/jakarta.persistence-api-2.2.3.jar!/javax/persistence/Table.class
    jar:file:/Users/arianehine/.m2/repository/javax/persistence/javax.persistence-api/2.2/javax.persistence-api-2.2.jar!/javax/persistence/Table.class

It was loaded from the following location:

    file:/Users/arianehine/IdeaProjects/SHProject/config-backend-spring/lib/javax.persistence.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.persistence.Table


Process finished with exit code 0

我已经在网上尝试了很多东西,但是我觉得我现在正在圈子里。

application.properties文件

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:mariadb://localhost:3306/aah4_drugs3
spring.datasource.username=aah4
spring.datasource.password=ari
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
spring.jpa.hibernate.ddl-auto=create-drop
# Naming strategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

主要春季班

package com.example.configbackendspring;

import com.example.configbackendspring.MedicineService;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@CrossOrigin
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ComponentScan("com.example")
@RestController
@EntityScan("com.example.configbackendspring")
public class ConfigBackendSpringApplication {
    String COUNT_ALL_QUERY = "SELECT COUNT(*) FROM drugs";

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

    @Autowired
    private MedicineService medicineService;
    @RequestMapping("/getAllMeds")
    public List<Medicine> getAllMeds() {



        return medicineService.getAll();

    }


}

Medicine.java

package com.example.configbackendspring;

import net.minidev.json.JSONObject;

import javax.persistence.*;

@Entity
@Table(name = "drugs")
public class Medicine {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "description")
    private String description;
    @Column(name = "toxicity")
    private String toxicity;

    public Medicine(int id, String name, String description, String toxicity) {
        this.id=id;
        this.name=name;
        this.description=description;
        this.toxicity=toxicity;
    }

    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 String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getToxicity() {
        return toxicity;
    }

    public void setToxicity(String toxicity) {
        this.toxicity = toxicity;
    }

    public JSONObject toJSONObject(){
        JSONObject object = new JSONObject();
        JSONObject medicineObject = new JSONObject();
        medicineObject.appendField("name", this.name);
        medicineObject.appendField("description", this.description);
        medicineObject.appendField("toxicity", this.toxicity);
        medicineObject.appendField("id", this.id);
        object.appendField("medicine", medicineObject);
        return object;

    }
}


MedicineRepository.java

package com.example.configbackendspring;


import com.example.configbackendspring.Medicine;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Repository
public interface MedicineRepository extends CrudRepository<Medicine,Integer> {


}



MedicineService.java 奇怪的是,MedicineRepository导入是灰色的,这意味着它没有被使用。

package com.example.configbackendspring;
import com.example.configbackendspring.MedicineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MedicineService {

    @Autowired
    private MedicineRepository medicineRepository;




    List<Medicine> getAll() {
        List<Medicine> meds = new ArrayList<>();
        System.out.println(medicineRepository);
        medicineRepository.findAll().forEach(meds::add);

        return meds;
    }

//    public Medicine getMed(int id) {
//        return medicineRepository.findById(id);
//    }

    public void addMedicine(Medicine medicine) {
        medicineRepository.save(medicine);
    }

//    public void deleteMedicine(String id) {
//        medicineList.removeIf(t -> t.getId().equals(id));
//    }
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-backend-spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-backend-spring</name>
    <description>Demo project for Spring Boot</description>

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


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.5.0</version>
        </dependency>
    </dependencies>

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

        </plugins>
    </build>

</project>

``
Please can someone help me figure the auto wiring error out so that I can proceed with my code.

2 个答案:

答案 0 :(得分:1)

我建议您尝试将@EnableJpaRepositories注释添加到ConfigBackendSpringApplication

类似这样的东西:

@CrossOrigin
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ComponentScan("com.example")
@RestController
@EntityScan
@EnableJpaRepositories
public class ConfigBackendSpringApplication {
...
}

只是为了完整性:

需要将spring-boot-starter-data-jpa添加到pom.xml

需要从javax.persistence-api删除pom.xml

需要为实体创建默认构造函数。 protected Medicine() {}

答案 1 :(得分:0)

库之间存在依赖关系冲突。删除lib解决了问题。请在这种情况下尝试运行mvn dependency:tree并解决冲突。

> jar:file:/Users/arianehine/IdeaProjects/SHProject/config-backend-spring/lib/javax.persistence.jar!/javax/persistence/Table.class