newego2008
2010-10-17 07:22 阅读 446
已采纳

多数据源spring 报java.lang.NullPointerException

在调用test.java的时候就会报

Exception in thread "main" java.lang.NullPointerException
at cn.m_solution.mds.mds40.service.MDS40T01Service.getStudent(MDS40T01Service.java:23)
at cn.m_solution.mds.mds40.service.test.main(test.java:13)

请大家一定帮忙,已经弄了两天了

[color=red]moduleContext-sys.xml:[/color]

<?xml version="1.0" encoding="UTF-8" ?>

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.5.xsd">

<!-- +++++++++++++++++++++++++++++++++++++++++ -->
<!-- 系统关联设定(必须)                        -->
<!-- +++++++++++++++++++++++++++++++++++++++++ -->
<bean id="ApplicationInfo"
    class="net.uniuvo.webridger.beans.ApplicationInfo">

    <!-- 应用系统名称  -->
    <property name="applicationName" value="mds" />

    <!-- 允许直接访问的Action列表  -->
    <property name="directAccessActions">
        <list>
            <value>mds00.do</value>
            <value>/mds13.do</value>
            <value>/mds40.do</value>
        </list>
    </property>

    <!-- 禁止访问的扩展名列表  -->
    <property name="prohibitedExtensions">
        <list>
            <value>js</value>
            <value>css</value>
            <value>jsp</value>
            <value>html</value>
        </list>
    </property>

    <!-- 禁止访问的扩展名中例外的文件列表  -->
    <property name="restrictionEscapeFiles">
        <list>
            <value>/pages/index.html</value>
            <value>/pages/mds40/mds40s00.jsp</value>
        </list>
    </property>

</bean>

<!-- +++++++++++++++++++++++++++++++++++++++++ -->
<!-- 数据库关联设定(必须)                      -->
<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->   
<!-- +++++++++++++++++++++++++++++++++++++++++ -->
<!-- 这里定义了ibatis的配置文件所在的位置 -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>/WEB-INF/config/jdbc/jdbc.properties</value>
    </property>
</bean>

<!-- destroy-method="close">-->
destroy-method="close">




<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName1}" />
    <property name="url" value="${jdbc.url1}" />
    <property name="username" value="${jdbc.username1}" />
    <property name="password" value="${jdbc.password1}" />
</bean>









<!--这里就声明了具体的事务 -->
<bean id="attrSource"
    class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    <property name="properties">
        <props>
            <prop key="execute*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
            <prop key="select*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

<!-- 定义事务处理拦截器 -->
<bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager">
        <ref local="transactionManager" />
    </property>
    <property name="transactionAttributeSource">
        <ref local="attrSource" />
    </property>
</bean>

<!-- 定义事务经理 -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
        <ref local="DataSource" />
    </property>
</bean>

<!-- 自动代理 (业务处理Bean的Id) -->
<bean id="autoProxy"
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="interceptorNames">
        <list>
            <idref bean="transactionInterceptor" />
        </list>
    </property>
    <property name="beanNames">
        <list>
            <value>*Service</value>
        </list>
    </property>
</bean>

<!-- iBATIS相关设定 -->
<bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="/WEB-INF/sqlMapConfig.xml" />
    <property name="dataSource">
        <ref bean="DataSource" />
    </property>

    <property name="lobHandler">
        <ref local="oracleLobHandler" />
    </property>
</bean>

<!-- 定义DAO -->
<!-- 检索用DAO -->
<bean id="queryDAO" 
    class="net.uniuvo.webridger.dao.QueryDAOiBatisImpl">
    <property name="sqlMapClient">
        <ref local="sqlMapClient" />
    </property>
</bean>

<!-- 追加 更新 删除用DAO -->
<bean id="updateDAO"
    class="net.uniuvo.webridger.dao.UpdateDAOiBatisImpl">
    <property name="sqlMapClient">
        <ref local="sqlMapClient" />
    </property>
</bean>

<!--根据sqlMapClien创建一个SqlMapClient模版类-->
<bean id="SqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient">
        <ref bean="sqlMapClient" />
    </property>
</bean>

<bean id="nativeJdbcExtractor" lazy-init="true"
    class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" />

<bean id="oracleLobHandler" lazy-init="true"
    class="org.springframework.jdbc.support.lob.OracleLobHandler" >
    <property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
</bean>

<!-- 这里就是Facade了,注意,它有个parent属性,就是这个属性,使它的操作受到了事物管理的限制
<bean id="baseFacade" parent="baseTransactionProxy">
    <property name="target">
        <bean class="facade.BaseFacadeImpl"> 把DAO做为属性注入到Facade中去
            <property name="proDao" ref="proDao" />
            <property name="errDao" ref="errDao" />
            <property name="userDao" ref="userDao" />
            <property name="orderDao" ref="orderDao" />
        </bean>
    </property>
</bean>
 -->

[color=red]sqlmapconfig.xml[/color]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">


<sqlMap resource="../config/sql/sqlMap-mds.xml" />
<sqlMap resource="../config/sql/sqlMap-mds00.xml" />
<sqlMap resource="../config/sql/sqlMap-mds01.xml" />
<sqlMap resource="../config/sql/sqlMap-mds02.xml" />
<sqlMap resource="../config/sql/sqlMap-mds03.xml" />
<sqlMap resource="../config/sql/sqlMap-mds04.xml" />
<sqlMap resource="../config/sql/sqlMap-mds10.xml" />
<sqlMap resource="../config/sql/sqlMap-mds11.xml" />
<sqlMap resource="../config/sql/sqlMap-mds12.xml" />
<sqlMap resource="../config/sql/sqlMap-mds13.xml" />
<sqlMap resource="../config/sql/sqlMap-mds20.xml" />
<sqlMap resource="../config/sql/sqlMap-mds21.xml" />
<sqlMap resource="../config/sql/sqlMap-mds22.xml" />
<sqlMap resource="../config/sql/sqlMap-mds23.xml" /> 
<sqlMap resource="../config/sql/sqlMap-mds30.xml" /> 
<sqlMap resource="../config/sql/sqlMap-mds31.xml" />
<sqlMap resource="../config/sql/sqlMap-mds32.xml" />
<sqlMap resource="../config/sql/sqlMap-mds33.xml" />
<sqlMap resource="../config/sql/sqlMap-mds40.xml" />

sqlMap-mds40.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<select id="sql.mds40.001" 
    parameterClass="cn.m_solution.mds.mds40.form.MDS40Form" 
    resultClass="cn.m_solution.mds.mds40.form.MDS40Form">
     select 
        syr
    from 
        vehicle
    where 
        hpzl = #hpzl#
        and hphm = #hphm#
</select>

MDS40T01Service

package cn.m_solution.mds.mds40.service;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import cn.m_solution.mds.mds40.form.MDS40Form;

public class MDS40T01Service {
SqlMapClientTemplate sqlMapClientTemplate;

public MDS40Form getStudent(){

    MDS40Form form40 = new MDS40Form();
    sqlMapClientTemplate.queryForObject("sql.mds40.001",form40);
    return form40;
}
/**
 * @return the sqlMapClientTemplate
 */
public SqlMapClientTemplate getSqlMapClientTemplate() {
    return sqlMapClientTemplate;
}

/**
 * @param sqlMapClientTemplate the sqlMapClientTemplate to set
 */
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
    this.sqlMapClientTemplate = sqlMapClientTemplate;
}

}

test.java

package cn.m_solution.mds.mds40.service;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class test extends JdbcDaoSupport{

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    MDS40T01Service s = new MDS40T01Service();
    s.getStudent();
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    zgy52188 zgy52188 2010-10-18 09:39

    [quote]public MDS40Form getStudent(){

    MDS40Form form40 = new MDS40Form();
    sqlMapClientTemplate.queryForObject("sql.mds40.001",form40);
    return form40;
    } [/quote]
    这个方法是用来调用数据的对吧?你在这new了一个对象,那么对象是什么?空的对象。
    在看这,
    [quote] parameterClass="cn.m_solution.mds.mds40.form.MDS40Form"
    resultClass="cn.m_solution.mds.mds40.form.MDS40Form">
    select
    syr
    from
    vehicle
    where
    hpzl = #hpzl#
    and hphm = #hphm#
    [/quote]
    这里的parameterClass是要对象参数的。在sql语句中有用到吧。可是你现在在类中给的是空的字符串,你说能查出东西吗?既然没东西返回一定是空值。报空指针异常。

    点赞 评论 复制链接分享
  • Total_L Total_L 2010-10-17 10:02

    MDS40T01Service 怎么是new出来的,不是loadbean load出来的?

    点赞 评论 复制链接分享
  • Total_L Total_L 2010-10-17 17:45

    [quote]我就是想通过一个main方法直接调用service来进行查询!
    [/quote]
    Servie要load出来,不能new出来,不然spring怎么把temple inject进去?

    点赞 评论 复制链接分享
  • iteye_4873 iteye_4873 2010-10-21 10:00

    sqlMapClientTemplate 没有注入~ 所以是空的,
    如果你非得这么测试的话,先
    [code="java"]
    ApplicationContext context = new FileSystemXmlApplicationContext("application.xml");

    [/code]然后获得Bean中的
    SqlMapClientTemplate
    [code="java"]
    SqlMapClientTemplate sqlMap = (SqlMapClientTemplate )context.getBean("SqlMapClientTemplate");
    [/code]
    然后
    [code="java"]
    MDS40T01Service s = new MDS40T01Service();
    s.setSqlMapClientTemplate(sqlMap);
    s.getStudent();
    [/code]
    这样应该就不会报空了~ 试试

    点赞 评论 复制链接分享

相关推荐