关于NoSuchBeanDefinitionException: No bean named 'xxxx' is defined问题解决

问题如下

NoSuchBeanDefinitionException: No bean named 'userDao' is defined

查阅了网上关于IoC的资料后,明白了一些这方面的知识。这里单刀直入的给出解决方法。


楼主是在UserController中使用了如下代码

@Resource(name = "userDao")
private UserDao userDao;


为了找到这个接口的实现类,我们需要知道两点。1、有这个借口的实现类存在;2、怎么找到它。


楼主定义的类和实现的类如下图所示





包错的意思大致是没有找到userDao的实现。


在spring下创建spring-bean.xml,并在该文件中定义映射关系




文件内容


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="userDao" class="cn.tabris.demo.daoImpl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
</beans>

此时没完。工程的context依旧是root-context.xml。进入web.xml修改配置信息。由于楼主工程还有spring-hibernate.xml需要作为context

。因此此处改为spring-*.xml。


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/spring-*.xml</param-value>
	</context-param>

重新发布。发现登录时404。打开Console,发现在发布时已经报错。错误很长,截取重点部分

Error creating bean with name 'txPointcut': Instantiation of bean failed;

似乎又是bean初始化失败。但是本工程并没有使用过txPointcut这个bean。想起楼主的spring-hibernat.xml是复制其他工程的。进入ctrl+f发现如下内容


	<aop:config expose-proxy="true">
		<!-- 只对业务逻辑层实施事务 -->
		<aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" />
		<!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
	</aop:config>

将其注销。一切正常。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值