Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot 2.2多模块项目自动配置JPA失败_Java_Spring_Spring Boot_Hibernate_Spring Data - Fatal编程技术网

Java Spring Boot 2.2多模块项目自动配置JPA失败

Java Spring Boot 2.2多模块项目自动配置JPA失败,java,spring,spring-boot,hibernate,spring-data,Java,Spring,Spring Boot,Hibernate,Spring Data,我有一个Spring Boot多模块项目。 主要模块是一个Vaadin Web应用程序(目前非常简单),第二个模块是登录Spring实体、存储库和服务bean 在登录模块中,我编写了针对H2数据库运行的测试代码。测试为绿色,实体已创建,存储库已存在,等等。(一切正常) 当我尝试启动WebApp时,我总是收到消息 组件需要名为“entityManagerFactory”的bean,但找不到该bean 我的主要应用课程 @SpringBootApplication(scanBasePackages

我有一个Spring Boot多模块项目。 主要模块是一个Vaadin Web应用程序(目前非常简单),第二个模块是登录Spring实体、存储库和服务bean

在登录模块中,我编写了针对H2数据库运行的测试代码。测试为绿色,实体已创建,存储库已存在,等等。(一切正常)

当我尝试启动WebApp时,我总是收到消息

组件需要名为“entityManagerFactory”的bean,但找不到该bean

我的主要应用课程

@SpringBootApplication(scanBasePackages = "eu.dwich.btc", exclude = ErrorMvcAutoConfiguration.class)
@Import(BtcBackendLoginConfig.class)
public class Application extends SpringBootServletInitializer {

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

}
然后是模块中的配置类

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = BtcBackendLoginConfig.class)
@EntityScan(basePackageClasses = BtcBackendLoginConfig.class)
@EnableJpaRepositories(basePackageClasses = BtcBackendLoginConfig.class)
public class BtcBackendLoginConfig {
}

和属性文件

database.driver-class-name=org.postgresql.Driver
database.database-url=jdbc:postgresql://localhost:5433/btc_dev
database.database-user=btc_dev
database.database-user-password=xxxxx
# --------------------------------------
# special DB settings
database.hibernater-format-s-q-l=true
database.hibernater-gen-statistics=true
database.hibernate-dialect=org.hibernate.dialect.PostgreSQL9Dialect
# possible: create-drop, none, validate, update, create
database.hibernate-ddl-auto=create-drop
database.create-db-script=true
第一个(可能很简单)问题,我应该将application.properties放在哪里来配置数据库,在web项目中还是在登录模块中

第二个问题我做错了什么?无法让spring boot自动配置正常工作吗?(我不需要配置datasource和entitymanager)

我尝试了很多不同的配置,直到现在都没有效果

提前谢谢

更新1: 只有一行带有警告,没有其他stacktrace

2020-09-24 08:07:34.788  INFO 19699 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: Ant [pattern='/h2-console/**'], []
2020-09-24 08:07:34.802  INFO 19699 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@b246c96, org.springframework.security.web.context.SecurityContextPersistenceFilter@f632a44, org.springframework.security.web.header.HeaderWriterFilter@3bd390ec, org.springframework.security.web.authentication.logout.LogoutFilter@44e09d5a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@30d21b64, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1eed7157, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1d1c15ef, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@15a14e1e, org.springframework.security.web.session.SessionManagementFilter@6a47a0c5, org.springframework.security.web.access.ExceptionTranslationFilter@5492fd4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4f5c34f5]
2020-09-24 08:07:34.825  WARN 19699 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userGroupRepository': Cannot create inner bean '(inner bean)#542c45c7' 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)#542c45c7': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-09-24 08:07:34.827  INFO 19699 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
最后只有这条消息,有可能获得更多信息吗

2020-09-24 08:07:34.887 ERROR 19699 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

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


Process finished with exit code 0
更新: 打开调试时将生成此输出:

Exclusions:
-----------

    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

    org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration

    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

    org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration

    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

    org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:680) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:498) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1451) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at eu.dwich.btc.Application.main(Application.java:24) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.0.RELEASE.jar:2.2.0.RELEASE]

为什么spring排除了整个spring数据堆栈???

我想我发现了错误

  • Spring应用程序简单,无需导入
  • 后端配置需要一些修改
  • 第二个@SpringBootApplication是一个错误,不需要 如果没有自动配置导入,它将无法工作

    小心这些财产,塔达

    #spring.jpa.properties.hibernate.ddl-auto=create-drop is wrong
    #spring.jpa.hibernate.hbm2ddl.auto=create-drop is also wrong
    # the right one (as mentioned by Nico Van Belle) is: 
    spring.jpa.hibernate.ddl-auto=create-drop 
    
    如果您运行的是一个真实的数据库(不是H2),那么默认的ddl auto=为none


    在我看来,导入自动配置类并不是最好的方法,但它确实有效。

    我想我发现了错误

  • Spring应用程序简单,无需导入
  • 后端配置需要一些修改
  • 第二个@SpringBootApplication是一个错误,不需要 如果没有自动配置导入,它将无法工作

    小心这些财产,塔达

    #spring.jpa.properties.hibernate.ddl-auto=create-drop is wrong
    #spring.jpa.hibernate.hbm2ddl.auto=create-drop is also wrong
    # the right one (as mentioned by Nico Van Belle) is: 
    spring.jpa.hibernate.ddl-auto=create-drop 
    
    如果您运行的是一个真实的数据库(不是H2),那么默认的ddl auto=为none


    在我看来,自动配置类的导入不是最好的方法,但它是有效的。

    您使用了错误的变量键,因此Spring无法配置所需的对象

    相反,请使用以下命令

    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://localhost:5433/btc_dev
    spring.datasource.username=btc_dev
    spring.datasource.password=xxxxx
    
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format-sql=true
    spring.jpa.properties.hibernate.generate_statistics=true
    spring.jpa.database=postgresql
    spring.jpa.hibernate.dll-auto=create-drop
    
    另外,确保类路径上有所有必需的库。 您至少需要javax.persistence-api和hibernate

    我还建议您不要使用
    basePackageClasses
    ,除非您知道它的作用。如果您将应用程序配置放在他自己的包中而不是根中,Spring将无法找到任何符合条件的类。 至少现在,请改用
    basePackages=“eu.dwich.btc”

    是常见弹簧特性的列表。
    在#DATASOURCE部分旁边,您可以看到对
    DataSourceAutoConfiguration

    的引用,您使用了错误的变量键,因此Spring无法配置所需的对象

    相反,请使用以下命令

    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://localhost:5433/btc_dev
    spring.datasource.username=btc_dev
    spring.datasource.password=xxxxx
    
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format-sql=true
    spring.jpa.properties.hibernate.generate_statistics=true
    spring.jpa.database=postgresql
    spring.jpa.hibernate.dll-auto=create-drop
    
    另外,确保类路径上有所有必需的库。 您至少需要javax.persistence-api和hibernate

    我还建议您不要使用
    basePackageClasses
    ,除非您知道它的作用。如果您将应用程序配置放在他自己的包中而不是根中,Spring将无法找到任何符合条件的类。 至少现在,请改用
    basePackages=“eu.dwich.btc”

    是常见弹簧特性的列表。
    在#DATASOURCE部分旁边,您可以看到对
    DataSourceAutoConfiguration

    的引用。请检查pom文件,您一定缺少一些依赖项。我也遇到了同样的问题,在仔细检查了pom文件后得到了解决,posgress驱动程序jar缺失,spring数据依赖性缺失。它给出了非常一般的错误,您应该启用堆栈跟踪
    “服务器。错误。包含stacktrace=never”
    要获取更详细的错误信息。

    请检查pom文件,您必须缺少一些依赖项。我也遇到了同样的问题,在仔细检查了pom文件后得到了解决,posgress驱动程序jar缺失,spring数据依赖性缺失。它给出了非常一般的错误,您应该启用堆栈跟踪
    “服务器。错误。包含stacktrace=never”
    获取更详细的错误。

    @SpringBootApplication
    是一组注释,其中已包含
    @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
    。您不需要在
    BtcBackendLoginConfig
    中再次重复它们,您可以将属性文件放在类路径下的
    src/main/resources
    下。如果您不想将这些属性保留在代码库中,也可以将其外部化,请参考以下内容。一旦你修正了这些建议(在我的2条评论中),你的数据源应该是用自动配置构建的。我已经删除了注释并添加了@SpringBootApplication。My application.properties位于src/ain/resources下,但我可能会收到一个错误消息:组件需要一个名为“entityManagerFactory”的bean,但找不到该bean。能否提供异常堆栈跟踪?向主要问题添加了更新
    @SpringBootApplication
    是一个注释集合,其中已包含
    @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
    。您不需要在
    BtcBackendLoginConfig
    中再次重复它们,您可以将属性文件放在类路径下的
    src/main/resources
    下。如果您不想将这些属性保留在代码库中,也可以将其外部化,请参考以下内容。一旦你修正了这些建议(在我的2条评论中),你的数据源应该是用自动配置构建的。我已经删除了注释并添加了@SpringBootApplication。我的苹果