ibatis+spring集成问题如何解决:-(

dingding5060 2010-01-14 02:43:07
小弟最近在做struts+ibatis+spring集成的时候都能成功,但是在做一个功能的时候遇到了点小麻烦,描述如下:

在系统启动的时候我想调用ibatis的SqlMapClient对象去查询数据库,加载一些数据到内存中,而不是通过struts页面跳转,通过action触发并通过spring的注入得到SqlMapClient对象并操作数据的,我现在只是想通过一个普通的类DictionaryInit来加载一些数据,在这里用到的就只有ibatis+spring,但是我总是得不到SqlMapClient的实例,配置感觉没有什么不正确的地方,各代码如下:

DictionaryInit.java源代码:



package org.dhf.bbw.init;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.dhf.bbw.admin.dictionary.bean.Dictionary;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ibatis.sqlmap.client.SqlMapClient;

public class DictionaryInit {//extends SqlMapClientDaoSupport {//想通过继承SqlMapClientDaoSupport来得到SqlMapClient对象,也没能成功,总是空指针异常

/**
* 获得Log4j日志处理类实例
*/
private Logger logger = Logger.getLogger(DictionaryInit.class);

/**
* 单例模式
*/
private static DictionaryInit dicInit = new DictionaryInit();

private DictionaryInit(){

}

public static DictionaryInit getInstance(){
return dicInit;
}

protected SqlMapClient sqlMapClient;// = getSqlMapClientTemplate().getSqlMapClient();

public List getAllParentNodetionary() {

// ApplicationContext context = new ClassPathXmlApplicationContext("../applicationContext-dataSource.xml");
// sqlMapClient = (SqlMapClient)context.getBean("sqlMapClient");

List<Dictionary> reslutList = null;
try {
reslutList = sqlMapClient.queryForList("getAllParentNodetionary");
} catch (SQLException e) {
//打印错误日志
logger.error("查询数据字典根节点出错:" + e.toString());
}
return reslutList;
}

public List getDictionaryByParentNode(int parentNode) {
List<Dictionary> reslutList = null;
try {
reslutList = sqlMapClient.queryForList("getDictionaryByParentNode", parentNode);
} catch (SQLException e) {
//打印错误日志
logger.error("查询数据字典子节点出错:" + e.toString());
}
return reslutList;
}

public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}




applicationContext-dataSource.xml配置如下:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:BBW</value>
</property>
<property name="username">
<value>bbw</value>
</property>
<property name="password">
<value>bbw</value>
</property>
</bean>

<!-- Spring iBatis SqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- Spring iBatisTemplate -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>




applicationContext.xml配置代码:



<bean id="dictionaryInit" class="org.dhf.bbw.init.DictionaryInit">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>




sqlmap-config.xml配置如下:



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="org/dhf/bbw/admin/dictionary/bean/Dictionary.xml"/>
</sqlMapConfig>



Dictionary.xml配置如下:



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

<typeAlias alias="dictionary" type="org.dhf.bbw.admin.dictionary.bean.Dictionary"/>

<resultMap id="DictionaryResult" class="dictionary">
<result property="recid" column="recid"/>
<result property="parentNode" column="parentNode" select="getDictionaryByDicID"/>
<result property="name" column="name"/>
<result property="remark" column="remark"/>
<result property="state" column="state"/>
</resultMap>

<select id="getAllParentNodetionary" resultMap="DictionaryResult">
select * from BBW_Dictionary where parentNode is null
</select>

<select id="getDictionaryByDicID" parameterClass="int" resultMap="DictionaryResult">
select * from BBW_Dictionary where recid = #value#
</select>

<select id="getDictionaryByParentNode" parameterClass="int" resultMap="DictionaryResult">
select * from BBW_Dictionary where parentNode = #value#
</select>

</sqlMap>




不知道是什么问题,总是得不到SqlMapClient对象,就只是少了struts的action的触发就不行了!跪拜...谢谢
...全文
766 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingding5060 2010-01-19
  • 打赏
  • 举报
回复
非常感谢各位的帮助,结贴啦!...(*^__^*)
dingding5060 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 warison2008 的回复:]
config对象为null。

config只有由servlet容器(也就是tomcat等应用服务器)调用init方法才能得到正确的config,而且只会调用一次。

你把SystemInitServlet取消托管,然后参考http://www.csip.com.cn/42/n-19242.html在SystemInitServlet访问spring容器,并获得dictionaryInit对象
[/Quote]




java.lang.NullPointerException
at org.dhf.bbw.init.SystemInitServlet.init(SystemInitServlet.java:60)



这里报错的SystemInitServlet的60行不是config对象为NULL,是DictionaryInit dicInit对象为NULL,呵呵,又学习了一种得到从spring容器管理中得到bean的方法:


WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

IWorksService worksService=(IWorksService)wac.getBean("worksService");



非常感谢!呵呵!
道光2008 2010-01-18
  • 打赏
  • 举报
回复
你吧 sqlMapClient 改下,参考:<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

sql-map-config.xml放在:/WEB-INF/CLASSES/sql-map-config.xml.
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 apollo_ts 的回复:]
主要是要把握生命周期,比如action中的构造方法就不能用servlet api主要是servlet上下文还没有注入,你的也一样,把握注入的生命周期时刻就行了。
[/Quote]


呵呵,非常感谢,能否就小弟这个问题给个详细的解释啊?我去网上查了些相关注入的生命周期的资料...希望仁兄给点意见!谢谢 O(∩_∩)O~
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 e332424234e 的回复:]
BaseTest

Java code
@ContextConfiguration(locations= {"classpath:/spring/applicationContext.xml" })
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)publicclass BaseTestextends AbstractTransactionalJUnit4SpringContextTests{

}


[/Quote]


这位大哥,这个是不是写的一个测试程序啊?我的报错如下:

ContextConfiguration cannot be resolved to a type
TransactionConfiguration cannot be resolved to a type
AbstractTransactionalJUnit4SpringContextTests cannot be resolved to a type


这些是要配置什么吧?我去网上找了些有关事务的,但是配置有点小问题,能否给个完整的配置,非常感谢!
BearKin 2010-01-18
  • 打赏
  • 举报
回复
不会 帮顶
careers1111 2010-01-18
  • 打赏
  • 举报
回复
学习下
APOLLO_TS 2010-01-18
  • 打赏
  • 举报
回复

主要是要把握生命周期,比如action中的构造方法就不能用servlet api主要是servlet上下文还没有注入,你的也一样,把握注入的生命周期时刻就行了。
阿士匹灵 2010-01-18
  • 打赏
  • 举报
回复
学习
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhang19870928 的回复:]
  struts1  中 struts-config.xml配置文件呢 action 的type="org.springframework.web.struts.DelegatingActionProxy"

struts2  struts.xml文件 <constant name="struts.objectFactory" value="spring"> </constant>

我就没有看出你代码有struts代码啊
[/Quote]


我的struts代码是写在struts.properties中的,如下:



struts.custom.i18n.resources = messageResourcesOfBBW_admin,messageResourcesOfBBW_user
struts.il8n.encoding = UTF-8
struts.locale = UTF-8
struts.il8n.reload = true
struts.enable.DynamicMethodInvocation = true
struts.devMode = false
struts.objectFactory.spring.autoWire = type
struts.configuration.xml.reload=true
struts.objectFactory = spring
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 imasmallbird 的回复:]
你在web.xml中是怎么配置的呀??
你需要把spring整合加载呀~~

[/Quote]


web.xml配置如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Blank</display-name>

<!-- page request filter add by dhf 2010-1-5 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.dhf.bbw.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml
</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<welcome-file-list>
<welcome-file>/WEB-INF/admin/index.jsp</welcome-file>
</welcome-file-list>

<!-- init servlet -->
<servlet>
<servlet-name>SystemInitServlet</servlet-name>
<servlet-class>org.dhf.bbw.init.SystemInitServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>

</web-app>




我是在SystemInitServlet类中调用DictionaryInit的方法加载数据的!
道光2008 2010-01-18
  • 打赏
  • 举报
回复
config对象为null。

config只有由servlet容器(也就是tomcat等应用服务器)调用init方法才能得到正确的config,而且只会调用一次。

你把SystemInitServlet取消托管,然后参考http://www.csip.com.cn/42/n-19242.html在SystemInitServlet访问spring容器,并获得dictionaryInit对象
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 warison2008 的回复:]
DictionaryInit dicInit = DictionaryInit.getInstance();
我晕死,你的DictionaryInit 是自己创建的啊,没有交给spring管理?
那当然不能使用spring来注入DictionaryInit 的sqlMapClient啊
[/Quote]



按照你的方法,我又改了一下:

SystemInitServlet.java代码如下:



package org.dhf.bbw.init;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dhf.bbw.admin.dictionary.bean.Dictionary;

/**
* 在系统启动时加载部分数据供系统使用
* @author dhf in 2010-1-18
*
*/
public class SystemInitServlet extends HttpServlet {

/**
* 获得Log4j日志处理类实例
*/
private Logger logger = Logger.getLogger(SystemInitServlet.class);

/**
* 定义application 对象
*/
public ServletContext servletContext = null;

protected DictionaryInit dicInit;

public DictionaryInit getDicInit() {
return dicInit;
}

public void setDicInit(DictionaryInit dicInit) {
this.dicInit = dicInit;
}

/**
* Initialize global variables
*
* @throws ServletException
* servlet异常处理
*/
public void init(ServletConfig config) throws ServletException {
logger.info("----------------------init start.............");
// 执行读取sql.xml
//DbSQLInit.init();
//加载数据字典
//得到application对象
servletContext = config.getServletContext();
//利用单例模式得到DictionaryInit对象
//DictionaryInit dicInit = DictionaryInit.getInstance();
//查询所有数据字典配置
List<Dictionary> dicList = dicInit.getAllParentNodetionary(servletContext);
//如果没有则打印错误日志
if (dicList == null) {
logger.error("数据字典初始化失败!");
}else {
//循环结果集得到子节点集合
for (Iterator iter = dicList.iterator(); iter.hasNext();) {
Dictionary fatherDic = (Dictionary) iter.next();
int recid = fatherDic.getRecid();
//根据父节点ID得到其下所有子节点以list对象集合返回
List<Dictionary> childList = dicInit.getDictionaryByParentNode(recid, servletContext);
//如果没有子节点打印日志
if (childList == null) {
logger.error("没有找到相应的子节点!");
}else {
//将得到的数据字典配置信息循环放入application范围,供页面使用
servletContext.setAttribute(fatherDic.getRemark(), childList);
}
}
}

logger.info("----------------------init end...............");
}

/**
* Process the HTTP Get request
*
* @param request
* HttpServletRequest请求
* @param response
* HttpServletResponse响应对象
* @throws ServletException
* servlet异常处理
* @throws IOException
* 文件异常处理
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

/**
* Process the HTTP post request
*
* @param request
* HttpServletRequest请求
* @param response
* HttpServletResponse响应对象
* @throws ServletException
* servlet异常处理
* @throws IOException
* 文件异常处理
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

/**
* Clean up resources
*/
public void destroy() {
}

}



增加applicationContext.xml中配置如下:


<bean id="dictionaryInit" class="org.dhf.bbw.init.DictionaryInit">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>

<bean id="SystemInitServlet" class="org.dhf.bbw.init.SystemInitServlet">
<property name="dicInit">
<ref bean="dictionaryInit"/>
</property>
</bean>



报错如下:



2010-01-18 17:47:27,750 INFO (org.apache.struts2.spring.StrutsSpringObjectFactory:83) - ... initialized Struts-Spring integration successfully
2010-01-18 17:47:27,781 ERROR (org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/bbw]:675) - StandardWrapper.Throwable
java.lang.NullPointerException
at org.dhf.bbw.init.SystemInitServlet.init(SystemInitServlet.java:60)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:932)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3917)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4201)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
2010-01-18 17:47:27,796 ERROR (org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/bbw]:3919) - Servlet /bbw threw load() exception
java.lang.NullPointerException
at org.dhf.bbw.init.SystemInitServlet.init(SystemInitServlet.java:60)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:932)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3917)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4201)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
2010-1-18 17:47:27 org.apache.coyote.http11.Http11BaseProtocol start
信息: Starting Coyote HTTP/1.1 on http-8888
2010-1-18 17:47:28 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2010-1-18 17:47:28 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/62 config=null
2010-1-18 17:47:28 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2010-1-18 17:47:28 org.apache.catalina.startup.Catalina start
信息: Server startup in 5687 ms



感觉出现连环效应了....:-(
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 warison2008 的回复:]
你这样写只能通过

//通过servlet的ServletConfig得到spring容器对象
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        SqlMapClient sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");获得sqlMapClient 啦

[/Quote]



恩,这种方法是可以解决了,但是我就是搞不明白,为什么那种方式不能行?
道光2008 2010-01-18
  • 打赏
  • 举报
回复
你这样写只能通过

//通过servlet的ServletConfig得到spring容器对象
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
SqlMapClient sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");获得sqlMapClient 啦
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 warison2008 的回复:]
DictionaryInit dicInit = DictionaryInit.getInstance();
我晕死,你的DictionaryInit 是自己创建的啊,没有交给spring管理?
那当然不能使用spring来注入DictionaryInit 的sqlMapClient啊
[/Quote]


我有试过用spring来管理DictionaryInit 的,但是也是不行...所以我才搞成这样的!O(∩_∩)O~
道光2008 2010-01-18
  • 打赏
  • 举报
回复
DictionaryInit dicInit = DictionaryInit.getInstance();
我晕死,你的DictionaryInit 是自己创建的啊,没有交给spring管理?
那当然不能使用spring来注入DictionaryInit 的sqlMapClient啊
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 warison2008 的回复:]
对了你的SystemInitServlet是怎么调用DictionaryInit的
[/Quote]


SystemInitServlet.java代码如下:




package org.dhf.bbw.init;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dhf.bbw.admin.dictionary.bean.Dictionary;

/**
* 在系统启动时加载部分数据供系统使用
* @author dhf in 2010-1-18
*
*/
public class SystemInitServlet extends HttpServlet {

/**
* 获得Log4j日志处理类实例
*/
private Logger logger = Logger.getLogger(SystemInitServlet.class);

/**
* 定义application 对象
*/
public ServletContext servletContext = null;

/**
* Initialize global variables
*
* @throws ServletException
* servlet异常处理
*/
public void init(ServletConfig config) throws ServletException {
logger.info("----------------------init start.............");
// 执行读取sql.xml
//DbSQLInit.init();
//加载数据字典
//得到application对象
servletContext = config.getServletContext();
//利用单例模式得到DictionaryInit对象
DictionaryInit dicInit = DictionaryInit.getInstance();
//查询所有数据字典配置
List<Dictionary> dicList = dicInit.getAllParentNodetionary(servletContext);
//如果没有则打印错误日志
if (dicList == null) {
logger.error("数据字典初始化失败!");
}else {
//循环结果集得到子节点集合
for (Iterator iter = dicList.iterator(); iter.hasNext();) {
Dictionary fatherDic = (Dictionary) iter.next();
int recid = fatherDic.getRecid();
//根据父节点ID得到其下所有子节点以list对象集合返回
List<Dictionary> childList = dicInit.getDictionaryByParentNode(recid, servletContext);
//如果没有子节点打印日志
if (childList == null) {
logger.error("没有找到相应的子节点!");
}else {
//将得到的数据字典配置信息循环放入application范围,供页面使用
servletContext.setAttribute(fatherDic.getRemark(), childList);
}
}
}

logger.info("----------------------init end...............");
}

/**
* Process the HTTP Get request
*
* @param request
* HttpServletRequest请求
* @param response
* HttpServletResponse响应对象
* @throws ServletException
* servlet异常处理
* @throws IOException
* 文件异常处理
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

/**
* Process the HTTP post request
*
* @param request
* HttpServletRequest请求
* @param response
* HttpServletResponse响应对象
* @throws ServletException
* servlet异常处理
* @throws IOException
* 文件异常处理
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

/**
* Clean up resources
*/
public void destroy() {
}

}





仁兄,帮忙好好瞄瞄,这个问题折磨我好久了...虽然现在有了比较笨的解决办法,但是我感觉这种配置实现方法总是哪里出现配置问题了才这样的...拜谢了 O(∩_∩)O~
dingding5060 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 warison2008 的回复:]
改:
private SqlMapClient sqlMapClient;// = getSqlMapClientTemplate().getSqlMapClient();
并设置
get,set方法看看
[/Quote]


按照你的方法改的,我本来有set方法,按照你的意思加了get方法,现在DictionaryInit.java代码如下:



package org.dhf.bbw.init;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.dhf.bbw.admin.dictionary.bean.Dictionary;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
* 数据字典加载
* @author dhf in 2010-1-18
*
*/
public class DictionaryInit {

/**
* 获得Log4j日志处理类实例
*/
private Logger logger = Logger.getLogger(DictionaryInit.class);

/**
* 单例模式
*/
private static DictionaryInit dicInit = new DictionaryInit();

private DictionaryInit(){

}

public static DictionaryInit getInstance(){
return dicInit;
}

/**
* ibatis的SqlMapClient对象
*/
private SqlMapClient sqlMapClient;

/**
* 得到所有的数据字典配置
* @param servletContext servlet的ServletContext对象
* @return 返回所有的数据库字典配置List对象集合
*/
public List getAllParentNodetionary(ServletContext servletContext) {
//从spring容器中得到SqlMapClient对象实例
// getSqlMapClient(servletContext);
List<Dictionary> reslutList = null;
try {
reslutList = sqlMapClient.queryForList("getAllParentNodetionary");
} catch (SQLException e) {
//打印错误日志
logger.error("查询数据字典根节点出错:" + e.toString());
}
return reslutList;
}

/**
* 根据给定的父节点parentNode返回该节点下的所有子节点List
* @param parentNode 给定的父节点id
* @param servletContext servlet的ServletContext对象
* @return 返回子节点List对象集合
*/
public List getDictionaryByParentNode(int parentNode, ServletContext servletContext) {
//从spring容器中得到SqlMapClient对象实例
// getSqlMapClient(servletContext);
List<Dictionary> reslutList = null;
try {
reslutList = sqlMapClient.queryForList("getDictionaryByParentNode", parentNode);
} catch (SQLException e) {
//打印错误日志
logger.error("查询数据字典子节点出错:" + e.toString());
}
return reslutList;
}

/**
* 重写SqlMapClient属性的get方法,加入参数ServletContext
* @param servletContext servlet的ServletContext对象
* @return 返回从spring容器中得到的SqlMapClient对象
*/
public SqlMapClient getSqlMapClient(ServletContext servletContext) {
//通过servlet的ServletConfig得到spring容器对象
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
return sqlMapClient;
}

/**
* SqlMapClient的set方法,供spring容易注入使用
* @param sqlMapClient
*/
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}

public SqlMapClient getSqlMapClient() {
return sqlMapClient;
}

}



错误还是存在....一样的错误!
道光2008 2010-01-18
  • 打赏
  • 举报
回复
对了你的SystemInitServlet是怎么调用DictionaryInit的
加载更多回复(15)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧