如何在Springpython中使用Spring Java XML文件

1 投票
1 回答
1127 浏览
提问于 2025-04-17 12:36

我有一个使用Spring的Java应用程序需要进行测试。现在我在Eclipse上使用Jython 2.5.2和Springpython 1.3.0RC。

这个Java应用程序使用了一个名为prop.properties的属性文件,并且使用了像这样的注解:

@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")

使用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的属性占位符转换为Python的Spring?
  2. 我该如何注入与@Value注解连接的属性?

谢谢,

1 个回答

1

如果你想了解更多,可以看看这个链接:http://static.springsource.org/spring-python/1.2.x/sphinx/html/objects-other-formats.html#springjavaconfig。这里面会让你更清楚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来获取属性值。需要注意的是,Spring Python没有自动注入的功能,所以属性不会自动填充到对象里。不过,用纯Python代码解析文件其实很简单,你可以把这些属性整齐地放进你的应用上下文中,然后再把它们注入到相关的对象里。

撰写回答