Miracle_Qsc 2011-10-28 15:49
浏览 236
已采纳

spring mvc 整合hibernate 菜鸟级问题

小弟想用spring mvc 整合hibernate 但是spring总是如法注入。 spring配置文件如下
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">


classpath:jdbc.properties


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>${datasource.driverClassName}</value>
    </property>
    <property name="jdbcUrl">
        <value>${datasource.url}</value>
    </property>
    <property name="user">
        <value>${datasource.username}</value>
    </property>
    <property name="password">
        <value>${datasource.password}</value>
    </property>
    <property name="acquireIncrement">
        <value>${c3p0.acquireIncrement}</value>
    </property>
    <property name="initialPoolSize">
        <value>${c3p0.initialPoolSize}</value>
    </property>
    <property name="minPoolSize">
        <value>${c3p0.minPoolSize}</value>
    </property>
    <property name="maxPoolSize">
        <value>${c3p0.maxPoolSize}</value>
    </property>
    <property name="maxIdleTime">
        <value>${c3p0.maxIdleTime}</value>
    </property>
    <property name="idleConnectionTestPeriod">
        <value>${c3p0.idleConnectionTestPeriod}</value>
    </property>
    <property name="maxStatements">
        <value>${c3p0.maxStatements}</value>
    </property>
    <property name="numHelperThreads">
        <value>${c3p0.numHelperThreads}</value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
</bean>

<bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"></property>
    <property name="transactionAttributes">
        <props>
            <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="mappingResources">
        <list>
            <value>classpath:ac02.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
</bean>

 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 

<bean id="QscDBService" class ="com.qsc.m.QscDBServiceImpl">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="ControllerJoin" class="com.qsc.c.ControllerJoin">
    <property name="QscDBService" ref="QscDBService"></property>
</bean>

[/code]
QscDBservice类如下
[code="java"]
package com.qsc.m;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.qsc.pojo.ac02;

public class QscDBServiceImpl extends JdbcTemplate implements QscDBService {

private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
    return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
}

public void sava(ac02 ac02) {
hibernateTemplate.save(ac02);
}
[/code]
ControllerJoin类如下
[code="java"]
package com.qsc.c;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.qsc.m.QscDBService;
import com.qsc.pojo.ac02;
import com.qsc.spring.ContextLoaderListener;

public class ControllerJoin implements Controller {
private QscDBService qsc;

public QscDBService getQsc() {
    return qsc;
}

public void setQsc(QscDBService qsc) {
    this.qsc = qsc;
}

public ModelAndView handleRequest(HttpServletRequest arg0,
        HttpServletResponse arg1) throws Exception {
    //final QscDBService qsc = (QscDBService)ContextLoaderListener.getBean("QscDBService");

    ac02 ac02 = new ac02();
    String aac001 = arg0.getParameter("account");
    String aac002 = arg0.getParameter("name");
    String aac003 = arg0.getParameter("age");
    String aac004 = arg0.getParameter("email");
    String aac005 = arg0.getParameter("phone");
    String aac006 = arg0.getParameter("pass");
    ac02.setAac001(aac001);
    ac02.setAac002(aac002);
    ac02.setAac003(Integer.parseInt(aac003));
    ac02.setAac004(aac004);
    ac02.setAac005(aac005);
    ac02.setAac006(aac006);
    qsc.sava(ac02);
    return new ModelAndView("success");
}

}
[/code]
debug后发现 qsc和hibernatetemplate 这两个全是null 说明spring没注入成功,这是为啥啊?
谢谢

  • 写回答

1条回答 默认 最新

  • songze86 2011-10-28 16:47
    关注

    注入的名字要和配置文件中一致 比如qsc,你在

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?