`
zhb8015
  • 浏览: 377131 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Group-logo
Spring Roo杂谈
浏览量:0
社区版块
存档分类
最新评论

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

 

文档结构:

 

 

 

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

 

 

  • 大小: 13.3 KB
分享到:
评论
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:>这样的前缀解决了,有没有其它的解决办法呢?


相关推荐

    No bean named 'user' available

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' available

    spring源码解决

    解析了spring的底层实现机制。public interface BeanFactory { //这里是对FactoryBean的转义定义,因为如果使用bean的名字检索FactoryBean...boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

    a road map through nachos

    a road map through nachos,分为 Introduction to Nacho、Nachos Machine、Nachos Threads、User-Level Processes、Nachos Filesystem、Experience With Nachos Assignments、MIPS Architecture等七个部分

    SpringMybatis:springMybatis多数据源

    SpringMybatis Spring / Mybatis多... org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.proto.net.aron.aronMapper]的合​​格Bean作为依赖项:至少应有1个有资格作为此依赖

    SPRING API 2.0.CHM

    All Classes ...NamedBean NamedParameterJdbcDaoSupport NamedParameterJdbcOperations NamedParameterJdbcTemplate NamedParameterUtils NameMatchMethodPointcut NameMatchMethodPointcutAdvisor ...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    catch (NoSuchBeanDefinitionException nex) { ... } catch (BeansException bex) { ... } catch (Exception ex) { ... } } } FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    catch (NoSuchBeanDefinitionException nex) { ... } catch (BeansException bex) { ... } catch (Exception ex) { ... } } } FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 ...

Global site tag (gtag.js) - Google Analytics