difference between dependency injection and autowiring - spring

Can I know what is the difference between dependency injection and autowiring? Whether autowiring is different from dependency injection?
Which is the best way to autowiring(XML based or annotation based)?

Short answer: Dependency Injection is a design pattern, and #autowired is a mechanism for implementing it.
The DI idea is that, instead of your object creating an object it needs (say by using new to instantiate it), this needed object - a dependency - is handed to your object, typically using the constructor or a setter method. If you autowire, you're injecting a dependancy. In this case, Spring uses reflection to make this work, so you're not using the constructor or a setter method, but you're still injecting the dependency.
To answer question 2, its your choice. Personally, I find the XML configuration files cumbersome and use annotations whenever I can. You can accomplish whatever configuration you need to do either way.

Related

Constructor injection on spring configuration classes

In several code examples I see autowiring used instead of constructor injection. Can any one explain why Constructor injection is difficult but autowiring is possible on spring configuration classes? Thanks in advance.
this question does touch the topic but doesn't have the detailed answer.
Constructor injection requires all dependencies are available beforehand.
In early Spring Boot (before 2.6) circular dependencies are allowed by default. It is impossible to have circular bean dependencies together with the constructor injection (egg-chicken problem if A => B & B => A and both use it).
I think for this reason such expression of DI dependencies was implemented later. With a default constructor + setter or #Autowired you could resolve circular dependencies.

Spring managed vs non-managed beans

I have a simple Result data holder object that gets returned from a method.
I'm confused on what the right way of using or creating this object using Spring boot.
Should this class be marked with #Component annotation?
Can I just create this object using 'new Result()' or should I autowire and use it?
If I use 'new Result()' then this instance will not be managed by Spring. Is that understanding correct? What are the advantages or disadvantages of managed vs non-managed beans.
Thanks,
Sudha
If I use 'new Result()' then this instance will not be managed by Spring. Is that understanding correct? What are the advantages or disadvantages of managed vs non-managed beans.
Well the greatest difference is the Inversion Of Control IoC (aka Dependency Injection DI). If Result is managed by spring, you can autowire other spring beans (like services, components, repositories and so on) Moreover you can autowire the Result bean in other spring beans. More details can be found here
Can I just create this object using 'new Result()' or should I autowire and use it?
Well it depends on your scenario. For example if you have a JSON response in a Spring controller, well in this case it's better to use a classic POJO and create it because it depends on your business logic.
On the other side id your bean is a kind of service who can be used in other points of your project because it offers some methods, well in this case I guess it's good to autowire it and make it as a spring bean
Should this class be marked with #Component annotation?
Well as I said earlier it depends on your scenario. In the case you described (result of a method) maybe it's better to use a classical POJO. But you didn't provide enough information

when to go for constructor injection and when to go for parameter injection in Spring

I'm a fresher, I'm recently started learning Spring.In spring dependency injection,we
can inject a bean in 2 ways,one is through constructor and the other one is through
setter method.My question is, for what situations constructor injection is better and
for what situations setter method injection is better. my focus only on where to use?
Give me an example if possible... waiting for your valuable reply..
There is a third way: Field injection.
You can directly apply the Annotation #Resource, #Inject or #Autowire at a (even private) field. This field even does not need to hava a getter or setter.
If you are building a Spring application, and there is no plan to use the classes in a not Spring application or a library, then the field injection is enough for 90% of the classes.
I prefer it, because it is less code.
Of course if you use a constructor for mandatory references then there is no way to forget one of them when creating a new instance. But (and this is my point of view, that differs from Alef Arendsen in his 3 year old Spring 2.0 blog entry "Setter injection versus constructor injection and the use of #Required") you have a spring bean and not a simple class. And this bean is created by spring, not directly by you. So if you use #Resource, #Inject or #Autowire for fields or setter spring checks them too and do not put the bean and the whole application in service if not all references can be satisfied.
I'd say go for constructor injection.
In some cases go for setter injection if dependency is optional.
If you forced to use setter injection and use Spring, the use #Required to ask Spring to enforce it.
Apply common sense in all cases :)

Dependency Injection Failover in Spring

Is it possible to specify another bean to inject in case that the first intended bean to be injected fails?
Lets say we have Bean1, Bean2, and Bean3. Bean1 requires Bean2 but if Bean2 fails to be injected for some reason, then I want Bean3 to be injected instead. But each time Bean1 is retrieved from the container, it should always try to inject Bean2 first before attempting to inject Bean3. Is this possible? If not, what are my options?
Per me the question is flawed. In normal circumstances, Spring is supposed to be used for injecting the beans declaratively. So as pointed out by #Don Roby, #Adrian Shum the problem you are trying to solve is not for Spring.
Spring is not designed to resolve the dependency for you dynamically like a Service Locator.

Using Spring to wire directly a concrete class

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.
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.
You will have to decide if hardwiring this dependency into other classes is ok. For instance, how many different classes are likely to require this dependency? If the answer is many then you will be creating many instances of this class where only one is required.
Also, what dependancies does this concrete class have? You will have to configure those inside the class that depends on it.
The object of dependency injection is to reduce the dependencies between classes and make your code loosely coupled.

Resources