SSH整合出现NoSuchBeanDefinitionException: No bean named 'userinfoDao' is defined sshexceptionjunit 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?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> database.properties#database jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.0.30:3306/scjhdemojdbc.username=myscjhjdbc.password=myscjh#hibernatehibernate.show_sql=truehibernate.dialect=org.hibernate.dialect.MySQLDialect 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> 你的spring配置文件根本就没有实现bean对象的创建啊,应该有bean并且把bean进行注入, #4 文件目录daoimplpackage com.scjhweb.sshdemo.dao.impl;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Repository;import com.scjhweb.sshdemo.dao.UserinfoDao;import com.scjhweb.sshdemo.entity.Userinfo;@Repository("userinfoDao")public class UserinfoDaoImpl implements UserinfoDao { @Resource private SessionFactory sessionFactory; public Userinfo findByUsername(String username){ return (Userinfo) sessionFactory.getCurrentSession().createSQLQuery("from Useinfo u where u.username='"+username+"'"); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }serviceimplpackage com.scjhweb.sshdemo.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;import com.scjhweb.sshdemo.dao.UserinfoDao;import com.scjhweb.sshdemo.entity.Userinfo;import com.scjhweb.sshdemo.service.UserinfoService;@Service("userinfoService")public class UserinfoServiceImpl implements UserinfoService { @Resource private UserinfoDao userinfoDao; public Userinfo findByUsername(String username) { // TODO Auto-generated method stub return userinfoDao.findByUsername(username); } public UserinfoDao getUserinfoDao() { return userinfoDao; } public void setUserinfoDao(UserinfoDao userinfoDao) { this.userinfoDao = userinfoDao; } } 以上是我的serviceimpl 和 daoimpl 请帮忙看看 你的 dao 为注册成功。 这个为testdemopackage test;import static org.junit.Assert.assertEquals;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.scjhweb.sshdemo.dao.UserinfoDao;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath*:applicationContext.xml")public class TestService extends AbstractJUnit4SpringContextTests { @Resource private UserinfoDao userinfoDao; @Test public void test(){ assertEquals(userinfoDao.findByUsername("dsltyyz"),null); } public UserinfoDao getUserinfoDao() { return userinfoDao; } public void setUserinfoDao(UserinfoDao userinfoDao) { this.userinfoDao = userinfoDao; } }运行这个报的异常 若是整个项目运行 就是空指针异常 没注入成功 你没有配置struts,把action的创建交给Spring吧 Struts与 (spring 注解)service dao 无关吧 而且 struts我调试通过了的而且 #1 写了的 <!-- 注解配置 --> <context:annotation-config /> <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> 我在网上找了跟多这方面的bug总结为2点:1.扫描包问题 2.注解问题但是这2点我都看了下 貌似没问题 有兴趣的可以加我QQ 278750600 给源代码调试 谢谢 你在spring的配置文件里加上这个bean 试试<bean id="userinfoDao" class="com.scjhweb.sshdemo.dao.UserinfoDao"/> 为啥你struts.xml里没有action的配置呢 #3中有<!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring" /> <!-- 注解配置 --> <context:annotation-config /> <!-- 自动扫描包 --> <context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/> spring也是通过 bean来创建对象的,你在spring的配置文件里加上<bean id="userinfoDao" class="com.scjhweb.sshdemo.dao.UserinfoDao"/> 看看 你的自动扫描路径是不是写错了, <!-- 自动扫描包 --> <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 没成功 这样写没错吧 com包下的scjhweb包下的sshdemo包的虽有子包 而且我action 是注解成功了的 只是service 和 dao 没成功我上面写错了,是不要使用通配符的形式,路径改为这个com.scjhweb.sshdemo,你试试不就知道了 我没记错的话,你这么写com.scjhweb.sshdemo.*,只会扫描sshdemo下面一级的包,而你的dao显然层次更深 而我这么写com.scjhweb.sshdemo,是全路径,会递归扫描sshdemo下所有包 把test包放到com.scjhweb.sshdemo下面 <context:component-scan base-package="com.scjhweb.sshdemo" annotation-config="true"/> 改成这样,会扫描com.scjhweb.sshdemo下的所有包 楼上有个哥们提到了,不知道怎么引用他的话 以上方式我都试过了 IDE eclipse jdk8 希望各位大大能帮忙看下 可提供源代码 QQ278750600 可以多给点分 还有就是整个项目运行 action注解是能够访问的 service dao 访问报空指针异常 在线等 急 它也许通过创建 'test.TestService 的bean 对象才能调用 set 方法进行注入,然后发现无法创建 'test.TestService 的bean 对象,是因为 test.TestService 对象没有交给 IOC 管理吗? 可能是扫描包的问题,使用下面的扫描方式。另外去掉强行指定名称,@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> 问题总结:*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.其他还有些小问题主要是路径 新手请见谅再次感谢各位 对新手的支持 谢谢 在此结贴 javaEE开发后台action的值jsp取不到 JAVA中Session失效的问题! 关于memcache中存放hashmap多线程访问 求在一个页面显示两个SSH检索结果集 分不多,麻烦了 java初中级水平提高方法 webservice如何入门 如何得到Tomcat运行环境? 请教SQL问题! JSP 的text框小技巧 50分求解一个错误...已经弄了好久了.. spring+hibernate配置双数据源的问题 求助,SSH,C3P0数据库连接一直涨。
<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>
#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" ?>
<!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>
daoimpl
package com.scjhweb.sshdemo.dao.impl;import javax.annotation.Resource;import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;import com.scjhweb.sshdemo.dao.UserinfoDao;
import com.scjhweb.sshdemo.entity.Userinfo;@Repository("userinfoDao")
public class UserinfoDaoImpl implements UserinfoDao { @Resource
private SessionFactory sessionFactory; public Userinfo findByUsername(String username){
return (Userinfo) sessionFactory.getCurrentSession().createSQLQuery("from Useinfo u where u.username='"+username+"'");
}
public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
serviceimpl
package com.scjhweb.sshdemo.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;import com.scjhweb.sshdemo.dao.UserinfoDao;
import com.scjhweb.sshdemo.entity.Userinfo;
import com.scjhweb.sshdemo.service.UserinfoService;@Service("userinfoService")
public class UserinfoServiceImpl implements UserinfoService { @Resource
private UserinfoDao userinfoDao;
public Userinfo findByUsername(String username) {
// TODO Auto-generated method stub
return userinfoDao.findByUsername(username);
}
public UserinfoDao getUserinfoDao() {
return userinfoDao;
}
public void setUserinfoDao(UserinfoDao userinfoDao) {
this.userinfoDao = userinfoDao;
}
}
package test;import static org.junit.Assert.assertEquals;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.scjhweb.sshdemo.dao.UserinfoDao;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:applicationContext.xml")
public class TestService extends AbstractJUnit4SpringContextTests { @Resource
private UserinfoDao userinfoDao;
@Test
public void test(){
assertEquals(userinfoDao.findByUsername("dsltyyz"),null);
} public UserinfoDao getUserinfoDao() {
return userinfoDao;
} public void setUserinfoDao(UserinfoDao userinfoDao) {
this.userinfoDao = userinfoDao;
}
}运行这个报的异常 若是整个项目运行 就是空指针异常 没注入成功
Struts与 (spring 注解)service dao 无关吧 而且 struts我调试通过了的
而且 #1 写了的
<!-- 注解配置 -->
<context:annotation-config />
<!-- 自动扫描包 -->
<context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/>
总结为2点:
1.扫描包问题
2.注解问题
但是这2点我都看了下 貌似没问题 有兴趣的可以加我QQ 278750600 给源代码调试 谢谢
<bean id="userinfoDao" class="com.scjhweb.sshdemo.dao.UserinfoDao"/>
#3中有
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" /> <!-- 注解配置 -->
<context:annotation-config />
<!-- 自动扫描包 -->
<context:component-scan base-package="com.scjhweb.sshdemo.*" annotation-config="true"/>
我上面写错了,是不要使用通配符的形式,路径改为这个com.scjhweb.sshdemo,你试试不就知道了
另外去掉强行指定名称,@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>
*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.其他还有些小问题主要是路径 新手请见谅
再次感谢各位 对新手的支持 谢谢 在此结贴