Use Spring annotation to inject dependency - spring

In my project, i see a spring dependency inujection syntax like this in my integration layer:
applicationContext.getBean("beanName");
where applicationContext instance of ClasspathXMLApplicationContext and "beanName" is defined in the spring xml.
If I want to inject it with Annotation, which one should I use? #Inject,#Autowired,#Resource. Seems like I cna use any one of these and I cannot seem to be able to decide which one.
This is SPring integration layer , not MVC layer but i dont think that makes any difference.

#Inject and #Autowired do the same thing, it autowires by type. #Inject is preferred because it is a java annotation and does not couple you to Spring
#Resource autowires by name. This is useful when you have many beans of the same type. You can also use #Named along with #Inject for the same behavior.

#Inject is synonymous to #Autowired.
#Autowired moreover offers optional injection #Autowired(reqired=false) #Inject doesn't have this option.
#Inject and #Resource is standardized in JSR-299 so it should be preffered if possible.
in short, order in which mentioned annotations match dependency to be injected:
#Autowired and #Inject
Matches by Type
Restricts by Qualifiers (#Qualifier annotation)
Matches by Name
#Resource
Matches by Name
Matches by Type
Restricts by Qualifiers (#Qualifier, ignored if match is found by name)
You can find more about these annotations here:
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

#Inject and #Autowired are similar. The native Spring annotation is #Autowired, even Spring also support the Java #Inject annotation, that makes the same.
#Resource is Java annotation, Spring also support that annotation. It means JNDI resource.
See
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-annotation-config
http://docs.oracle.com/javase/7/docs/api/javax/annotation/Resource.html
http://docs.oracle.com/javaee/6/api/javax/inject/Inject.html

Related

What class implements the spring framework Autowired

I downloaded the spring-framework project, because I want to see how #Autowired is implemented.
So, I got to this file, which is an interface.
But when I want in Intellij to go to its implementation, no implementations are found.
So is this interface not implemented?
Then where is the code for #Autowired?
Well, this is not an interface it is actually an annotation.
In java #inteface is used to create an annotation.
Once the annotation is created, you can use that annotation on fields, classes, methods (based on what is specified in #Target of the annotation definition.
Spring does package scanning and finds all the things which are using a particular annotation and does the required processing.
Use this article to undestand more in How an annotation is created, used and the how the annotation processor finds and processes the annotation.
#Autowired doesn't really have much code, so to speak. It's just an annotation which is a Java type of interface that provides instructions to other parts of the codebase.
#Autowired is only an annotation or you can say a "marker". Spring use reflection to identify annotation and do something about that annotated thing. For example with #Autowired, when spring found it, spring will inject the annotated property with eligible bean.

Spring injects dependencies in constructor without #Autowired annotation

I'm experimenting with examples from this official Spring tutorials and there is a dependency on this code:
https://github.com/spring-guides/gs-async-method/tree/master/complete
If you look at the code on AppRunner.java class, I have 2 questions:
When server is starting, if I put a breakpoint in this class's constructor, seems like in the constructor, the GitHubLookupService is provided by spring, using the #Service bean that was configured. BUT, there was no #Autowired annotation on the constructor, so how in the world this constructor get called with the right dependency? It was supposed to be null.
Is it an automatic assumption of Spring Boot?
Does Spring see "private field + constructor argument, and it assumes it should look for an appropriate bean?
Is it Spring Framework or Spring boot?
Am I missing something?
As I remember, it was mendatory to provide default constructor to beans / service etc. How come this class (AppRunner) doesn't have a default constructor?
How does Spring knows that it should run the constructor with the argument?
Is it because it is the only constructor?
Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the #Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies.
Regarding the default constructor: You either need the default constructor, a constructor with the #Autowired annotation when you have multiple constructors, or only one constructor in your class with or without the #Autowired annotation.
Read the #Autowired chapter from the official Spring documentation for more information.
Think of it this way... Suppose you have the following component:
#Component
public class FooService {
public FooService(Bar bar) { /*whatever*/ }
}
When Spring is scanning this class, it wants to know how it should go about constructing an instance. It's using reflection so it can get a list of all of the constructors at runtime.
In this case, it is completely unambiguous how Spring must construct this instance. There's only one constructor so there is no decision to be made, and no ambiguity at all.
If you add #Autowired here, you are not narrowing anything down, and you are not giving Spring any extra information to help make its decision - its decision is already made because there is only one candidate.
And so, as a convenience, the Spring team decided #Autowired should be optional. Since its not helping the Spring framework to make a decision, its presence is just noise.
If your component has multiple constructors then you can use #Autowired on one of them to tell Spring "use this one, not that one".

Bean Injection without #Autowire Annotation in spring

How to inject a class without using auto-wire annotation in spring?
There are 3 ways to do injection with Spring:
Use #Autowired (which you don't want)
Use #Inject (has most of the features as Autowired)
Use XML configuration. This doesn't require any annotations on the classes.

What is the difference between #Inject and #Autowired

i am just wondering what is the difference between #Inject & #Autowired
when to use each one ?, or they are doing the same thing ?
and if i have a spring bean which have a scope:
#Service
#Scope("singleton")
can i make dependency injection for it with both with no problems ?
thanks in advance.
From what I know, they do the same. #Inject is an annotation from javax.inject, which is only the API for dependency injection. In Spring you can use both, as I think Spring provides an implementation for #Inject which does the same thing as #Autowired in Spring environments.
Matthias Wessendorf blogged about this here: http://matthiaswessendorf.wordpress.com/2010/04/20/spring-3-0-and-jsr-330-part-2/
How about reading the documentation?
JSR 330's #Inject annotation can be used in place of Spring's
#Autowired in the examples below. #Inject does not have a required
property unlike Spring's #Autowired annotation which has a required
property to indicate if the value being injected is optional. This
behavior is enabled automatically if you have the JSR 330 JAR on the
classpath.
I think it is worth pointing out that, if you use #Autowired, you are creating a dependency on Spring, where using #Inject, you will be able to swap out another dependency injection framework that supports JSR 330.
First, #Autowired is defined by Spring Framework but #Inject came from "Dependency Injection for Java" (JSR-330)"
Second, #Inject doesn't take required attribute so if it fails to find any bean, it will fail with an error but #Autowired can come with required=false and will allow a nullable field.
Third, Advantage of #Inject annotation is that rather than inject a reference directly, you could ask #Inject to inject a Provider. The Provider interface enables, among other things, lazy injection of bean references and injection of multiple instances of a bean.

What is the most appropriate way of injecting daos in services, services in controllers in Spring?

There are many annotations in the Spring framework like #Component, #Service, #Repository, #Service #Resource and #Autowired etc.
What is the most appropriate way of injecting my daos in services, and my service class in the Spring Controller.
With so many annotations it is getting confusing especially with #Autowired working for all situations.
#Service and #Repository are just "sub-annotations" for #Component to specify the bean a bit more (to separete Services from Repositories for more sophisticated stuff). From the point of injection this three are equal.
For injection, there are 3:
#Resource
#Inject
#Autowired
#Autowired is the most powerful annotation, but #Resource (JSR-250) and #Inject (JSR-330) are standardized. — Anyway if you not plan to reuse your application in a non-Spring environment, then I would not pay to many attention to this concern.
See Annotation based configuration in Spring, best Spring Annotation tutorial for me.
I prefer to avoid annotations, especially if they start getting confusing. Nothing wrong with good old getter and setters in this case. Just gotta wire the bean on your own, which isn't so difficult that annotations are necessary.

Resources