sethackpro 2011-05-11 12:49
浏览 272
已采纳

Spring sessionFactory注入配置问题直接报容器错误,请大虾们看看

环境:spring 3.1 M2 + hibernate 3.6

web.xml
[code="xml"]<!-- 自动装配ApplicationContext的配置信息-->




org.springframework.web.context.ContextLoaderListener



<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>
        /WEB-INF/classes/conf/hibernate.cfg.xml,
        /WEB-INF/classes/conf/beans.xml
    </param-value>  
</context-param>  

<display-name>springDemo</display-name>
<servlet>
    <servlet-name>springDemo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>  
         <param-name>contextConfigLocation</param-name>
         <param-value>
         classpath*:conf/applicationContext.xml
         </param-value> 
     </init-param>
    <load-on-startup>1 </load-on-startup> 
</servlet>
<servlet-mapping>
    <servlet-name>springDemo</servlet-name>   
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>

<!--初始化log4j -->
<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>/WEB-INF/classes/conf/log4j.properties</param-value>  
</context-param>  

<context-param>  
    <param-name>log4jRefreshInterval</param-name>  
    <param-value>6000</param-value>  
</context-param>  
<listener>  
    <listener-class>  
        org.springframework.web.util.Log4jConfigListener  
    </listener-class>  
</listener>  

<!-- 防止内存泄漏,可选配置 -->
<listener>                  
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>[/code]

applicationContext.xml
[code="xml"]
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">


classpath:conf/jdbc.properties


<!-- Choose the dialect that matches your "dataSource" definition -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>${driverClassName}</value>
        <!--com.mysql.jdbc.Driver -->
    </property>
    <property name="url">
        <value>${url}</value>
        <!--jdbc:mysql://localhost:3306/example -->
    </property>
    <property name="username">
        <value>${username}</value>
        <!--root -->
    </property>
    <property name="password">
        <value>${password}</value>
        <!--1234 -->
    </property>
</bean>

<!-- 配置Hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>     

<!-- 配置事务 -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

<!-- 配置事务代理 -->
<bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
</bean>


<!-- Default ViewResolver 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView"/>
    <!-- 视图名称前缀和后缀 -->
    <property name="prefix" value="/web/"/>
    <property name="suffix" value=".jsp"></property>
</bean>

[/code]

/WEB-INF/classes/conf/hibernate.cfg.xml,
[code="xml"]



[/code]

beans.xml

[code="xml"]
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">


manager







<!-- 如果加上这个Dao的注入就会报错,后面的错语误



</bean>

 -->
<bean id="managerService" class="personal.learn.hibernate.service.ManagerService">
    <!-- <property name="managerDao" ref="managerDao"/> -->
</bean>[/code]

错误码:
[code="log"]
Log4j:WARN No appenders could be found for logger (org.apache.commons.digester.Digester.sax).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2011-5-11 12:44:32 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2011-5-11 12:44:32 org.apache.catalina.core.StandardContext start
严重: Context [/springDemo] startup failed due to previous errors
2011-5-11 12:44:32 org.apache.coyote.http11.Http11BaseProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080[/code]
ManagerDaoImpl.java
[code="java"]
public class ManagerDaoImpl extends HibernateDaoSupport implements ManagerDao {

@SuppressWarnings("unchecked")
public List<Manager> findAll() {

    Criteria cri = this.getSession().createCriteria(Manager.class);

    return this.getHibernateTemplate().find("Select * from Manager");

}

}[/code]
ManagerService.java
[code="java"]public class ManagerService extends BaseService{

private ManagerDao managerDao;

public ManagerDao getManagerDao() {
    return managerDao;
}

public void setManagerDao(ManagerDao managerDao) {
    this.managerDao = managerDao;
}


public List<Manager> findAll(){


    System.out.println("came here...");
    return null; 
    //return managerDao.findAll();
}

}[/code]
如果不注入DAO 可控制台可以输出 came here.. 说明service 没有错

  • 写回答

4条回答 默认 最新

  • cbcgkx 2011-05-11 15:37
    关注

    没有看到你web.xml的配置

    你的配置方式有点繁琐


    contextConfigLocation



    /WEB-INF/classes/conf/hibernate.cfg.xml,

    classpath*:applicationContext*.xml


    在给spring的配置文件命名的时候 最好是使用一个统一的前缀 方便管理 一般都是使用applicationContext为前缀

    这样就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记