Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
配置SpringJunit4测试时如何配置@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" })?????
具体问题描述如下:
目录
- 问题报错
- 配置文件
- 分析原因
1、问题报错:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
2、配置文件
文档结构:
测试代码 MybatisTest1:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" })
public class MybatisTest1 {
private UserService userService;
@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
userService.findAll();
}
}
配置文件applicationContext.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/*/*.xml
</param-value>
</context-param>
<!-- log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>
/WEB-INF/conf/common/log4j.properties
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Loads the Spring web application context -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/common/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3、分析原因:
配置文件applicationConetxt.xml 找不到,导致注解无法找到userService bean的配置,但不知道 (@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" }))这个配置该如何修改。