Spring: Why Do We Autowire the Interface and Not the Implemented Class

Spring: Why do we autowire the interface and not the implemented class?

How does spring know which polymorphic type to use.

As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, Spring framework can find out the (interface, implementation) pair. If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).

Do I need @Qualifier or @Resource?

Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.

Why do we autowire the interface and not the implemented class?

Firstly, it is always a good practice to code to interfaces in general. Secondly, in case of spring, you can inject any implementation at runtime. A typical use case is to inject mock implementation during testing stage.

interface IA
{
public void someFunction();
}

class B implements IA
{
public void someFunction()
{
//busy code block
}
public void someBfunc()
{
//doing b things
}
}

class C implements IA
{
public void someFunction()
{
//busy code block
}
public void someCfunc()
{
//doing C things
}
}

class MyRunner
{
@Autowire
@Qualifier("b")
IA worker;

....
worker.someFunction();
}

Your bean configuration should look like this:

<bean id="b" class="B" />
<bean id="c" class="C" />
<bean id="runner" class="MyRunner" />

Alternatively, if you enabled component scan on the package where these are present, then you should qualify each class with @Component as follows:

interface IA
{
public void someFunction();
}

@Component(value="b")
class B implements IA
{
public void someFunction()
{
//busy code block
}
public void someBfunc()
{
//doing b things
}
}

@Component(value="c")
class C implements IA
{
public void someFunction()
{
//busy code block
}
public void someCfunc()
{
//doing C things
}
}

@Component
class MyRunner
{
@Autowire
@Qualifier("b")
IA worker;

....
worker.someFunction();
}

Then worker in MyRunner will be injected with an instance of type B.

What happens when an interface is Autowired vs the implementation

As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, Spring framework can find out the (interface, implementation) pair.

Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE), then you should specify the bean name using the name attribute of this annotation.

Normally, both will work, you can autowire interfaces or classes.

Spring autowire interface

@Autowired is actually perfect for this scenario. You can either autowire a specific class (implemention) or use an interface.

Consider this example:

public interface Item {
}

@Component("itemA")
public class ItemImplA implements Item {
}

@Component("itemB")
public class ItemImplB implements Item {
}

Now you can choose which one of these implementations will be used simply by choosing a name for the object according to the @Component annotation value

Like this:

@Autowired
private Item itemA; // ItemA

@Autowired
private Item itemB // ItemB

For creating the same instance multiple times, you can use the @Qualifier annotation to specify which implementation will be used:

@Autowired
@Qualifier("itemA")
private Item item1;

In case you need to instantiate the items with some specific constructor parameters, you will have to specify it an XML configuration file. Nice tutorial about using qulifiers and autowiring can be found HERE.

Spring boot autowiring an interface with multiple implementations

Use @Qualifier annotation is used to differentiate beans of the same interface

Take look at Spring Boot documentation

Also, to inject all beans of the same interface, just autowire List of interface

(The same way in Spring / Spring Boot / SpringBootTest)

Example below:

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

public interface MyService {

void doWork();

}

@Service
@Qualifier("firstService")
public static class FirstServiceImpl implements MyService {

@Override
public void doWork() {
System.out.println("firstService work");
}

}

@Service
@Qualifier("secondService")
public static class SecondServiceImpl implements MyService {

@Override
public void doWork() {
System.out.println("secondService work");
}

}

@Component
public static class FirstManager {

private final MyService myService;

@Autowired // inject FirstServiceImpl
public FirstManager(@Qualifier("firstService") MyService myService) {
this.myService = myService;
}

@PostConstruct
public void startWork() {
System.out.println("firstManager start work");
myService.doWork();
}

}

@Component
public static class SecondManager {

private final List<MyService> myServices;

@Autowired // inject MyService all implementations
public SecondManager(List<MyService> myServices) {
this.myServices = myServices;
}

@PostConstruct
public void startWork() {
System.out.println("secondManager start work");
myServices.forEach(MyService::doWork);
}

}

}

For the second part of your question, take look at this useful answers first / second

Spring Autowiring only works with Interface

I tried multiple ways to fix this problem, but I got it working the following way.

@Autowired
private ClassA classA1;

@Autowired
private ClassA classA2;

In the application context, I defined the bean as below:

<bean id="classA1" class="com.abc.ClassA1" autowire="byName" />
<bean id="classA2" class="com.abc.ClassA2" autowire="byName" />


Related Topics



Leave a reply



Submit