2

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

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

or,

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

?

Helder
  • 73
  • 1
  • 8
  • 1
    What are you trying to achieve? – Andrei Stefan Jul 22 '14 at 12:07
  • Maybe this helps: http://stackoverflow.com/questions/2964180/adding-an-annotation-to-a-runtime-generated-method-class-using-javassist – Stefan Jul 22 '14 at 12:19
  • Hi, Andrei. I'm trying use the default behaviour provided by spring stereotypes into a project that must use xml configuration. I would like, too, define my custom stereotypes and use them. – Helder Jul 23 '14 at 14:24

2 Answers2

1

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>

The definition of the meta element in spring-beans.xsd is:

<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>

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

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.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
0

It is not possible to dynamically add annotations to your classes. Simple answer to your question is: No, it is not possible to apply stereotype annotation via XML.

However stereotype annotation usually serve only as a marking annotations for AOP components (and component scan). You can define your own AOP behavior. Of course you won't be able to use any built in <xyz:annotation-driven \> shortcut declaration / configuration.

Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • Thanks Pavel, it's possible define stereotypes and use them without annotations? Can i mark that a class or a bean is stereotyped via xml? Maybe tag inside a bean definition? – Helder Jul 23 '14 at 14:36
  • @Helder What will be the use-case of that? Maybe I can provide you with alternative approach. – Pavel Horal Jul 23 '14 at 15:21
  • Pavel, sorry for not answering you before and for my English. We are developing a security framework based on XACML (https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=xacml#XACML20) and we defined a service oriented layer structure based on service models that exists in core of our solution. We designed a stereotype to each service model and the framework clients (basically others products/projects in our organization) can use them to extends or replace service behaviours. – Helder Aug 07 '14 at 18:47
  • A common behaviour of service models is translate exceptions to a known exception by service clients. We doesn't can use specific spring annotations directly on core implementation (core-impl) because we provide support to others DI technologies (an other jar will binding the core-impl with the technology: spring, cdi, etc.), thus, we preferred use xml. Therefore, we need some way to declare a stereotype to beans or types/classes via xml. We also do not desire coupling the aspect for implement behaviors to a specific package or type, except the stereotype. Thanks in advance! – Helder Aug 07 '14 at 18:49
  • Adding stereotype manually to a set of beans is in my view equivalent to defining list of beans. So why not just do that? – Pavel Horal Aug 08 '14 at 07:59