如何在 Springpython 中使用 spring java xml 文件
我有一个使用 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.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'
- 如何将java property-placeholder转换为python spring?
- 如何注入连接到注释 @Value 的属性?
谢谢,
I have a java application that uses spring, that I need to test.
I'm using Jython 2.5.2 with Springpython 1.3.0RC installed on eclipse.
the java application uses a properties file prop.properties and uses annotations like:
@Value("${csvdatafetcher.filename:input.csv}")
The properties file is:
core.filedatafetcher.filename=test.csv
I'm trying to call the application:
from springpython.context import ApplicationContext
from springpython.config import SpringJavaConfig
ctx = ApplicationContext(SpringJavaConfig("javaBeans.xml"))
service = ctx.get_object("csvDataFetcher")
with 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>
and it gives me the error:
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;
or using (instead of bean id="props"...):
<context:property-placeholder location="classpath:prop.properties" />
that gives me the error:
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'
- How to convert java property-placeholder into python spring?
- How can I inject properties that connected to annotation @Value?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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 的更顺畅的方法,而无需重写配置文件。
通过以下方式访问您的应用程序上下文:
从这里开始,您可以参考 ctx.props 来访问 prop 值。请注意,Spring Python 没有自动装配功能,因此属性不会自动注入到事物中。但使用纯 Python 代码解析该文件非常容易,并将其很好地隐藏到应用程序上下文中,而应用程序上下文又可以注入到相关对象中。
If you read http://static.springsource.org/spring-python/1.2.x/sphinx/html/objects-other-formats.html#springjavaconfig for more details, this should give you more clarity of what SpringJavaConfig and Spring Python does. For one thing, it only supports Spring 2.5 XML format, and doesn't include the extra namespace stuff. It is also geared for configuring Python objects in a Python system, not Java objects in a Python system. The idea is to simply provide a smoother way to transition from Java to Python without having to rewrite your configuration files.
Access your app context with this:
From here on, you can refer to ctx.props to access the prop values. Pay note, Spring Python doesn't have an autowiring, so properties are not automatically injected into things. But it's pretty easy to parse the file using pure Python code, and have it nicely tucked into your app context, which in turn can be injected into relevant objects.