可以通过 XML 定义 bean 构造型吗? [英] It's possible define bean stereotypes via XML?

查看:23
本文介绍了可以通过 XML 定义 bean 构造型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过 XML 定义 bean 构造型吗?如:

It's possible, via XML, define a bean stereotype? Something as:

<bean ... stereotype="org.springframework.stereotype.Service">
</bean>

或者,

<bean...>
    <stereotype class="mypackage.myStereotype" />
</bean> 

?

推荐答案

可能最简单的解决方案是使用任意的 spring bean 元数据,如下所示:

Probably the easiest solution would be to use arbitrary spring bean metadata as follows:

<bean id="fooService" class="org.example.FooServiceImpl">
    <meta key="stereotype" value="mypackage.myStereotype" />
</bean>

spring-beans.xsd 是:

<xsd:element name="meta" type="metaType">
    <xsd:annotation>
        <xsd:documentation><![CDATA[
Arbitrary metadata attached to a bean definition.
        ]]></xsd:documentation>
    </xsd:annotation>
</xsd:element>

<xsd:complexType name="metaType">
    <xsd:attribute name="key" type="xsd:string" use="required">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
The key name of the metadata attribute being defined.
            ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="value" type="xsd:string" use="required">
        <xsd:annotation>
            <xsd:documentation><![CDATA[
The value of the metadata attribute being defined (as a simple String).
            ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
</xsd:complexType>

然后您可以使用 BeanDefinitionRegistry.getBeanDefinition(String)BeanDefinition.getAttribute(String name) 读取构造型bean 并对其进行处理.

You can then use BeanDefinitionRegistry.getBeanDefinition(String) and BeanDefinition.getAttribute(String name) to read the stereotype of the bean and process it.

另一种可能性是使用 spring bean 模式创作工具.因此你需要实现一个 BeanDefinitionDecorator参考文档示例.

Another possibility would be using the spring bean schema authoring facilities. Therefore you would need to implement a BeanDefinitionDecorator as described in the reference documentation example.

这篇关于可以通过 XML 定义 bean 构造型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆