- 浏览: 5112 次
- 来自: ...
文章分类
最新评论
Spring+hibernate+dwr(1.14),location参数导致重复装载Hbm文件探讨。
近来,项目用到了dwr做ajax的应用。有点不爽的是,
1.使用了多个Bean文件,在Web.xml里定义顺序为application1.xml,applicaiton2.xml,applicaiton3.xml,
导致Spring在装载这些bean文件定义时,当前上下文里只会有一个bean定义文件,application1.xml.
其余的bean文件定义是放在ParentContext下。
2.通过WebContext取BeanFactory,只能是取第一个bean文件定义的工厂;
其他bean文件的bean只能是通过xmlpath的方式获取。
3.spring+dwr结合时,需在dwr.xml里定义creator的value是spring。由于dwr默认使用的spring
上下文工厂,当dwr文件里有其他bean文件定义的bean对象时,将会导致这些非第一个bean文件如:
(applicaiton2.xml,applicaiton3.xml)定义的bean对象在Dwr里不起作用。
4.dwr官方的方法是在dwr.xml文件里加入类似
的参数定义,如果大部分的bean对象是在第一个bean文件里定义的,处理还算可以。
如果有10多个bean对象不是在第一个bean文件里,加入一堆的location参数,每个bean就得装载一次
,在很多时候将会导致启动时发生OutofMemery的异常。
5.此时的方法有二,一是调整启动的容器内存参数,把内存调大。二,更改dwr的location参数装载方式。
6,最理想的方式,从parentContext里取得beanFactory;其次,在装载的时候就把读入bean定义
文件,在内存存储这些beanfactory对象,以备当使用了location参数时使用。
这样就可以一点程度上避免重复装载着启动时消耗过多的内存,加快容器部署速度。
以下是我更新DWR的location参数的代码
在uk.ltd.getahead.dwr.util包里新增一个类SpringLocationUtil
[code="java"]/*
* Copyright 2009 Jandytom
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ClassDescription
* This class help people who want to use Spring bean file location param
* aware cycle build the bean defined xml file.
*/
package uk.ltd.getahead.dwr.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringLocationUtil {
private static SpringLocationUtil locationUtil = null;
private InputStream in;
private Map beanFactoryMap;
private Map beanFileMap;
private SpringLocationUtil(){
this.setBeanFileMap(new HashMap());
this.setBeanFactoryMap(new HashMap());
}
public synchronized static SpringLocationUtil getInstance(){
if(locationUtil==null){
locationUtil = new SpringLocationUtil();
}
return locationUtil;
}
class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
log.debug("Attibute:---" + node.getName() + "="+ node.getValue());
}
public void visit(Element node) {
if (node.isTextOnly()) {
String nodeName = node.getName().trim();
if(nodeName.equals("param")){
String atrNameValue = node.attributeValue("name");
if(atrNameValue!=null&&atrNameValue.startsWith("location")){
String beaFileName = node.attributeValue("value");
if(beaFileName!=null){
beanFileMap.put(atrNameValue, beaFileName);
}
}
}
log.debug("Element:---" + node.getName() + "=" + node.getText());
}else{
log.debug("--------" + node.getName() + "-------");
}
}
public void visit(ProcessingInstruction node) {
log.debug("PI:"+node.getTarget()+" "+node.getText());
}
}
public void InitBeanFactoryMap(InputStream in){
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(in);
doc.accept(new MyVistor());
Map beanMap = new HashMap();
if(beanFileMap!=null&&beanFileMap.size()>0){
for (Iterator it = beanFileMap.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Entry) it.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
BeanFactory factory = new ClassPathXmlApplicationContext(String.valueOf(value));
beanMap.put(value, factory);
}
this.setBeanFactoryMap(beanMap);
}else{
log.info("location param not found .");
}
} catch (DocumentException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
log.error("Close InputStream Error:"+e.getMessage());
}
}
}
}
public static void main(String args[]){
java.io.File file = new java.io.File("D:\\work\\eclipse\\workspaceAll\\Test\\WebRoot\\WEB-INF\\dwr.xml");
try {
InputStream in = new java.io.FileInputStream(file);
SpringLocationUtil.getInstance().InitBeanFactoryMap(in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public InputStream getIn() {
return in;
}
public void setIn(InputStream in) {
this.in = in;
}
public Map getBeanFactoryMap() {
return beanFactoryMap;
}
public void setBeanFactoryMap(Map beanFactoryMap) {
this.beanFactoryMap = beanFactoryMap;
}
public Map getBeanFileMap() {
return beanFileMap;
}
public void setBeanFileMap(Map beanFileMap) {
this.beanFileMap = beanFileMap;
}
/**
* The log stream
*/
private static final Logger log = Logger.getLogger(SpringLocationUtil.class);
}
然后,更改AbstractDWRServlet,更新后的Servlet init()方法如下,红色部分为修改内容
[code="java"] public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
ServletLoggingOutput.setExecutionContext(this);
// How much logging do we do?
String logLevel = config.getInitParameter(INIT_LOGLEVEL);
if (logLevel != null)
{
ServletLoggingOutput.setLevel(logLevel);
}
// call the abstract method to obtain the container
container = getContainer(config);
// Now we have set the implementations we can set the WebContext up
builder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
WebContextFactory.setWebContextBuilder(builder);
// And we lace it with the context so far to help init go smoothly
builder.set(null, null, getServletConfig(), getServletContext(), container);
// Load the system config file
Configuration configuration = (Configuration) container.getBean(Configuration.class.getName());
InputStream in = getClass().getResourceAsStream(FILE_DWR_XML);
log.info("retrieved system configuration file: " + in); //$NON-NLS-1$
InputStream inPlug = config.getServletContext().getResourceAsStream(DEFAULT_DWR_XML);
try
{ SpringLocationUtil.getInstance().InitBeanFactoryMap(inPlug);
configuration.addConfig(in);
}
catch (Exception ex)
{
log.fatal("Failed to load system config file from dwr.jar", ex); //$NON-NLS-1$
throw new ServletException(Messages.getString("DWRServlet.SystemConfigError"), ex); //$NON-NLS-1$
}
// call the abstract method to perform additional configuration
configure(config, configuration);
// Finally the processor that handles doGet and doPost
processor = (Processor) container.getBean(Processor.class.getName());
}
catch (ServletException ex)
{
throw ex;
}
catch (Exception ex)
{
log.fatal("init failed", ex); //$NON-NLS-1$
throw new ServletException(ex);
}
finally
{
if (builder != null)
{
builder.unset();
}
ServletLoggingOutput.unsetExecutionContext();
}
}
更新uk.ltd.getahead.dwr.create.SpringCreator的getBeanFactory()方法
[code="java"] /**
* @return A found BeanFactory configuration
*/
private BeanFactory getBeanFactory()
{
// If someone has set a resource name then we need to load that.
if (configLocation != null && configLocation.length > 0)
{
log.info("Spring BeanFactory via ClassPathXmlApplicationContext using " + configLocation.length + "configLocations."); //$NON-NLS-1$ //$NON-NLS-2$
return (BeanFactory) SpringLocationUtil.getInstance().getBeanFactoryMap().get(configLocation[0]);
//return new ClassPathXmlApplicationContext(configLocation);
}
ServletContext srvCtx = WebContextFactory.get().getServletContext();
HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
if (request != null)
{
return RequestContextUtils.getWebApplicationContext(request, srvCtx);
}
else
{
return WebApplicationContextUtils.getWebApplicationContext(srvCtx);
}
}
编译后更新相关的Class即可。这个对使用location参数多的应用效果比较明显。
注:改动后的jar包只在Tomcat5.0.28下做过测试,其他容器未做测试。
近来,项目用到了dwr做ajax的应用。有点不爽的是,
1.使用了多个Bean文件,在Web.xml里定义顺序为application1.xml,applicaiton2.xml,applicaiton3.xml,
导致Spring在装载这些bean文件定义时,当前上下文里只会有一个bean定义文件,application1.xml.
其余的bean文件定义是放在ParentContext下。
2.通过WebContext取BeanFactory,只能是取第一个bean文件定义的工厂;
其他bean文件的bean只能是通过xmlpath的方式获取。
3.spring+dwr结合时,需在dwr.xml里定义creator的value是spring。由于dwr默认使用的spring
上下文工厂,当dwr文件里有其他bean文件定义的bean对象时,将会导致这些非第一个bean文件如:
(applicaiton2.xml,applicaiton3.xml)定义的bean对象在Dwr里不起作用。
4.dwr官方的方法是在dwr.xml文件里加入类似
的参数定义,如果大部分的bean对象是在第一个bean文件里定义的,处理还算可以。
如果有10多个bean对象不是在第一个bean文件里,加入一堆的location参数,每个bean就得装载一次
,在很多时候将会导致启动时发生OutofMemery的异常。
5.此时的方法有二,一是调整启动的容器内存参数,把内存调大。二,更改dwr的location参数装载方式。
6,最理想的方式,从parentContext里取得beanFactory;其次,在装载的时候就把读入bean定义
文件,在内存存储这些beanfactory对象,以备当使用了location参数时使用。
这样就可以一点程度上避免重复装载着启动时消耗过多的内存,加快容器部署速度。
以下是我更新DWR的location参数的代码
在uk.ltd.getahead.dwr.util包里新增一个类SpringLocationUtil
[code="java"]/*
* Copyright 2009 Jandytom
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ClassDescription
* This class help people who want to use Spring bean file location param
* aware cycle build the bean defined xml file.
*/
package uk.ltd.getahead.dwr.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringLocationUtil {
private static SpringLocationUtil locationUtil = null;
private InputStream in;
private Map beanFactoryMap;
private Map beanFileMap;
private SpringLocationUtil(){
this.setBeanFileMap(new HashMap());
this.setBeanFactoryMap(new HashMap());
}
public synchronized static SpringLocationUtil getInstance(){
if(locationUtil==null){
locationUtil = new SpringLocationUtil();
}
return locationUtil;
}
class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
log.debug("Attibute:---" + node.getName() + "="+ node.getValue());
}
public void visit(Element node) {
if (node.isTextOnly()) {
String nodeName = node.getName().trim();
if(nodeName.equals("param")){
String atrNameValue = node.attributeValue("name");
if(atrNameValue!=null&&atrNameValue.startsWith("location")){
String beaFileName = node.attributeValue("value");
if(beaFileName!=null){
beanFileMap.put(atrNameValue, beaFileName);
}
}
}
log.debug("Element:---" + node.getName() + "=" + node.getText());
}else{
log.debug("--------" + node.getName() + "-------");
}
}
public void visit(ProcessingInstruction node) {
log.debug("PI:"+node.getTarget()+" "+node.getText());
}
}
public void InitBeanFactoryMap(InputStream in){
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(in);
doc.accept(new MyVistor());
Map beanMap = new HashMap();
if(beanFileMap!=null&&beanFileMap.size()>0){
for (Iterator it = beanFileMap.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Entry) it.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
BeanFactory factory = new ClassPathXmlApplicationContext(String.valueOf(value));
beanMap.put(value, factory);
}
this.setBeanFactoryMap(beanMap);
}else{
log.info("location param not found .");
}
} catch (DocumentException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
log.error("Close InputStream Error:"+e.getMessage());
}
}
}
}
public static void main(String args[]){
java.io.File file = new java.io.File("D:\\work\\eclipse\\workspaceAll\\Test\\WebRoot\\WEB-INF\\dwr.xml");
try {
InputStream in = new java.io.FileInputStream(file);
SpringLocationUtil.getInstance().InitBeanFactoryMap(in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public InputStream getIn() {
return in;
}
public void setIn(InputStream in) {
this.in = in;
}
public Map getBeanFactoryMap() {
return beanFactoryMap;
}
public void setBeanFactoryMap(Map beanFactoryMap) {
this.beanFactoryMap = beanFactoryMap;
}
public Map getBeanFileMap() {
return beanFileMap;
}
public void setBeanFileMap(Map beanFileMap) {
this.beanFileMap = beanFileMap;
}
/**
* The log stream
*/
private static final Logger log = Logger.getLogger(SpringLocationUtil.class);
}
然后,更改AbstractDWRServlet,更新后的Servlet init()方法如下,红色部分为修改内容
[code="java"] public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
ServletLoggingOutput.setExecutionContext(this);
// How much logging do we do?
String logLevel = config.getInitParameter(INIT_LOGLEVEL);
if (logLevel != null)
{
ServletLoggingOutput.setLevel(logLevel);
}
// call the abstract method to obtain the container
container = getContainer(config);
// Now we have set the implementations we can set the WebContext up
builder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
WebContextFactory.setWebContextBuilder(builder);
// And we lace it with the context so far to help init go smoothly
builder.set(null, null, getServletConfig(), getServletContext(), container);
// Load the system config file
Configuration configuration = (Configuration) container.getBean(Configuration.class.getName());
InputStream in = getClass().getResourceAsStream(FILE_DWR_XML);
log.info("retrieved system configuration file: " + in); //$NON-NLS-1$
InputStream inPlug = config.getServletContext().getResourceAsStream(DEFAULT_DWR_XML);
try
{ SpringLocationUtil.getInstance().InitBeanFactoryMap(inPlug);
configuration.addConfig(in);
}
catch (Exception ex)
{
log.fatal("Failed to load system config file from dwr.jar", ex); //$NON-NLS-1$
throw new ServletException(Messages.getString("DWRServlet.SystemConfigError"), ex); //$NON-NLS-1$
}
// call the abstract method to perform additional configuration
configure(config, configuration);
// Finally the processor that handles doGet and doPost
processor = (Processor) container.getBean(Processor.class.getName());
}
catch (ServletException ex)
{
throw ex;
}
catch (Exception ex)
{
log.fatal("init failed", ex); //$NON-NLS-1$
throw new ServletException(ex);
}
finally
{
if (builder != null)
{
builder.unset();
}
ServletLoggingOutput.unsetExecutionContext();
}
}
更新uk.ltd.getahead.dwr.create.SpringCreator的getBeanFactory()方法
[code="java"] /**
* @return A found BeanFactory configuration
*/
private BeanFactory getBeanFactory()
{
// If someone has set a resource name then we need to load that.
if (configLocation != null && configLocation.length > 0)
{
log.info("Spring BeanFactory via ClassPathXmlApplicationContext using " + configLocation.length + "configLocations."); //$NON-NLS-1$ //$NON-NLS-2$
return (BeanFactory) SpringLocationUtil.getInstance().getBeanFactoryMap().get(configLocation[0]);
//return new ClassPathXmlApplicationContext(configLocation);
}
ServletContext srvCtx = WebContextFactory.get().getServletContext();
HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
if (request != null)
{
return RequestContextUtils.getWebApplicationContext(request, srvCtx);
}
else
{
return WebApplicationContextUtils.getWebApplicationContext(srvCtx);
}
}
编译后更新相关的Class即可。这个对使用location参数多的应用效果比较明显。
注:改动后的jar包只在Tomcat5.0.28下做过测试,其他容器未做测试。
- dwr.jar (184.6 KB)
- 下载次数: 1
相关推荐
- **配置文件**:`dwr.xml`是DWR的核心配置文件,用于声明Java方法的可调用性以及参数类型等信息。 - **安全性**:由于DWR允许直接调用服务器端方法,因此需要确保只暴露安全的、设计为被调用的方法,防止跨站脚本...
### DWR调用含有参数的JAVA类 #### 一、简介 DWR(Direct Web Remoting)是一种简化Ajax开发的框架,它允许JavaScript代码直接调用服务器端的Java方法,从而实现更简单、高效的数据交互。本文将详细介绍如何使用DWR...
dwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwrdwr dwr
在部署时,将相应的jar包添加到Web应用的类路径中,然后配置DWR的Context参数以启用服务。最后,编写客户端和服务器端的代码,利用DWR提供的API实现双向通信。 总的来说,DWR是一个强大的工具,它简化了Web应用中的...
**DWR(Direct Web Remoting)集合Spring实现异步提交** DWR(Direct Web Remoting)是一种开源JavaScript库,它允许Web应用程序在客户端与服务器之间进行实时、双向通信,从而实现异步数据交换。结合Spring框架,...
Direct Web Remoting (DWR) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,从而实现动态Web应用程序。DWR压缩文件中的帮助文档旨在为开发者提供详细的指导,帮助他们理解和使用DWR...
- **Call**:表示一个Ajax调用,包括方法名、参数等信息。 - **Update**:用于更新HTML元素,通常与Ajax响应关联。 5. **使用场景**: - **实时数据更新**:在股票交易、在线聊天等应用中,DWR可以实时推送数据...
**DWR配置文件详解** Direct Web Remoting (DWR) 是一种开源的Java库,它允许Web应用程序在客户端JavaScript和服务器端Java之间进行双向通信。DWR的核心配置文件是`dwr.xml`,该文件定义了DWR允许访问的Java对象、...
### DWR框架详解 #### 一、为什么要使用Ajax框架? Ajax框架的核心价值在于它能够显著地提高用户界面的响应性和用户体验。传统的Web应用程序通常需要完全重新加载页面来更新数据或内容,而Ajax技术则允许在不刷新...
dwr配置文件是Direct Web Remoting(DWR)的核心组件之一,它负责配置DWR的各种设置和参数。在本文中,我们将详细介绍dwr配置文件的结构和配置方法,并探讨它在实际应用中的作用。 dwr配置文件的结构 -------------...
API文档通常会包括每个类的描述、方法签名、参数说明和返回值,帮助开发者快速定位并使用所需的功能。 "WEB界面开发规范.doc"可能是一个关于Web界面设计和开发的最佳实践文档,它可能涵盖了使用DWR时应遵循的样式、...
DWR (Direct Web Remoting) 是一种开源Java技术,它允许Web应用程序在浏览器和服务器之间进行实时、双向通信,使得JavaScript可以直接调用服务器端的Java方法,极大地简化了客户端和服务器端的数据交换。本笔记将...
1、 导入dwr.jar包 2、 在web.xml中配置dwr,如下: <!-- 配置DWR --> <servlet-name>dwr-invoker org.directwebremoting.servlet.DwrServlet <init-param> <param-name>debug</param-name> ...
在这个配置中,`<init-param>`用于设置DWR的运行时参数,如调试模式。`<url-pattern>`定义了DWRServlet监听的URL模式,所有以`/dwr/`开头的请求都将由DWR处理。 总的来说,这些文件共同构成了DWR2.0在Java Web应用...
2. **配置web.xml**:在`web.xml`中添加DWRServlet的配置,设置DWR的相关参数,如开启调试模式等。 3. **配置dwr.xml**:在`WEB-INF`目录下创建`dwr.xml`文件,指定哪些JavaBean需要生成对应的JavaScript库。 ```...
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在客户端与服务器端进行交互,实现了Web应用中的AJAX(Asynchronous JavaScript and XML)无刷新功能。在"dwr验证注册用户"这个场景中,DWR被用来实现在...
DWR (Direct Web Remoting) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,实现动态的Web应用。DWR简化了AJAX(Asynchronous JavaScript and XML)的开发,使得开发者可以像调用...
**DWR(Direct Web Remoting)**是一种Java技术,它允许Web应用程序在客户端与服务器之间进行实时通信,而无需刷新整个页面。通过DWR,我们可以使用JavaScript直接调用服务器端的Java方法,实现Ajax(Asynchronous ...
DWR(Direct Web Remoting)是一种Java库,用于在Web应用程序中实现实时的JavaScript到服务器端Java对象的通信。这个技术允许开发者在浏览器中直接调用服务器端的方法,极大地简化了AJAX(Asynchronous JavaScript ...