Mahesh Myneni
Mahesh Myneni

Reputation: 1

Does Spring Supports Interface Injection

Does Spring supports Interface Injection completely?

From “Martin Fowler” Blog it was mentioned that a component that depends on a bean needs to implemented as an Interface that injects Bean.

When we consider this scenario it will be possible BeanNameAware, XXXAware Interfaces that injects appropriate bean object.

Is it possible to inject userdefinedd Beans using Interface Injection other than spring supplied classes.

I am confused with all the above.

Upvotes: 0

Views: 877

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121212

It is correct and canonical concept.

You rely on contract (interfaces) and inject them, but container takes care of their implementations:

public interface MyService {}

public class MyServiceImpl implements MyService {}

<bean id="myService" class="com.my.proj.MyServiceImpl"/>

public class MyController {

   @Autowired
   private MyService service;

}

Don't mix contaier components and its API (*Aware) and user-defined application components.

Upvotes: 1

Related Questions