解决方案 »

  1.   

    applicationContext.xml已经配置了 sessionFactory
    <bean id="PhotoDaoImpl" class="com.org.dao.impl.PhotoDaoImpl">
            <property name="sessionFactory" >
            <ref local="sessionFactory" />
        </property>
    所以 PhotoDaoImpl 继承HibernateDaoSupport即可,不需要一下代码
     private HibernateTemplate hibernateTemplate ;
     private SessionFactory sessionFactory; 
      

  2.   

    你都让spring容器去管理类了   当然不能自己new了  要用spring的getBean方法去创建实例 这样spring才能帮你注入
      

  3.   

    +
    new对象的时候 赋值了吗,如果是空对象 会报错 的,最好交给spring管理报错代码也贴出来呗
      

  4.   

    +
    new对象的时候 赋值了吗,如果是空对象 会报错 的,最好交给spring管理报错代码也贴出来呗
    这是servlet的代码 其中注释里的new是原来的 后来改写的是下面的那两句 结果就一直报
    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is java.lang.IllegalArgumentException: At least one base package must be specifiedpackage com.org.util;import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;import com.org.dao.impl.PhotoDaoImpl;
    import com.org.entity.Photo;/**
     * Servlet implementation class test
     */
    public class test extends HttpServlet {
    private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public test() {
            super();
            // TODO Auto-generated constructor stub
        } /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    } /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Photo photo = new Photo();

    String username = request.getParameter("username");
    photo.setPhotoId(111);
    username = username+".jpg";
    System.out.print(username);
    photo.setPhotoname(username);

    System.out.print("before photodaoimpl is created");

    //PhotoDaoImpl photoDaoImpl = new PhotoDaoImpl();
    ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml");
         
    PhotoDaoImpl photoDaoImpl = (PhotoDaoImpl) ac.getBean("PhotoDaoImpl");

         if(photoDaoImpl == null){
    System.out.print("dao is null");
    if(photoDaoImpl.getHibernateTemplate() == null){
    System.out.print("hb is null");
    }
    }
    try {
    photoDaoImpl.addPhoto(photo);
    System.out.print("save is ok");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.print("Save is error");
    }
    }}
      

  5.   

    结果只能出来 username 和 before photodaoimpl is created 然后就报错了 
    applicationContext在src目录下
      

  6.   

    可能存在的问题
    1. 先检查一下你的Spring配置文件里 
    <bean id ="sessionFactory" class="xxxxxxxx">xxxxxxxxx</bean>
    这个配置好了没 或者是否注解注入2.是否配置<property name="sessionFactory" ref="sessionFactory">
    或者session 有没有没有引用sessionFactory??
      

  7.   

    因为最开始报错就是没找到sessionFactory 所以我先看自己有没有设置注入 这个daoimpl的设置应该没有问题 
    因为没有通过编译 所以sessionFactory的配置不敢保证 但是是照着例子写的 所以具体问题可能像楼上的几位 在dao的创建时可能除了问题
      

  8.   

    <value>com.org.entity.Register.hbm.xml</value>
    <value>com/org/entity/Photo.hbm.xml</value>
    这样写没问题吗?
      

  9.   

      <value>com.org.entity.Register.hbm.xml</value>应该是解析这个出问题的,改为///形式的
      

  10.   

    哦 这个是有问题 之前贴完之后看到的改过了 现在的代码没这个问题 而且依然报错 应该不是这段代码的问题吧
    新代码吧上面的哪行删掉了 <value>com/org/entity/Photo.hbm.xml</value>
      

  11.   


    恩 <value>com/org/entity/Photo.hbm.xml</value>这里是不对 ls的比较细心
      

  12.   

    总结一下 在test里 new个新的photodaoimpl的话 会报sessionfactory is required 
    如果是 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         
    PhotoDaoImpl photoDaoImpl = (PhotoDaoImpl) ac.getBean("PhotoDaoImpl");
    结果就是
    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is java.lang.IllegalArgumentException: At least one base package must be specified这个问题应该是applicationContext有问题还是缺包?
      

  13.   


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id=".......
    ...
    </bean>
    ....
    </beans>不是少beans了
      

  14.   

    你的applicationContext.xml 不全吧
      

  15.   

    完整的applicationContext.xml 看看哪里有问题?<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-autowire="byName" default-lazy-init="true">
    <context:component-scan base-package=""></context:component-scan>

     <!-- 引入参数配置文件 --> 
     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
     <property name="locations">   
      <list>    
      <value>jdbc.properties</value>   
      </list>  
     </property> 
     </bean>

    <!-- 配置数据库源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass"><value>${jdbc.driverClass}</value></property>
    <property name="pjdbcDriver"><value>${jdbc.driver}</value></property>
    <property name="jdbcUrl"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
    </bean>

    <!--配置SessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">   
             <ref bean="dataSource" />  
            </property>
            
    <property name="mappingResources">
    <list>
    <value>com/org/entity/Photo.hbm.xml</value>
    </list>
    </property>

    <property name="hibernateProperties">
    <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.hbm2ddl.auto=update
    hibernate.show_sql=true
    hibernate.formate_sql=false
    </value>
    </property>
    </bean>
    <!-- 事务管理 
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>--> <!-- hibernateTemplate -->

    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property> 
    </bean>-->
    <!-- 配置数据持久层 -->

    <bean id="PhotoDaoImpl" class="com.org.dao.impl.PhotoDaoImpl">
    <property name="sessionFactory" >
    <ref local="sessionFactory" />
    </property>
    </bean>
    </beans>
    是不是<context:component-scan base-package=""></context:component-scan>这里有问题? 该怎么写?
      

  16.   

    我改写<context:component-scan base-package="com.org.dao.impl"></context:component-scan>之后 显示缺c3p0包 我下载个c3p0包导入 开始显示我所有的bean的配置都是错误的 包括dataSourceorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PhotoDaoImpl' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'pjdbcDriver' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'pjdbcDriver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?