SSH整合出现NoSuchBeanDefinitionException: No bean named 'userinfoDao' is defined

多少楼台烟雨中 2014-11-03 12:00:22
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'test.TestService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userinfoDao' is defined
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userinfoDao' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:446)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:420)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:545)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)
... 26 more
...全文
10728 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
问题总结: *1.文件路径很坑爹 创建的时候把resources放在了src下 虽然classpath*:...能够在web.xml中找到 但是在applicationContext.xml中数据库配置加载路径就classpath:... 不是classpath*:...就找不到 *2.daoimpl方法中return (Userinfo) sessionFactory.getCurrentSession().createSQLQuery("from Useinfo u where u.username='"+username+"'"); sql查询用的是hql createSQLQuery 改为 createQuery ;查询出来的结果是query对象 却强制转换成Userinfo 应该调用 query.list() 或者 query.uniqueResult 再进行转换 3.@Service@Repository @Resource这些都不需要强制规定名称,貌似有个包自动转,类首字母小写(如SessionFactory=>sessionFactory)然后 service dao 切掉impl (如 UserDaoImpl=>userDao) 4.其他还有些小问题主要是路径 新手请见谅 再次感谢各位 对新手的支持 谢谢 在此结贴
  • 打赏
  • 举报
回复
引用 28 楼 qq_15800585 的回复:
它也许通过创建 'test.TestService 的bean 对象才能调用 set 方法进行注入,然后发现无法创建 'test.TestService 的bean 对象,是因为 test.TestService 对象没有交给 IOC 管理吗?
引用 29 楼 brockpeng 的回复:
可能是扫描包的问题,使用下面的扫描方式。 另外去掉强行指定名称,@Repository 使用@Autowired注入 <context:component-scan base-package="com.bjhnd"> <context:include-filter type="regex" expression=".Actions.*"/> <context:include-filter type="regex" expression=".Server.*"/> <context:include-filter type="regex" expression=".Dao.*"/> </context:component-scan>
问题解决了 谢谢各位 我是新手 表示很坑爹~~
brockpeng 2014-11-04
  • 打赏
  • 举报
回复
可能是扫描包的问题,使用下面的扫描方式。 另外去掉强行指定名称,@Repository 使用@Autowired注入 <context:component-scan base-package="com.bjhnd"> <context:include-filter type="regex" expression=".Actions.*"/> <context:include-filter type="regex" expression=".Server.*"/> <context:include-filter type="regex" expression=".Dao.*"/> </context:component-scan>
qq_15800585 2014-11-04
  • 打赏
  • 举报
回复
它也许通过创建 'test.TestService 的bean 对象才能调用 set 方法进行注入,然后发现无法创建 'test.TestService 的bean 对象,是因为 test.TestService 对象没有交给 IOC 管理吗?
  • 打赏
  • 举报
回复
还有就是整个项目运行 action注解是能够访问的 service dao 访问报空指针异常 在线等 急
  • 打赏
  • 举报
回复
引用 25 楼 u013887751 的回复:
<context:component-scan base-package="com.scjhweb.sshdemo" annotation-config="true"/> 改成这样,会扫描com.scjhweb.sshdemo下的所有包 楼上有个哥们提到了,不知道怎么引用他的话
以上方式我都试过了 IDE eclipse jdk8 希望各位大大能帮忙看下 可提供源代码 QQ278750600 可以多给点分
  • 打赏
  • 举报
回复
struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --> <constant name="struts.multipart.maxSize" value="10701096"/> <!-- 文件上传临时文件--> <constant name="struts.multipart.saveDir" value="d:/temp"/> <!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring" /> <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.devMode" value="true" /> <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 默认后缀名 --> <constant name="struts.action.extension" value="action," /> </struts>
  • 打赏
  • 举报
回复
database.properties #database jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.0.30:3306/scjhdemo jdbc.username=myscjh jdbc.password=myscjh #hibernate hibernate.show_sql=true hibernate.dialect=org.hibernate.dialect.MySQLDialect
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:database.properties" /> <!-- 创建数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxPoolSize" value="40"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="maxIdleTime" value="20"></property> </bean> <!--配置数据库工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.scjhweb.sshdemo.entity</value> </list> </property> </bean> <!-- 定义事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 申明annotation 加载事务驱动 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <!-- hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 --> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="transactionPointcut" expression="execution(*com.scjhweb.sshdemo.service..*.*(..))" /> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/> </aop:config> <!-- 注解配置 --> <context:annotation-config /> <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> </beans>
u013887751 2014-11-03
  • 打赏
  • 举报
回复
<context:component-scan base-package="com.scjhweb.sshdemo" annotation-config="true"/> 改成这样,会扫描com.scjhweb.sshdemo下的所有包 楼上有个哥们提到了,不知道怎么引用他的话
W15872276789 2014-11-03
  • 打赏
  • 举报
回复
把test包放到com.scjhweb.sshdemo下面
  • 打赏
  • 举报
回复
引用 20 楼 u011564172 的回复:
我没记错的话,你这么写com.scjhweb.sshdemo.*,只会扫描sshdemo下面一级的包,而你的dao显然层次更深
引用 21 楼 W15872276789 的回复:
点和* 都不要
这些我都试过了的 实际结果还是不行
Mr_JieLQ 2014-11-03
  • 打赏
  • 举报
回复
而我这么写com.scjhweb.sshdemo,是全路径,会递归扫描sshdemo下所有包
W15872276789 2014-11-03
  • 打赏
  • 举报
回复
点和* 都不要
Mr_JieLQ 2014-11-03
  • 打赏
  • 举报
回复
我没记错的话,你这么写com.scjhweb.sshdemo.*,只会扫描sshdemo下面一级的包,而你的dao显然层次更深
Mr_JieLQ 2014-11-03
  • 打赏
  • 举报
回复
引用 18 楼 yytmm2010 的回复:
[quote=引用 16 楼 u011564172 的回复:] 你的自动扫描路径
是不是写错了, <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> 中的路径com.scjhweb.sshdemo.最后面是不是多个"点",应该是

    <context:component-scan base-package="com.scjhweb.sshdemo*" annotation-config="true"/>  
这样写没错吧 com包下的scjhweb包下的sshdemo包的虽有子包 而且我action 是注解成功了的 只是service 和 dao 没成功。。。[/quote] 我上面写错了,是不要使用通配符的形式,路径改为这个com.scjhweb.sshdemo,你试试不就知道了
  • 打赏
  • 举报
回复
引用 16 楼 u011564172 的回复:
你的自动扫描路径
是不是写错了, <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> 中的路径com.scjhweb.sshdemo.最后面是不是多个"点",应该是

    <context:component-scan base-package="com.scjhweb.sshdemo*" annotation-config="true"/>  
这样写没错吧 com包下的scjhweb包下的sshdemo包的虽有子包 而且我action 是注解成功了的 只是service 和 dao 没成功。。。
  • 打赏
  • 举报
回复
引用 15 楼 shuipaomo062 的回复:
spring也是通过 bean来创建对象的,你在spring的配置文件里加上<bean id="userinfoDao" class="com.scjhweb.sshdemo.dao.UserinfoDao"/> 看看
你是想测试一下 配置文件下 是否能够正常运行?
Mr_JieLQ 2014-11-03
  • 打赏
  • 举报
回复
你的自动扫描路径
是不是写错了, <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> 中的路径com.scjhweb.sshdemo.最后面是不是多个"点",应该是

    <context:component-scan base-package="com.scjhweb.sshdemo*" annotation-config="true"/>  
小伙要自信 2014-11-03
  • 打赏
  • 举报
回复
spring也是通过 bean来创建对象的,你在spring的配置文件里加上<bean id="userinfoDao" class="com.scjhweb.sshdemo.dao.UserinfoDao"/> 看看
加载更多回复(11)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧