Spring: Why are we using an autwire interface and not an implemented class?

Example

interface IA { public void someFunction(); } @Resource(name="b") class B implements IA { public void someFunction() { //busy code block } public void someBfunc() { //doing b things } } @Resource(name="c") 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(); } 

Can someone explain this to me.

  • How spring knows which polymorphic type to use.
  • Do I @Qualifier or @Resource ?
  • Why do we use an autwire interface and not an implemented class?
+106
java spring dependency-injection
Oct. 15 '12 at 15:53
source share
3 answers

How spring knows which polymorphic type to use.

As long as there is only one implementation of the interface, and this implementation is annotated with @Component with spring scanning enabled, the spring infrastructure can find a pair (interface, implementation). If component verification is not enabled, you need to explicitly define a bean in the application config-config.xml configuration file (or equivalent spring).

Do I need @Qualifier or @Resource?

After you have several implementations, you need to qualify each of them and during automatic posting, you will need to use the @Qualifier annotation to enter the correct implementation along with the @Autowired annotation. If you use @Resource (J2EE semantics), then you must specify the bean name using the name attribute of this annotation.

Why do we use an autwire interface and not an implemented class?

Firstly, it is always good practice to code interfaces in general. Secondly, in the case of spring, you can implement any implementation at run time. A typical use case is the introduction of a mock implementation at the 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(); } 

The bean configuration should look like this:

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

Alternatively, if you enable component checking in the package where they are present, 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, an instance of type B will be introduced in MyRunner .

+172
Oct. 15
source share

It may also cause some warnings in the logs, for example Cglib2AopProxy Impossible proxy method . And many other reasons for this are described here. Why are there always uniform interfaces for the implementation of the service and Tao?

+1
Sep 22 '15 at 11:29
source share

I think that creating an interface for only one implementation is a stupid practice accepted in the Java world. The result is a lot of junk code, but everyone is happy that they followed the SOLID and OOP rules. Use the signboard and throw the spring in the trash bin of history.

0
Dec 21 '18 at 9:39
source share



All Articles