如何在Springpython中使用springjavaxml文件

2024-06-28 20:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个使用spring的java应用程序,我需要测试它。 我使用的是安装在eclipse上的jython2.5.2和springpython1.3.0RC。 java应用程序使用属性文件属性并使用如下注释:

@Value("${csvdatafetcher.filename:input.csv}")

属性文件是: core.filedatafetcher.filename=测试.csv文件

我正在尝试调用应用程序:

^{pr2}$

使用spring xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">
....

    <bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:classpath:prop.properties</value>
        </property>
    </bean>

    <bean id="csvDataFetcher" class="com.framework.fetchers.CsvFileDataFetcher" />
</beans>

它给了我一个错误:

Traceback (most recent call last):
  File "/home/nir/.eclipse/org.eclipse.platform_3.6.1_185596441/plugins/org.python.pydev.debug_2.2.4.2011110216/pysrc/pydevd.py", line 1307, in <module>
    debugger.run(setup['file'], None, None)
  File "/usr/share/jython2.5.2/Lib/site-packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init__.py", line 80, in get_object
    comp = self._create_object(object_def)
  File "/usr/share/jython2.5.2/Lib/site-packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init__.py", line 132, in _create_object
    [prop.set_value(obj, self) for prop in object_def.props if hasattr(prop, "set_value")]
  File "/usr/share/jython2.5.2/Lib/site-packages/springpython-1.3.0.RC1-py2.5.egg/springpython/config/_config_base.py", line 149, in set_value
    setattr(obj, self.name, self.value)
TypeError: can't convert 'classpath:spring-config-test.xml' to org.springframework.core.io.Resource;

或者使用(而不是bean id="props"...):

<context:property-placeholder   location="classpath:prop.properties" />

这给了我一个错误:

Traceback (most recent call last):
  File "/home/nir/.eclipse/org.eclipse.platform_3.6.1_185596441/plugins/org.python.pydev.debug_2.2.4.2011110216/pysrc/pydevd.py", line 1307, in <module>
    debugger.run(setup['file'], None, None)
  File "/usr/share/jython2.5.2/Lib/site-packages/springpython-1.3.0.RC1-py2.5.egg/springpython/container/__init__.py", line 128, in _create_object
    obj = object_def.factory.create_object(self._get_constructors_pos(object_def),
  File "/usr/share/jython2.5.2/Lib/site-packages/springpython-1.3.0.RC1-py2.5.egg/springpython/factory/__init__.py", line 31, in create_object
    parts = self.module_and_class.split(".")
AttributeError: 'NoneType' object has no attribute 'split'
  1. 如何将java属性占位符转换成pythonspring?在
  2. 如何插入连接到annotation@Value的属性?在

谢谢


Tags: inpyorgselfshareobjectvaluelib
1条回答
网友
1楼 · 发布于 2024-06-28 20:28:18

如果您阅读http://static.springsource.org/spring-python/1.2.x/sphinx/html/objects-other-formats.html#springjavaconfig以了解更多细节,这将使您更加清楚地了解SpringJavaConfig和springpython的功能。首先,它只支持spring2.5xml格式,不包括额外的名称空间。它还可以在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()

使用以下命令访问应用程序上下文:

^{pr2}$

从这里开始,你可以参考ctx支柱访问属性值。请注意,springpython没有自动连接,所以属性不会自动注入到事物中。但是,使用纯Python代码解析文件非常容易,并将其很好地嵌入到应用程序上下文中,而上下文又可以被注入到相关的对象中。在

相关问题 更多 >