-1

I'm using Spring 2.0.6. In my application I have an interface and there are multiple implementations classes.

First of all I need to know, Is there a restriction in Spring that One interface can have one implementation class?

If not, How can I configure these multiple implementations and an interface in spring Context xml.

And how to make Spring to use a single implementation class at run time using a name or some property?

Murugesh
  • 1,009
  • 1
  • 12
  • 34
  • 1
    2.0.6? Is this a typo? That version was released around June 2007. Do yourself a favour and upgrade to a supported version so you can use the documentation effectively. – kryger Mar 02 '15 at 13:43
  • It's an old application, I'm not in control of changing the tech stack and version. – Murugesh Mar 03 '15 at 06:30

1 Answers1

0

First there is no restriction in Spring about implementations, you are free to implement classes as much as you want.

The exmaple below may help you to understand multiple implementations, and how you can chose one at runtime:

Suppose you have 2 DAO classes one for Oracle, the seconde for MySQL, and both classes are implementing a DAO interface. You define an implementation as a bean in Spring configuration file. In the business class you have an attribut of type DAO, while in the spring configuration file you choose the real type wheather Oracle or MySQL to inject or using spring annotation @Autowired.

This reduce coupling and it will be easy to move from Oracle to MySQL.

@Service
public class Business {
    @Autowired
    private Dao daoImpl;

    //Business methods that invoks Dao methods 
}

In the Spring configuration file (XML file) you use the following:

<bean id="daoImpl" class="app.com.MySQLDaoImpl OR app.com.OracleDaoImpl"/>

By just changing the class attribut of your bean you change the whole implementation, without any change in your business class

This is the original answer.

Community
  • 1
  • 1
user3728064
  • 158
  • 1
  • 11