Spring boot 2.0.0.M4 需要一个名为“entityManagerFactory”的 bean,但无法找到

2024-06-26

使用 spring-boot 2.0.0.M4 的版本我遇到了这个问题:


    Description:
Field userRepository in 
webroot.websrv.auth.service.JwtUserDetailsServiceImpl required a bean 
named 'entityManagerFactory' that could not be found.


Action:

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

[WARNING] 
 java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at ...
Caused by: org.springframework.context.ApplicationContextException: 
Unable to start web server; nested exception is 
org.springframework.boot.web.server.WebServerException: Unable to start 
embedded Tomcat

Caused by: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'jwtUserDetailsServiceImpl': Unsatisfied 
dependency expressed through method 'setUserRepository' parameter 0; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'userRepository': Cannot create inner bean '(inner 
bean)#770f146b' 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)#770f146b': Cannot resolve reference to 
bean 'entityManagerFactory' while setting constructor argument; nested 
exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
bean named 'entityManagerFactory' available

我找到了解决方案,需要实现 JpaConfiguration:

 @Configuration
@EnableJpaRepositories(basePackages = "webroot.webserv",
    entityManagerFactoryRef = "entityManagerFactory",
    transactionManagerRef = "transactionManager")
@EnableTransactionManagement
public class JpaConfiguration {

@Autowired
private Environment environment;

@Value("${datasource.sampleapp.maxPoolSize:10}")
private int maxPoolSize;

/*
 * Populate SpringBoot DataSourceProperties object directly from 
application.yml 
 * based on prefix.Thanks to .yml, Hierachical data is mapped out of 
the box with matching-name
 * properties of DataSourceProperties object].
 */
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties dataSourceProperties(){
    return new DataSourceProperties();
}

/*
 * Configure HikariCP pooled DataSource.
 */
@Bean
public DataSource dataSource() {
    DataSourceProperties dataSourceProperties = dataSourceProperties();
        HikariDataSource dataSource = (HikariDataSource) 
org.springframework.boot.jdbc.DataSourceBuilder
                .create(dataSourceProperties.getClassLoader())

.driverClassName(dataSourceProperties.getDriverClassName())
                .url(dataSourceProperties.getUrl())
                .username(dataSourceProperties.getUsername())
                .password(dataSourceProperties.getPassword())
                .type(HikariDataSource.class)
                .build();
        dataSource.setMaximumPoolSize(maxPoolSize);
        return dataSource;
}

/*
 * Entity Manager Factory setup.
 */
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
throws NamingException {
    LocalContainerEntityManagerFactoryBean factoryBean = new 
LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource());
    factoryBean.setPackagesToScan(new String[] { "webroot.websrv" });
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    factoryBean.setJpaProperties(jpaProperties());
    return factoryBean;
}

/*
 * Provider specific adapter.
 */
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new 
HibernateJpaVendorAdapter();
    return hibernateJpaVendorAdapter;
}

/*
 * Here you can specify any provider specific properties.
 */
private Properties jpaProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", 
  environment.getRequiredProperty
("spring.jpa.properties.hibernate.dialect")
 );
 ...
    return properties;
}

@Bean
@Autowired
public PlatformTransactionManager 
transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(emf);
    return txManager;
}
}

Thanks for the suggestions.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring boot 2.0.0.M4 需要一个名为“entityManagerFactory”的 bean,但无法找到 的相关文章

  • 创建 RESTful WebService 并通过 Glassfish 4 提供服务

    我在 JEE6 中看到了很多关于 RESTful WebServices 的问题 所以我想与您分享这个示例解决方案 它展示了实现 RESTful Webservice 是多么容易 首先创建一个新的动态 Web 项目并将 Glassfish
  • Eclipse + Spring Boot 中“抛出 new SilentExitException()”处的断点

    每次我在 Eclipse IDE Spring Tool Suite 中以调试模式运行 Spring Boot 项目时 线程都会停止在throw new SilentExitException 即使没有断点也行 有什么解决方案可以避免这种行
  • Java无法读取字体

    好的 我在使用自定义字体时遇到问题 基本上我得到了从互联网上下载的自定义字体并在我的程序中使用它 当我在 Eclipse 我使用的编辑器 中运行该程序时 一切正常 没有问题 但是 每当我将它从 eclipse 导出到 jar 时 或者尝试从
  • RSA Java 加密和 Node.js 解密不起作用

    我有一个系统 需要在 javascript 中生成 RSA 密钥对 然后将公钥存储在服务器端的数据库中 作为字符串 然后 Java 中的服务器端将使用存储的公钥对字符串进行加密密钥并将其发送到客户端 客户端将使用私钥解密该字符串 我在客户端
  • HQL - 分页的行标识符

    有谁知道HQL是否有一个关键字来标识行 例如ROWID或ROWNUM 我想使用 HQL 实现分页 但我无法使用 setMaxResult 或 setFirstResult 因为我不直接使用会话对象 因此不使用 Query 对象 而只是将查询
  • Java(正则表达式)-获取句子中的所有单词

    我需要将 java 字符串拆分为单词数组 假设该字符串是 Hi I need to split this string into a serie s of words 目前我正在尝试使用这个String strs str split w 但
  • java中简单程序中的“未为类型定义方法”错误

    所以我只是想学习 Java 在观看了一些教程并阅读了一些基本内容之后 我对为什么它无法运行感到困惑 package Test public class TestProg public static void main String args
  • Spring Data JPA 规范继承

    我有三个实体 如下所示 Entity Inheritance strategy InheritanceType JOINED DiscriminatorColumn name type public abstract class Emplo
  • Java写入ByteArrayOutputStream内存泄漏

    我正在将图像字节写入ByteArrayOutputStream然后通过套接字发送它 问题是 当我这样做时 ImageIO write image gif byteArray 内存增加很多 有点内存泄漏 我用这个发送 ImageIO writ
  • 获取 Spring Boot 中当前活动数据源的引用

    我想通过实现数据库数据初始化DataSourceInitializer 我将这些方法放在我的 Spring Boot 主方法下面 但似乎它根本没有被执行 我尝试故意删除字符只是为了触发一个错误来确认执行 什么也没有发生 Configurat
  • double 或 BigDecimal 会溢出吗?

    Java 8 给了我们Math addExact https docs oracle com javase 8 docs api java lang Math html addExact int int 适用于整数 但不适用于小数 是否有可
  • Java 1.8 和 tomcat 6.0.53 引起:java.io.EOFException:SSL 对等点错误关闭

    java cp catalina jar org apache catalina util ServerInfo Server version Apache Tomcat 6 0 53 Server built Apr 19 2017 22
  • Java SFTP使用JSch上传,但如何覆盖当前文件?

    我正在尝试使用 JSch 通过 SFTP 将两个文件上传到服务器 如果目录为空 则上传文件效果很好 但我想一遍又一遍地上传相同的文件 只需更改内部的 id 但我不知道如何执行此操作 JSch 中有一些名为 OVERWRITE 的静态参数 但
  • Finalize() 何时执行? [复制]

    这个问题在这里已经有答案了 在一次采访中我被问到 假设 JVM 在 A 类对象未被使用时运行 gc class A some code here protected void finalize code here 它是否保证finalize
  • Java Reflection:为什么这么慢?

    我一直避免使用 Java 反射 因为它速度缓慢 我在当前项目的设计中达到了一个点 能够使用它将使我的代码更具可读性和优雅性 所以我决定尝试一下 我只是对这种差异感到惊讶 我注意到有时运行时间几乎延长了 100 倍 即使在这个简单的例子中 它
  • 如何更新 Websphere 7 以使用 EL2.2?

    这就是我所做的 按照此林肯 巴克斯特的帖子 http ocpsoft com java jsf2 java jsf2 how to add the magic of el el2 to jsf 我下载了 el api 2 2 jar 和 e
  • Scala 不可变 Map 速度慢

    当我创建地图时 我有一段代码 val map gtfLineArr 8 split map split collect case Array k v gt k v toMap 然后我使用这张地图来创建我的对象 case class MyOb
  • 在 Ubuntu 上的 Tomcat 中加载共享本机库

    如何在 Ubuntu 上的 Tomcat6 中加载共享库 我创建了一个名为 libawragrids so 的库 awragrids 并将其放置在 var lib tomcat6 shared 我在调用启动 tomcat 的终端中设置了以下
  • 从 Java 程序中获取局部变量的名称和类型

    这是我正在尝试的代码 JavaCompiler compilerA ToolProvider getSystemJavaCompiler int resultA compilerA run null null null Users a Do
  • 从 google play 中提取统计信息

    我正在建立一些统计数据 并希望获得来自 google play 应用程序商店 的统计数据 最受欢迎 下载量 价格等信息 有谁知道是否有这个 API 或者我必须自己抓取它 有一个名为 android market api 的项目http co

随机推荐