它报的错误代码是:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDAOImpl' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
at com.hibernate.demo.Test.main(Test.java:16)
它的配置是:applicationContext.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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-autowire="autodetect">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/hibernate"></property>
<property name="username" value="sa"></property>
<property name="password" value="song"></property>
</bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
         <list>
         <value>com.hibernate.model.User</value>
         </list>
        </property>
        <property name="annotatedPackages">
            <list>
                <value>com.hibernate.model.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.jdbc.fetch_size">10</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            </props>
        </property>
    </bean>
    <context:component-scan base-package ="com.hibernate.model.UserDAOImpl"/>    
</beans>
我的源代码是:UserDAOImpl.javapackage com.hibernate.dao;
import org.springframework.stereotype.Component;
import com.hibernate.model.User;
@Component("userDAOImpl")
public class UserDAOImpl extends SuperDAO implements UserDAO{
public void save(User user) {
this.save(user);
}
}
SuperDAO.java@Component
public class SuperDAO extends HibernateDaoSupport {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
this.setSuperSessionFactory(sessionFactory);
}
}调用:Test.javapublic class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDAOImpl userDAOImpl=(UserDAOImpl)context.getBean("userDAOImpl");
User user=new User();
user.setId(1);
user.setName("和三");
user.setAge(11);
user.setSex("男");
userDAOImpl.save(user);
}
}
请大家帮忙指出错误!

解决方案 »

  1.   

    在文件UserDAOImpl.java 中加了@Component("userDAOImpl")和配置applicationContext.xml中加了 <context:component-scan base-package ="com.hibernate.model.UserDAOImpl"/>为什么都还不行呢?
      

  2.   

     <context:component-scan base-package ="com.hibernate.model.UserDAOImpl"/>  
    其属已经说了是 base-PACKAGE , 怎么你还写成类的全限定类名呢?? 开启了 Classpath 扫描后, Spring 会扫描配置的包下面所有的子包,
    所以你的配置改成 base-package="com.hibernate.model" /> 即可
      

  3.   

    楼上正解。
    另外好像还少了一项:
    <context:annotation-config />
    不然@Resource没法用。
      

  4.   


    Tag name: component-scan Description : Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected.  Note: This tag implies the effects of the 'annotation-config' tag, activating @Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit其实开启了 Classpath 扫描前者就可以不用了, 这个也是我一次配置错误的时候发现同样可以使用的时候
    偶然发现的 :)
      

  5.   

    1、SuperDAO 上不需要@Component2、
    @Component("userDAOImpl")
    public class UserDAOImpl extends SuperDAO implements UserDAO{}
    @Component换成@Repository试试,另外用接口名作bean名,即:
    @Repository("userDAO")使用时: UserDAO userDAO = (UserDAO)context.getBean("userDAO");
    最好是这种习惯,因为已经定制了接口。
    另外谢谢4楼的提醒,我抽空做下实验。
      

  6.   

    @Repository是哪个包里的,我怎么用不起它种注解呢?
      

  7.   

    @Repository可以用了,但是按6楼的方法修改过,还是报原来的错误!
      

  8.   

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
     <property name="mappingResources">
        <list>
          <value>cn/itcast/bean/Employee.hbm.xml</value>
        </list>
    </property>
     <property name="hibernateProperties">
     <value>
          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
          hibernate.hbm2ddl.auto=update
          hibernate.show_sql=false
          hibernate.format_sql=false
      </value>
     </property>
    </bean>
    俺平常都是这么配的,你这种配法没研究过
      

  9.   

    我已经放到我网站上了,如果有兴趣,请到 http://www.house08.cn/FileDown/src.rar 下载!
      

  10.   

    <context:component-scan base-package ="com.hibernate.model"/> 
    改为
    <context:component-scan base-package ="com.hibernate"/> 
      

  11.   

    谢谢 zhangjihao 我现在报以下错了!我的文件重新上传过,请您帮我看看 下载文件
    jar包下载
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOImpl' defined in file [D:\workspace\SSH\WebRoot\WEB-INF\classes\com\hibernate\dao\UserDAOImpl.class]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
    PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'superSessionFactory' threw exception; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.hibernate.demo.Test.main(Test.java:11)
    Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
    PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'superSessionFactory' threw exception; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
    ... 16 more
      

  12.   

    朋友们,我已经发到 http://topic.csdn.net/u/20100104/00/b619566d-f03f-4ef6-85b2-0bead2e33184.html 了,请大家帮帮忙
      

  13.   

    StackOverflowError ?
    是不是有死循环了?既然:
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    那么就可以在SuperDAO中这样取:
    @Resource protected SessionFactory sessionFactory;
    (setter && getter可要可不要)注意变量修饰权限为protected,这样可以在子类中:
    Session session = sessionFactory.getCurrentSession();那么就可以:
    session.save(user);而不能用getHibernateTemplate().save(user)了,因为HibernateTemplate根本没有在容器中装配。最后注意事务控制。
    可以SuperDAO上配上注解:@Transactional。但提前是xml中要声明事务。