gpt4 book ai didi

java - 带有调度程序的 Spring Boot-BeanCreationNotAllowedException : Error creating bean with name 'entityManagerFactory' : Singleton bean creation not allowed

转载 作者:搜寻专家 更新时间:2023-11-01 00:55:21 24 4
gpt4 key购买 nike

我们有一个带有调度程序的 spring boot 项目,它以固定的时间间隔从数据库中读取数据。

在使用 Maven 从 STS 构建项目时,我们在控制台运行测试用例时出现以下错误,即使最终构建状态为成功。

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:523) at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:276) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:162) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:145) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy70.findByTraIdAndTransactionNameAndExecutionTime(Unknown Source) at

申请文件

@SpringBootApplication
@PropertySource("classpath:application.properties")
@EnableScheduling
public class ProvisioningApplication {

public static void main(String[] args) {

SpringApplication.run(ProvisioningApplication.class, args);

}
}

调度程序文件

BusinessService有读取数据库的逻辑

@Component
public class SchedulerJob {

@Autowired
BusinessService service;

@Scheduled(fixedRate=300000) //5mnts
public void schdeule() {
service.startService();

}
}

测试文件

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public class ProvisioningApplicationTests {

@Test
public void contextLoads() {
}

}

这里的问题是为什么spring boot在构建项目的时候会运行scheduler任务,为什么会抛出上面的异常?

最佳答案

Spring Boot 中,当您执行 maven 构建时,测试用例默认运行。在这种情况下,运行集成测试 脚本将尝试连接到您的数据库。由于您没有任何东西可以作为项目集成测试的一部分执行。一种可能的解决方案是将您的类 ProvisioningApplicationTests 声明为抽象。这将限制 ProvisioningApplicationTests 类的实例创建。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public abstract class ProvisioningApplicationTests {
@Test
public void contextLoads() {
}
}

解决此问题的另一种方法是在您的 pom.xml 中包含以下代码

<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<skipTests>true</skipTests>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

这将排除您在构建您的项目时要执行的集成测试类。 ma​​ven-surefire-plugin 用于运行单元测试。 ma​​ven-failsafe-plugin 用于运行集成测试。使用此方法时,请确保所有集成类文件名都以 'IT' 结尾。例如。 UserTestIT.java

关于java - 带有调度程序的 Spring Boot-BeanCreationNotAllowedException : Error creating bean with name 'entityManagerFactory' : Singleton bean creation not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com