Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring 2.5 : Mapping two different Class Impl for a single Interface in application-Context.xml

 
Baalamurugan Raman
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya

Its exactly similar to a Factory pattern. But we dont know how to map two different Concrete implementation classes for a single Interface in application context. It goes like ...

<beans>
<bean name="intI" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces" value="com.ejb.InterfaceI" />
<property name="target" ref="classA" />
</bean>

<bean id="classA"
class="com.ejb.ClassA">
</bean>

<bean id="classB"
class="com.ejb.ClassB">
</bean>

<beans>

based on the input data, I might decide Class A or Class B but with the current setup in spring we will always have only one implementation of Class ie. ClassA. There is no scope of ClassB. We struck up with on how to resolve it. Could anyone help us on this issue.

Thanks in advance!!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Baalamurugan "

Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names, with a space between the two.

Thanks

Mark
 
Baalamurugan Raman
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

oh any saviour of the day to help us !!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly sure what you're trying to do...

If what you're saying is - "I want two ProxyFactoryBeans, one which targets ClassA and one which targets ClassB" - then you can just create two beans with different names -



Instead if what you mean is - "I want one instance of ProxyFactoryBean, and one each of ClassA and ClassB" - take a look at this blog post about Spring instantiation strategies - especially the section titled 'Using a static factory method'.

If you mean - "I want one ProxyFactoryBean, and I want to instantiate either ClassA or ClassB at runtime" - unless this is known at startup, you won't be able to do this in a spring configuration - you'll need to make another class that can construct a ProxyFactoryBean and the class you want, and call this at runtime. If it is known at startup, you could make the class name a property, and use something like what is described in this blog post - Spring configuration with property files - the blog post isn't really about property *files* per se, but how to use a class to filter/provide properties like Spring would get from property files.
 
Baalamurugan Raman
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Yes, You are right !! Actually I want to load the Concrete class implementation dynamically but in applicationContext.xml i can point one interface to one Class. If that is the case How can I use another implementation of class dynamically.

May be spring doesnt support us to dynamically switch two classes

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic