1

スプリングを使用するJavaアプリケーションがあり、テストする必要があります。EclipseにSpringpython1.3.0RCがインストールされたJython2.5.2を使用しています。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")

春の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スプリングに変換する方法は?
  2. アノテーション@Valueに接続するプロパティを挿入するにはどうすればよいですか?

ありがとう、

4

1 に答える 1

1

詳細についてhttp://static.springsource.org/spring-python/1.2.x/sphinx/html/objects-other-formats.html#springjavaconfigを読むと、SpringJavaConfig と Spring Python が何であるかがより明確になります。します。1 つには、Spring 2.5 XML 形式のみをサポートし、追加の名前空間は含まれていません。また、Python システムでの Java オブジェクトではなく、Python システムでの Python オブジェクトの構成にも対応しています。アイデアは、構成ファイルを書き直すことなく、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 コードを使用してファイルを解析するのは非常に簡単で、それをアプリのコンテキストに適切に組み込むことができます。これにより、関連するオブジェクトに注入することができます。

于 2012-02-09T01:10:16.313 に答える