在线程类中怎么使用自动装配,线程中需要对数据库做操作,但是在线程类中使用自动装配的形式调用DB层的方法运行是报错,,,
代码如下:
@SpringBootTest
@RunWith(value = SpringRunner.class)
@Slf4j
class UsRealestateAggregateApplicationTests {
@Resource
private ToolRequestRepository toolRequestRepository;
@Test
void test3(){
System.out.println("=========>kaishi");
new Thread(new Runnable() {
@Override
public void run() {
for( int i=0; i<10; i++){
log.info("=====>"+i);
}
ToolRequest one = toolRequestRepository.getOne(3146);
log.info("====all");
log.info(JSON.toJSONString(one));
}
}, "qiao").start();
}
}
报错:
Exception in thread "qiao" org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed while 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:220)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:624)
at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:378)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:168)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:151)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy95.getOne(Unknown Source)
at com.********.UsRealestateAggregateApplicationTests$1.run(UsRealestateAggregateApplicationTests.java:70)
at java.lang.Thread.run(Thread.java:748)