当前位置: 首页 > 知识库问答 >
问题:

在定义bean时遇到麻烦,"考虑在配置中定义一个名为'entityManagerFactory'的bean"

姬俊远
2023-03-14

尝试运行我的Spring启动应用程序,我的编译器说;“_worldcom.example.hello字段回购。UserService需要一个名为entityManagerFactory的bean,但找不到。

注入点有以下注释:-@org.springframework.beans.factory.annotation.自动生成(必需=true)"

Spring启动的新手,所以我不太确定为什么我的项目没有运行。我尝试过使用不同的注释,甚至使用“basPackages={”com.example.hello_world}”,并在我的xml文件中添加不同的依赖项,但似乎没有任何效果。不确定哪个文件是我的配置文件。M我的编译器



  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-08 13:30:58.527  INFO 904 --- [           main] c.e.hello_world.HelloWorld1Application   : Starting HelloWorld1Application on LAPTOP-QBE4L7C0 with PID 904 (C:\Users\Tristan\eclipse-workspace\hello_world-1\target\classes started by Tristan in C:\Users\Tristan\eclipse-workspace\hello_world-1)
2020-02-08 13:30:58.530  INFO 904 --- [           main] c.e.hello_world.HelloWorld1Application   : No active profile set, falling back to default profiles: default
2020-02-08 13:30:58.914  INFO 904 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-02-08 13:30:58.962  INFO 904 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 39ms. Found 1 JPA repository interfaces.
2020-02-08 13:30:59.390  INFO 904 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-02-08 13:30:59.397  INFO 904 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-02-08 13:30:59.397  INFO 904 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-08 13:30:59.519  INFO 904 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-02-08 13:30:59.519  INFO 904 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 949 ms
2020-02-08 13:30:59.582  WARN 904 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#420745d7' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#420745d7': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-02-08 13:30:59.584  INFO 904 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-02-08 13:30:59.657  INFO 904 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-08 13:30:59.728 ERROR 904 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field repo in com.example.hello_world.UserService 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.

我的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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hello_world-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello_world-1</name>
    <description>Demo project for Spring Boot</description>



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

    <dependencies>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.0.0.Alpha4</version>
            <type>pom</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>6.0.0.Alpha4</version>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            </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>
    </dependencies>

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

</project>

我项目的第一个文件,


    package com.example.hello_world;

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


@SpringBootApplication
@EnableJpaRepositories

public class HelloWorld1Application {

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

}

我的服务文件用户服务。爪哇


    package com.example.hello_world;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Service

@EntityScan("com.example.hello_world")
@ComponentScan("com.example.hello_world")
public class UserService {

        @Autowired
        private UserRepository repo;

        public List<UserInfo> listAll()
        {
            return repo.findAll();
        }


        public void save(UserInfo User)
        {
            repo.save(User);
        }


        public UserInfo get(Long id)
        {
            return repo.findById(id).get();
        }

        public void delete(Long id)
        {
            repo.deleteById(id);
        }
}

我的响应文件用户存储库。贾弗


    package com.example.hello_world;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;



@Service

@EntityScan("com.example.hello_world")
@ComponentScan("com.example.hello_world")
public class UserService {

        @Autowired
        private UserRepository repo;

        public List<UserInfo> listAll()
        {
            return repo.findAll();
        }


        public void save(UserInfo User)
        {
            repo.save(User);
        }


        public UserInfo get(Long id)
        {
            return repo.findById(id).get();
        }

        public void delete(Long id)
        {
            repo.deleteById(id);
        }
}

我的控制器文件应用程序Controller.java


    package com.example.hello_world;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
import org.springframework.ui.*;

@Controller
public class AppController
{

    @Autowired
    private UserService service;

    @RequestMapping("/")
    public String ViewHomePage(Model model)
    {
        List<UserInfo> listUserInfo = service.listAll();
        model.addAttribute("listUserInfo", listUserInfo);

        return "index";
    }
}


共有1个答案

慕弘义
2023-03-14

恐怕你没有把事情做好。

您希望有一个带有控制器和存储库的Spring Boot应用程序。对于控制器来说,您的配置似乎还可以,但对于存储库来说,则不行。

  1. 在冬眠和日食之间下定决心。您正在项目中导入这两者。如果您不打算使用EclipseLink,那么请相信我,删除它并使用Hibernate
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

如果你不这样做,那么你最终会错过一些强制性的bean,比如entityManagerFactoryBean。事实上,启动依赖项是用来加载一堆AutoConfiguration类的,还有一个if用于配置entityManagerFactoryBean

这是我给你推荐的POM:

<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hello_world-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello_world-1</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </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>
    </dependencies>

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

</project>

试一试,让我知道结果。

 类似资料:
  • 我从以下链接下载了示例代码:https://spring.io/guides/gs/accessing-data-mysql/我更改了应用程序。属性文件如下: 之后,我转到源文件夹并运行以下命令:mvn clean spring boot:run发生此错误: 请帮我解决这个问题

  • 试着运行我的spring boot应用程序,我的编译器说;“com.example.hello_world.userservice中的字段repo需要一个名为'Entity ManagerFactory‘的bean,但找不到该bean。 注入点有以下注释:-@org.springframework.beans.factory.annotation.AutoWired(required=true)“

  • 角色JAVA 任务JAVA 使用者JAVA RoleRepository.java TaskRepository。JAVA 用户库 登录注册应用程序。JAVA application.properties http://maven.apache.org/xsd/maven-4.0.0.xsd" 波姆。xml [错误]测试运行:1,失败:0,错误:1,跳过:0,耗时:2.941秒

  • 尝试启动Spring Boot应用程序时,我收到以下错误: 注意:仅当我将自动连线注释置于IDeviceRepository IDeviceRepository上方时,才会发生此错误 。java类 我还没有让持久化到数据库-但是正在创建实体 这是项目的目录结构: com。美国广播公司。初始化。应用JAVA com.abc.controller.ontroller.java com。美国广播公司。服

  • 我正在Spring中开发一个应用程序,使用Tomcat、Mysql5、Java8。。。问题是,由于“required bean‘entityManagerFactory’not found”问题,我无法部署它。我与同事一起开发了这个项目,但他们可以完美地部署它,即使我在Spring工具套件中复制粘贴相同的项目。怎么可能呢?错误: 这是我的pom.xml 这是我的存储库 这是我的控制器 应用属性 实

  • 我只是在学习如何通过spring上传文档。所以,在进行这项工作时,我面临着上述问题。由于有许多问题与同一个问题相关,我参考了它们并尝试了所有方法,但没有一个问题得到纠正。事先,我不需要为我的存储库接口添加@Repository,也不需要在我的应用程序类中添加@EnableJpaRepositories,因为我已经使用了正确的包序列。如需更多参考,请参阅https://dzone.com/artic