是否可以通过XML定义bean原型?某些东西,如:
<bean ... stereotype="org.springframework.stereotype.Service">
</bean>或,
<bean...>
<stereotype class="mypackage.myStereotype" />
</bean> 发布于 2016-02-18 07:44:06
最简单的解决方案可能是使用任意spring元数据,如下所示:
<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的原型并对其进行处理。
另一种可能是使用spring 模式创作工具。因此,您需要像在BeanDefinitionDecorator中所描述的那样实现参考文档示例。
发布于 2014-07-22 12:12:28
不可能动态地向类添加注释。对您的问题的简单回答是:不可能通过XML.应用原型注释。
然而,原型注释通常只作为AOP组件(和组件扫描)的标记注释。您可以定义自己的AOP行为。当然,您将无法使用<xyz:annotation-driven \>中内置的任何快捷声明/配置。
https://stackoverflow.com/questions/24886846
复制相似问题