Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring to wire directly a concrete class

Tags:

spring

Does it makes sense in Spring to use @Autowired to wire directly to a concrete class and not to an interface (and make use of 'by type' autowiring)

If a class doesn't implements an interface wouldn't it be better to instantiate it via constructor or a factory (keeping things simple); rather than make it a Spring bean just for the heck of it.

like image 684
haps10 Avatar asked Mar 13 '11 07:03

haps10


People also ask

What are the different ways that Spring can wire beans?

There are two ways to wire beans in the Spring framework: one, you wire the beans manually by declaring the beans, using the Dependency Injection (DI) and so on and two, let the Spring container wire the required beans. The latter process is known as auto wiring.

Can we Autowire abstract class in Spring?

We can't use @Autowired on a constructor of an abstract class. Spring doesn't evaluate the @Autowired annotation on a constructor of an abstract class. The subclass should provide the necessary arguments to the super constructor.

Why is Autowired not recommended?

If we had used Autowired, we would have been getting the following error in the test. Therefore, it is not recommended to use Autowired. Safety — Forces Spring to provide mandatory dependencies. We make sure that the created objects are valid after construction.

What is difference between @inject and @autowired?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application. Sr.


1 Answers

Does it makes sense in Spring to use @Autowired to wire directly to a concrete class and not to an interface

Sure. The practice of autowiring is independent of what you're autowiring. It'll work with classes just as well as interfaces.

However, whether or not it's a good idea is debatable, although this is a more general question of whether you should always introduce an interface for a given class, rather than talk directly to the class type. The benefits include easier unit-testing and a cleaner design, at the expense of code clutter.

There's another good reason to autowire interface types rather than class types, which is that if Spring needs to generate a proxy object around the bean before injecting it, then if the bean's class defines any interfaces, then the proxy will implement those interfaces, and will not be type-compatible with the bean class itself. If you then try and autowire that bean by class type, it will fail. The easiest way to avoid this annoying scenario is to always autowire by interface type, that way it will lways work as you expect.

and make use of 'by type' autowiring

If you mean container-level byType autowiring, then you don't want to do that. It's the old Spring 1.x style of autowiring, and it's highly inflexibile (see limitations of autowiring).

Stick with @Autowired, it's much more flexible and easier to control.

If a class doesn't implements an interface wouldn't it be better to instantiate it via constructor or a factory (keeping things simple); rather than make it a Spring bean just for the heck of it.

The two questions are completely separate. An object should be made a Spring bean if you need Spring to control its dependencies and lifecycle, regardless of whether or not it implements interfaces. If you find that the object has no dependencies, and no meaningful interface, then perhaps there is no reason to make it a bean.

like image 112
skaffman Avatar answered Nov 15 '22 20:11

skaffman