使用/转换XML文件到Spring bean定义

时间:2022-01-14 09:00:17

I have defined a Spring application context xml which will be edited by end users to add new beans.Something like:

我已经定义了Spring应用程序上下文xml,最终用户将对其进行编辑,以添加新的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>

.
.
.

Now I am asked to change this to a normal xml so that users wont be bothered by bean property and class names:

现在我被要求将其更改为普通的xml,这样用户就不会被bean属性和类名所困扰:

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

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

As my code is using the bean, how can I use the new xml with minimal code change so that it is converted to a bean definition as earlier and things work as before?.

由于我的代码使用的是bean,所以我如何能够以最小的代码更改来使用新的xml,以便像前面那样将其转换为bean定义,并像以前那样工作呢?

I dont know if Spring has a solution for this out of the box. What I thought was applying stylesheet to the new xml to create my older bean. Any other better/elegant solution for this?

我不知道Spring是否有现成的解决方案。我想的是将样式表应用到新的xml中来创建旧的bean。还有更好/更优雅的解决方案吗?

Edit: Now that user input is not inside the bean anymore, is it possible to use the new xml to inject to a @Component class?

编辑:既然用户输入已经不在bean中了,是否可以使用新的xml向@Component类注入?

1 个解决方案

#1


4  

Spring supports creating custom tags. You need to create xsd schema, NamespaceHandlerm, implement BeanDefinitionParsers and make spring aware of these by creating spring.handlers & spring.schemas special files.

Spring支持创建自定义标记。您需要创建xsd模式、NamespaceHandlerm、实现BeanDefinitionParsers并通过创建spring使spring意识到这些。处理程序和春天。模式特殊文件。

Have a look at Extensible XML authoring

看看可扩展的XML创作?

Example:

例子:

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

#1


4  

Spring supports creating custom tags. You need to create xsd schema, NamespaceHandlerm, implement BeanDefinitionParsers and make spring aware of these by creating spring.handlers & spring.schemas special files.

Spring支持创建自定义标记。您需要创建xsd模式、NamespaceHandlerm、实现BeanDefinitionParsers并通过创建spring使spring意识到这些。处理程序和春天。模式特殊文件。

Have a look at Extensible XML authoring

看看可扩展的XML创作?

Example:

例子:

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