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

本文介绍了一个关于 Spring Junit4 测试配置中找不到 userService Bean 的问题,并提供了具体的测试代码和配置文件示例。分析了问题产生的原因可能是由于配置文件 applicationContext.xml 未被正确加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、配置文件

 

文档结构:

 

 

 

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值