java - Controller 中的字段需要名为 'entityManagerFactory' 的 bean,但无法找到

标签 java spring spring-boot jpa gradle

我根本不知道代码有什么问题。我用谷歌搜索了几个小时,但没有一个建议/标记的解决方案对我有用。我只是不明白为什么找不到这个entityManagerFactory类。

我使用MySQL作为后端数据库

项目的树形结构:

├───.gradle
│   ├───4.3
│   │   ├───fileChanges
│   │   ├───fileContent
│   │   ├───fileHashes
│   │   └───taskHistory
│   └───buildOutputCleanup
├───.settings
├───bin
│   └───com
│        ├───controller
│        └───model
├───build
│   ├───classes
│   │   └───java
│   │       ├───main
│   │       │   └───com
│   │       │        ├───controller
│   │       │        └───model
│   │       └───test
│   ├───libs
│   ├───reports
│   │   └───tests
│   │       └───test
│   │           ├───classes
│   │           ├───css
│   │           ├───js
│   │           └───packages
│   ├───resources
│   │   └───main
│   ├───test-results
│   │   └───test
│   │       └───binary
│   └───tmp
│       ├───bootJar
│       ├───compileJava
│       ├───compileTestJava
│       └───jar
├───gradle
│   └───wrapper
└───src
    ├───main
    │   ├───java
    │   │   └───com
    │   │        ├───controller
    │   │        └───model
    │   └───resources
    └───test
        └───java

Application.java 位于:src/main/java/com/

Controller 位于/src/main/java/com/controller/

AccountRepositoryAccount 位于/src/main/java/com/model

主类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @SpringBootApplication
    @EnableJpaAuditing
    @EnableJpaRepositories
    public class Application {

        // Start application
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

AdminAccount 类:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "ccs_account_admin")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class AdminAccount implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name = "account_id")
    private int account_id; 

    @Column(name = "first_name")
    private String first_name;

    @Column(name = "last_name")
    private String last_name;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    public AdminAccount AdminAccount (String email, String password) {
        return this;
    }

    @Override
    public String toString() {
        return "Account [id=" + id + ", account_id=" + account_id + ", first_name=" + first_name + ", last_name="
                + last_name + ", email=" + email + ", password=" + password + "]";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAccount_id() {
        return account_id;
    }

    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

AdminAccountRepo:

@Repository
public interface AdminAccountRepository  extends JpaRepository<AdminAccount, Integer> {

    @Query("Select * from ccs_account_admin where email=:email and password=:password")
    AdminAccount getAccount(@Param("email") String email, @Param("password") String password);   
}

Controller :

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private AdminAccountRepository adminAccountRepository; <--The problem happens here

    @GetMapping("/account") 
    public AdminAccount account(
            @RequestParam(value="email", required = true) String email,
            @RequestParam(value="password", required = true) String password)
    {       
        System.out.println("email:" + email);
        System.out.println("pass:" + password);
        AdminAccount account = adminAccountRepository.getAccount(email, password);      
        System.out.println("account: " + account);;
        return account;     
    }
}

Gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
    }
}

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8
targetCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()

}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    //api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    //implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'    
    // https://mvnrepository.com/artifact/junit/junit
    testCompile group: 'junit', name: 'junit', version: '4.12'   
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12' 
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.0.4.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.4.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'

}

我尝试了不同的注释,删除 .m2 目录,重建整个东西,不同的依赖关系和各种其他东西......但无济于事

编辑:

应用程序属性:

#General
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

##PostgreSQL Configuration
#spring.jpa.hibernate.ddl-auto=none
#spring.datasource.url=jdbc:postgresql://185.83.216.7:5432/orderlyq
#spring.datasource.username=postgres
#spring.datasource.password=postgres
#
## Disable feature detection by this undocumented parameter. Check the org.hibernate.engine.jdbc.internal.JdbcServiceImpl.configure method for more details.
#spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
#
## Because detection is disabled you have to set correct dialect by hand.
#spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect


#MySQL Configuration
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:myql://127.0.0.1:3306/cc_stats
spring.datasource.username=root
spring.datasource.password=password

错误消息:

Description:

Field adminAccountRepository in com.controller.LoginController required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

最佳答案

您已排除 Spring Boot 对 DataSource 的自动配置,并且似乎没有手动配置 DataSource bean。 JPA 需要一个 DataSource,因此,如果没有 DataSource,JPA 将不会自动配置。因此,将没有可用的 EntityManagerFactory bean,因此您会看到失败。

要解决此问题,您需要一个 DataSource bean。获取其中一个的最简单方法是删除禁用自动配置的配置:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

关于java - Controller 中的字段需要名为 'entityManagerFactory' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52229139/

相关文章:

java - 绑定(bind)<丰富:listShuttle> or put two list in render attribute

java - 包含路径的 URI 模板变量?

java - 性能问题持续存在/使用 JPA 更新大量对象

Java AJAX 变量传递

java - Spring , hibernate : Check for timestamp within expiration time

java - 在 HTTP 请求之间使 Future 可用的安全方法是什么?

java - 玩!框架返回 json 响应

java - 在 Spring 中更改数据库

spring - 如何使用 Spring 嵌入的 ActiveMQ 代理指定自定义的 activemq.xml?

java - 如何在不使用第三个多模块 Maven 项目的情况下自动从命令行构建主 Maven 项目之前构建项目依赖关系?