您的位置:首页 > 其它

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

2015-06-09 10:28 519 查看
问题如下

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>


将其注销。一切正常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: