BeanCreationException: Errorcreating bean with name ‘userServiceImpl’:导致原因之一

  • Post author:
  • Post category:其他




Caused by: BeanCreationException: Errorcreating bean with name ‘userServiceImpl’:导致原因之一

在我使用JUnit测试service层的时候,报了以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

省略看不懂一连串…

Caused by: org.springframework.beans.factory.

BeanCreationException: Error creating bean with name ‘userServiceImpl’:

Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.he.dao.userDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)

at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)

at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)

… 24 more

Caused by: org.springframework.beans.factory.

NoSuchBeanDefinitionException: No qualifying bean of type ‘com.he.dao.userDao’

available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

重点就在于以上加粗字体BeanCreationException: Error creating bean with name以及NoSuchBeanDefinitionException: No qualifying bean of type ‘com.he.dao.userDao’,

发现问题所在:

当我们在使用JUnit测试dao层的时候,只需要如此配置

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({“classpath:spring/spring-dao.xml”})

public class testService {




}

但是在测试service层的时候,就不能只导入一个配置文件了,因为测试service层也会使用dao层,所以应该这样配置

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({“classpath:spring/spring-service.xml”,“classpath:spring/spring-dao.xml”})

public class testService {




}

要同时导入dao层和service层的配置文件

如果,Junit报no tests found matching

可能是比较简单的疏忽:

1.测试方法也许没有用public void 修饰

2.或者测试方法忘了加@Test注释



版权声明:本文为HE_hey原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。