Question

I am using an external library that requires that I configure it using an XML Bean definition; in the Bean definition I need to provide an external class with a Bean from my project. I am using spring annotations with component scanning.

How do I reference my annotated Bean within the XML Bean Definition?

Are there any alternatives to creating an XML Bean Definition?

Details: Spring 3.0.7

Was it helpful?

Solution

No matter how it got created (based on XML- or annotation- metadata), every bean ends up in the application context under a unique name.

If you've just annotated your class with @Component or derivatives, without stating any name, the default naming scheme will be applied and the bean name will be your class name with the first character lowercased: ClassName => "className".

With that in mind, if you need to inject that bean in an XML bean definition, you do it like with any other bean in your context:

<bean id="someBean" class="SomeClass">
    <property name="someProp" ref="className"/><!-- to stick to the above example -->
</bean>

Since you're mixing annotations with XML, the application context will be able to locate the "className" bean properly.

OTHER TIPS

The @Service annotation takes an optional String value which can be used to give the bean a name of your choosing. For example, if your custom bean looks like:

@Service("mySpecialName")
public class MyClass { ... }

Then your xml could have:

<bean class="com.someone.else.library.SomeClass">
  <property name="someProp" ref="mySpecialName"/>
</bean>

Make sure add below code in your xml file

<context:component-scan base-package="" />
<context:annotation-config />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top