no bean named is defined

场景:NoSuchBeanDefinitionException: No bean named 'userService' is defined

求助:NoSuchBeanDefinitionException: No bean named 'userService' is defined

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined

 

配置SpringJunit4测试时如何配置@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" })?????

 

具体问题描述如下:

 

目录

  1. 问题报错
  2. 配置文件
  3. 分析原因

 

1、问题报错:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined

 

 

2、配置文件

 

文档结构:

 
no bean named is defined
 

 

测试代码 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" }))这个配置该如何修改。

 

 

1 楼 zhb8015 2013-10-10  
问题可能找到了,spring-test对于(@ContextConfiguration(locations = { "classpath*:applicationContext-test.xml" }))这个注解只认classpath,所以配置其它的文件路径是找不到的,把配置改成以下就测试通过了。


把applicationContext.xml,jdbc.properties,log4j.properties 拷贝到src下

修改后的配置
A、测试类:
@ContextConfiguration(locations = { "classpath*:applicationContext-test.xml" })


applicationContext.xml中关于jdbc的配置:
<!-- jdbc.properties Directory -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="jdbc.properties" />
	</bean>
	


但是在测试类中只能把配置文件都放到classpath中了,不能用<file:>这样的前缀解决了,有没有其它的解决办法呢?