source

스프링 부트 & MariaDB - 자동 배선 문제 - 빈을 찾을 수 없음

lovecheck 2023. 8. 10. 18:56
반응형

스프링 부트 & MariaDB - 자동 배선 문제 - 빈을 찾을 수 없음

데이터베이스 'aah4_drugs'에 있는 테이블 'drugs'를 사용하여 실행 중인 mariadb 데이터베이스에 연결하도록 스프링부트 백엔드를 구성하려고 합니다.

기존 외부 데이터베이스에 전화를 걸기 위해 Medicine Service and Medicine Repository를 사용하려고 합니다.

그러나 자동 배선으로 인해 문제가 발생했습니다.@EnableJpaResposities를 추가했는데 다음 오류가 발생했습니다.


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

    }


}

약.자바

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;

    }
}


의약품 저장소.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.

한 번 시도해 보고 추가하는 것을 제안합니다.@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() {}

라이브러리 간에 종속성 충돌이 있습니다.lib를 제거하여 문제가 해결되었습니다.제발, 이런 경우에는 도망가도록 해보세요.mvn dependency:tree그리고 갈등을 해결합니다.

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

언급URL : https://stackoverflow.com/questions/58493671/spring-boot-mariadb-autowiring-problem-bean-not-found

반응형