简体   繁体   English

使用Spring的XML配置解析器

[英]XML Configuration parser with Spring

I'm currently re-factoring a legacy application to use Spring but I currently have a pretty big sticking point: 我目前正在重新考虑遗留应用程序以使用Spring,但我目前有一个非常重要的问题:

We have an XML configuration file for the application which determines not only how an object is configured but also which objects should be used ie (if xmlNode.text = 'A' then use object A). 我们有一个应用程序的XML配置文件,它不仅确定如何配置对象,还确定应该使用哪些对象,即(如果xmlNode.text ='A'则使用对象A)。 I therefore need to be able to parse the XML config in order to know which beans would be injected. 因此,我需要能够解析XML配置,以便知道将注入哪些bean。

Does anybody know of a way of doing this? 有人知道这样做的方法吗?

Kind Regards, 亲切的问候,

JLove JLove

It's XML - use a DOM parser and walk the tree. 它是XML - 使用DOM解析器并遍历树。

It feels like you're defeating the purpose of Spring. 感觉就像是在打败Spring的目的。 You could create factories that would act as virtual constructors and do the same thing. 您可以创建充当虚拟构造函数的工厂并执行相同的操作。 Maybe you should reconsider this design. 也许你应该重新考虑这个设计。

I would favor replacing the old XML configuration file completely with a Spring application context file. 我倾向于使用Spring应用程序上下文文件完全替换旧的XML配置文件。 Leaving the old configuration in place sounds like a half-baked migration which will probably give you pain quite soon. 保留旧的配置听起来像一个半生不熟的迁移,很可能会很快给你带来痛苦。

Spring supports bean creation using a static factory method. Spring使用静态工厂方法支持bean创建。 Maybe you could use this to handle the custom object creation you mention. 也许您可以使用它来处理您提到的自定义对象创建。

Use a SAXParser or DOMParser? 使用SAXParser或DOMParser? Google for JAXP (Java API for XML Processing), especially the DocumentBuilderFactory and SAXParserFactory. Google for JAXP(用于XML处理的Java API),尤其是DocumentBuilderFactory和SAXParserFactory。

You can find a really helpful introduction here: JAXP 你可以在这里找到一个非常有用的介绍: JAXP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM