spring 3: inject a dependency from a library?

I am developing a project and using 3rd party libraries. Let's say I use a library which gives me the object ExtObj. In my project I have a class MyObj, which uses ExtObj. How can I configure spring 3 to inject ExtObj in MyObj?

I tried to research the topic on the internet, but I didn't find a straight answer. I would like to use xml configuration and maybe (?) @Autowired, not @EJB or @Inject

Thanks in advance!

UPDATE my guess was:

<bean id="myObj" value="me.MyObj">
  <property name="extObj" value=" ... ??? ...">
</bean>

So, I don't know what I should put into value. I guess that's where the reference to the external object goes. But spring can only reference objects that have been already defined/configured in spring. So:

<bean id="extObj" value="ext.lib.ExtObj">
<bean id="myObj" value="me.MyObj">
  <property name="extObj" value="extObj">
</bean>

Is that configuration right?

Answers


First you need to define a bean for your ExtObj in your application context (an xml file or a @Configuration class). Eg. if ExtObj has a constructor taking a String you can define the bean this way:

<bean id="extObj" class="ext.lib.ExtObj">
    <constructor-arg value="SomeString"/>
</bean>

To define MyObj you can use constructor injection:

<bean id="myObj" class="me.MyObj">
    <constructor-arg ref="extObj"/>
</bean>

or setter injection:

<bean name="myObj" class="me.MyObj">
    <property name="extObj" ref="extObj"/>
</beans>

If you use setter injection then MyObj needs to have a setter setExtObj. If you use constructor injection MyObj needs to have a constructor taking an instance of the ExtObj class.


Need Your Help

iPhone: Dynamic spaces in NSString

iphone

It may be a simple question, but i could't get the answer and needing your help!

Create and assign reference data from within a struct method

rust

I have abstracted my problem inside the following code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.