u010219209 2015-05-14 12:25 采纳率: 0%
浏览 3826

Spring bean注入 获取为空

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver">
    </property>
    <property name="url"
        value="jdbc:mysql://localhost:3306/db_xyd?useUnicode=true&amp;amp;amp;amp;amp;amp;characterEncoding=utf-8-">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="1234"></property>
</bean>

<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.SQLServerDialect-->
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/model/TDictionary.hbm.xml</value>
         </list>
    </property>
</bean>
<bean id="TDictionaryDAO" class="com.dao.TDictionaryDAO">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

<bean id="dictionaryAction" class="com.action.dictionaryAction" scope="prototype">
    <property name="dictionaryDAO">
        <ref bean="TDictionaryDAO" />
    </property>
</bean>

package com.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.model.TDictionary;

public class TDictionaryDAO extends HibernateDaoSupport
{
private static final Log log = LogFactory.getLog(TDictionaryDAO.class);
public void save(TDictionary transientInstance)
{
log.debug("saving TDictionary instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public static TDictionaryDAO getFromApplicationContext(ApplicationContext ctx) {
    return (TDictionaryDAO) ctx.getBean("TDictionaryDAO");
}

}

package com.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.dao.TDictionaryDAO;
import com.model.TDictionary;
import com.opensymphony.xwork2.ActionSupport;

public class dictionaryAction extends ActionSupport
{
private int OID;
private String willtake;

private TDictionaryDAO dictionaryDAO;

public String dictionaryAdd()
{   
    TDictionary dict=new TDictionary();
    dict.setOID(OID);
    dict.setL_key(100000);
    dict.setVc_value(willtake);
    dict.setVc_caption("vc_caption");

    //这里报空指针异常,是dictionaryDAO空
    dictionaryDAO.save(dict);
    return "success";
}

public TDictionaryDAO getDictionaryDAO()
{
    return dictionaryDAO;
}

public void setDictionaryDAO(TDictionaryDAO dictionaryDAO)
{
    this.dictionaryDAO = dictionaryDAO;
}

}

  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2015-05-15 00:55
    关注

    看配置没发现问题啊。关注中!

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料