如何在Springpython中使用SpringJavaXML文件
我有一个使用spring的java应用程序,我需要对它进行测试。 我正在使用Jython 2.5.2和eclipse上安装的Springpython 1.3.0RC。 java应用程序使用属性文件prop.properties并使用如下注释:如何在Springpython中使用SpringJavaXML文件,python,spring,jython,applicationcontext,Python,Spring,Jython,Applicationcontext,我有一个使用spring的java应用程序,我需要对它进行测试。 我正在使用Jython 2.5.2和eclipse上安装的Springpython 1.3.0RC。 java应用程序使用属性文件prop.properties并使用如下注释: @Value("${csvdatafetcher.filename:input.csv}") 属性文件是: core.filedatafetcher.filename=test.csv 我正在尝试调用应用程序: from springpython.con
@Value("${csvdatafetcher.filename:input.csv}")
属性文件是:
core.filedatafetcher.filename=test.csv
我正在尝试调用应用程序:
from springpython.context import ApplicationContext
from springpython.config import SpringJavaConfig
ctx = ApplicationContext(SpringJavaConfig("javaBeans.xml"))
service = ctx.get_object("csvDataFetcher")
使用SpringXML:
....
类路径:类路径:prop.properties
这给了我一个错误:
回溯(最近一次呼叫最后一次):
文件“/home/nir/.eclipse/org.eclipse.platform_3.6.1_185596441/plugins/org.python.pydev.debug_2.2.4.2011110216/pysrc/pydevd.py”,第1307行
运行(安装程序['file'],无,无)
文件“/usr/share/jython2.5.2/Lib/site packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init___.py”,第80行,在get_对象中
comp=self.\u创建\u对象(对象定义)
文件“/usr/share/jython2.5.2/Lib/site packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init__.py”,第132行,在创建对象中
[prop.set_值(obj,self)用于对象_def.props中的prop,如果hasattr(prop,“set_值”)]
文件“/usr/share/jython2.5.2/Lib/site packages/springpython-1.3.0.RC1-py2.5.egg/springpython/config/_-config_-base.py”,第149行,在set_值中
setattr(对象、self.name、self.value)
TypeError:无法将“classpath:spring config test.xml”转换为org.springframework.core.io.Resource;
或者使用(而不是bean id=“props”…):
这给了我一个错误:
回溯(最近一次呼叫最后一次):
文件“/home/nir/.eclipse/org.eclipse.platform_3.6.1_185596441/plugins/org.python.pydev.debug_2.2.4.2011110216/pysrc/pydevd.py”,第1307行
运行(安装程序['file'],无,无)
文件“/usr/share/jython2.5.2/Lib/site packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init__.py”,第128行,在创建对象中
obj=对象定义工厂。创建对象(自我获取构造函数)位置(对象定义),
文件“/usr/share/jython2.5.2/Lib/site packages/springpython-1.3.0.RC1-py2.5.egg/springpython/factory/__init___;.py”,第31行,位于create_对象中
部件=自模块和类拆分(“.”)
AttributeError:“非类型”对象没有属性“拆分”
from springpython.config import PythonConfig
from springpython.config import Object
from com.framework.fetchers import CsvFileDataFetcher
class YourAppContext(PythonConfig):
def __init__(self):
self.props = {}
with open("relative/path/to/your/prop.properties") as f:
for line in f.readlines():
key, value = line.split("=")
self.props[key] = value
@Object
def csvDataFetcher(self):
return CsvFileDataFetcher()
使用以下内容访问您的应用程序上下文:
from springpython.context import ApplicationContext
ctx = ApplicationContext(YourAppContext())
service = ctx.get_object("csvDataFetcher")
从这里开始,您可以参考ctx.props来访问prop值。请注意,Spring Python没有自动连接,因此属性不会自动注入到对象中。但是,使用纯Python代码解析文件非常容易,并将其很好地嵌入到应用程序上下文中,而应用程序上下文又可以注入到相关对象中如果您阅读更多详细信息,这将使您更清楚地了解SpringJavaConfig和Spring Python的功能。首先,它只支持Spring 2.5 XML格式,不包括额外的命名空间内容。它还适用于在Python系统中配置Python对象,而不是Python系统中的Java对象。其思想是提供了一种从Java到Python的更平滑的过渡方式,无需重写配置文件
from springpython.config import PythonConfig
from springpython.config import Object
from com.framework.fetchers import CsvFileDataFetcher
class YourAppContext(PythonConfig):
def __init__(self):
self.props = {}
with open("relative/path/to/your/prop.properties") as f:
for line in f.readlines():
key, value = line.split("=")
self.props[key] = value
@Object
def csvDataFetcher(self):
return CsvFileDataFetcher()
使用以下内容访问您的应用程序上下文:
from springpython.context import ApplicationContext
ctx = ApplicationContext(YourAppContext())
service = ctx.get_object("csvDataFetcher")
从这里开始,您可以参考ctx.props来访问prop值。请注意,Spring Python没有自动连接,因此属性不会自动注入到对象中。但是,使用纯Python代码解析文件非常容易,并将其很好地嵌入到应用程序上下文中,而应用程序上下文又可以注入到相关对象中我很想尝试将配置xml重写为python配置,但我找不到如何转换这些上下文和占位符部分。你知道一个不仅包括基本方法的好教程吗?我做了一些编辑,试图向你展示如何用纯python重写Spring xml文件。我很想尝试并重写将配置xml转换为python配置,但我找不到如何转换这些上下文和占位符部分。你知道一个不仅包括基本方法的好教程吗?我做了一些编辑,试图向你展示如何用纯python重写Spring xml文件。