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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问