2013-03-20 66 views
1

我已經定義了一個Spring應用程序上下文XML將最終用戶進行編輯,以增加新的beans.Something,如:/XML文件轉換成一個Spring bean定義

<bean name="myBeanName1" class="com.xxx.Yyy"> 
    <property name="type" value="type1" /> 
    <property name="name" value="name1" /> 
    <property name="arguments" value="arg1" /> 
</bean> 

<bean name="myBeanName2" class="com.xxx.Yyy"> 
    <property name="type" value="type2" /> 
    <property name="name" value="name2" /> 
    <property name="arguments" value="arg2" /> 
</bean> 

. 
. 
. 

現在我要求改變這對一個正常的XML,使用戶不會通過beanpropertyclass的名字被人打擾:

<def name="Name1"> 
    <type>type1</type> 
    <argument>arg1</argument 
</def> 

<def name="Name2"> 
    <type>type2</type> 
    <argument>arg2</argument 
</def> 

由於我的代碼使用的是綠豆,我該如何使用新的XML用最少的代碼改變,使其轉化爲一個早期的bean定義事情和以前一樣工作?

我不知道Spring是否有這個開箱即用的解決方案。我認爲應用樣式表到新的xml來創建我的舊bean。任何其他更好/優雅的解決方案呢?

編輯:現在,用戶輸入不再在bean內部,是否有可能使用新的xml注入到@Component類?

回答

4

Spring支持創建自定義標籤。您需要創建xsd模式NamespaceHandlerm,實現BeanDefinitionParsers,並通過創建spring.handlers spring.shandmas特殊文件來使spring瞭解這些。

看一看Extensible XML authoring

例子:

<beans xmlns declaration goes here> 
    <yournamespace:yourcustomtag id="some id"> 
     <yournamespace:some_other_tag your-custom-attribute="some value" /> 
    </yournamespace:yourcustomtag> 
</beans> 
+0

+1會檢查了這一點。但看起來好像有一些編碼需要實現這一點。這也會將xml更改爲不同的格式,儘管很小? – rajesh 2013-03-20 09:19:03

+0

是的,它會將xml更改爲不同的格式。示例在答案中更新。 – 2013-03-20 09:35:48