Spring Boot为什么要求我定义一个'entityManagerFactory'bean?

时间:2019-05-13 16:09:26

标签: java hibernate spring-boot

我正在尝试使用Spring Boot 2.1.3启动一个新的应用程序,并且收到此错误消息:

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

Description:

Field userProfileJpaRepository in ie.gtludwig.pa.service.impl.UserProfileServiceImpl required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

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


Process finished with exit code 0

我尝试从org.hibernate.hibernate-core移除<user>/.m2/repository/org/hibernate罐子,但仍然遇到相同的错误。实际上,为了确定并且仍然是相同的问题,我删除了<user>/.m2/repository并重新安装了所有依赖项。

我在StackOverflow类似线程上找到this,并通过删除休眠依赖关系解决了该问题,但这对我不起作用。

IntelliJ缓存也被清除。

有什么想法吗?

编辑-添加了UserProfileJpaRepository和pom.xml

UserProfileJpaRepository.java

public interface UserProfileJpaRepository extends JpaRepository<UserProfile, String> {

    UserProfile findByType(String type);
}

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>ie.gtludwig.pa</groupId>
    <artifactId>pa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>PA</name>
    <modules>
        <module>core</module>
        <module>engine</module>
    </modules>

    <properties>
        <!-- JAVA -->
        <version.java>1.8</version.java>
        <java.version>${version.java}</java.version>
        <jdk.version>${version.java}</jdk.version>
        <maven.compiler.target>${version.java}</maven.compiler.target>
        <maven.compiler.source>${version.java}</maven.compiler.source>
        <!-- Generic properties -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <!-- External Dependency Versions -->
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <spring-boot-jdbc.version>2.1.3.RELEASE</spring-boot-jdbc.version>
        <spring-boot-data-redis.version>2.1.3.RELEASE</spring-boot-data-redis.version>
        <jedis.version>2.9.1</jedis.version>
        <junit.version>4.12</junit.version>
        <logback.version>1.2.3</logback.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
        <flyway.version>5.2.0</flyway.version>
        <mysql.version>8.0.16</mysql.version>
        <h2database.version>1.4.199</h2database.version>
        <sendgrid-java.version>4.3.0</sendgrid-java.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <commons-io.version>2.6</commons-io.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <!--WEB-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>${thymeleaf-layout-dialect.version}</version>
        </dependency>

        <!--SECURITY-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
        </dependency>

        <!--OPERATIONS-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--            <version>${spring-boot-data-redis.version}</version>-->
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>io.lettuce</groupId>-->
<!--                    <artifactId>lettuce-core</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>redis.clients</groupId>-->
<!--            <artifactId>jedis</artifactId>-->
<!--            <version>${jedis.version}</version>-->
<!--        </dependency>-->

         <!--SERVER-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-mail</artifactId>-->
<!--        </dependency>-->

        <!--DATABASE-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${spring-boot-jdbc.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.flywaydb</groupId>-->
            <!--<artifactId>flyway-core</artifactId>-->
            <!--<version>${flyway.version}</version>-->
        <!--</dependency>-->
        <!--TEST DATABASE FOR TESTING PROCESSES AND RULES IN-MEMORY-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2database.version}</version>
        </dependency>

        <!--EMAIL-->
        <!-- https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java -->
        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>${sendgrid-java.version}</version>
        </dependency>

        <!--DEV/TEST-->
        <!--DEV TOOLS CONFLICT WITH JREBEL - DISABLE THIS IF JREBEL IS BEING USED -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.0.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- Resource plugin to enable expanding properties from this file so that they can be exposed by the zone (E.g. @project.version@) -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <nonFilteredFileExtensions>
                        <!--font/binary files must be excluded from filtering or they will be corrupted-->
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <!-- Add our project version to the manifest file -->
                            <version>${project.version}</version>
                            <description>${project.description}</description>
                        </manifestEntries>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <attachClasses>false</attachClasses>
                </configuration>
                <version>3.1.0</version>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>external</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <version>${spring-boot.version}</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

2 个答案:

答案 0 :(得分:0)

似乎您在自己的展示次数中注入了public void cargar_txt(){ File ruta = new File(ruta_txt); String linea = null; int ln = 0; try{ FileReader fr = new FileReader(ruta); BufferedReader bu = new BufferedReader(fr); p = null; while((linea = bu.readLine()) != null){ ln++; try{ StringTokenizer st = new StringTokenizer(linea, ","); while(st.hasMoreElements()){ p = new Producto(); p.setCodigo(Integer.parseInt(st.nextToken())); p.setNombre(st.nextToken()); p.setPrecio(Double.parseDouble(st.nextToken())); p.setDescripcion(st.nextToken()); p.setEdad(Integer.parseInt(st.nextToken())); p.setDireccion(st.nextToken()); p.setTelefono(Double.parseDouble(st.nextToken())); p.setOcupacion(st.nextToken()); p.setEstatoCivil(st.nextToken()); p.setFechaLlamada(st.nextToken()); p.setTipoServicio(st.nextToken()); p.setCategoriaServicio(st.nextToken()); p.setEstadoServicio(st.nextToken()); p.setEmpleadoSeguimiento(st.nextToken()); p.setNumeroLlamadas(Integer.parseInt(st.nextToken())); rp.agregarRegistro(p); } }catch(NoSuchElementException ex){ System.out.println(String.format("NSE in %d: %s", ln, linea)); } }bu.close(); }catch(Exception e){ mensaje("Error al cargar el archivo: " + e.getMessage()); mensaje("Error: " + e); System.out.println(e.getMessage()); System.out.println(e); } }
要获得EntityManagerFactory,您需要执行以下操作:

EntityManagerFactory

答案 1 :(得分:-1)

似乎您是在没有Bean的情况下自动装配的。 也许在您的主类中创建@bean可以解决该问题。

@Bean
public EntityManagerFactory entityManagerFactory() {
 return new EntityManagerFactory();
}