Spring is picking an interface implementation out of many, on its own?

change your code as follows.

Class Writer.java

  package DI;

     import org.springframework.stereotype.Service;

     @Service("writer")
     public class Writer implements IWriter {
     public void writer (String s){
      System.out.println(s);
     }
    } 

Class NiceWriter.java

    package DI;

    import org.springframework.stereotype.Service;

   @Service("niceWriter")
   public class NiceWriter implements IWriter {
   public void writer (String s){
    System.out.println("The string is " + s);
   }
  } 

Another class

     package DI;

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.stereotype.Service;

      @Service
      public class MySpringBeanWithDependency {

     @Autowired
     @Qualifier("writer")//if you need to autowire Writer service   
     private IWriter writer;

     @Autowired
    @Qualifier("niceWriter")//if you need to autowire NiceWriter service
    private IWriter niceWriter


       public void run() {
       String s = "This is my test";
        writer.writer(s);
      }
    }

When there is more than one implementation of interface and you use @Autowired in that case spring bind any of the class. but if you want to autowire specific implementation then you can use

@Qualifier( "<implementing class name>" ) 

@Qualifier documentation

Few things that you must know about Spring is

  • All spring beans are managed - they "live" inside a container, called "application context".
  • Each application has an entry point to that context. Also, there is a place where the application context is bootstrapped and all beans - autowired. In web applications this can be a startup listener.

Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.


Try this one.

Class Writer.java

  package DI;

     import org.springframework.stereotype.Service;

     @Service("writer")
     public class Writer implements IWriter {
     public void writer (String s){
      System.out.println(s);
     }
    } 

Class NiceWriter.java

    package DI;

    import org.springframework.stereotype.Service;

   @Service("niceWriter")
   public class NiceWriter implements IWriter {
   public void writer (String s){
    System.out.println("The string is " + s);
   }
  } 

Another class

     package DI;

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.stereotype.Service;

      @Service
      public class MySpringBeanWithDependency {

     @Autowired
     private IWriter writer;

     @Autowired
    private IWriter niceWriter


       public void run() {
       String s = "This is my test";
        writer.writer(s);
      }
    }

I'd like to show one more option using application.properties.

Benefits:

  • You don't need to change code when you add/change implementation of the interface
  • Works well with unit tests and other environments

Sample code based on @ConditionalOnProperty attribute

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnProperty(value = "writer.type", havingValue = "default")
public class Writer implements IWriter {
    @Override
    public void writer(String s) {
        System.out.println("The string is " + s);
    }
}

And

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnProperty(value = "writer.type", havingValue = "nice")
public class NiceWriter implements IWriter {
    @Override
    public void writer(String s) {
        System.out.println("Nice string is " + s);
    }
}

When application.properties contains writer.type=nice NiceWriter will be instantiated for IWriter interface.

Instead of @ConditionalOnProperty there are other options like Conditional, @ConditionalOnExpression.