What is the difference between #Inject and #Autowired - spring

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.

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.

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.

When to use Spring #Autowire annotation

Recently I had discussion with my friend regarding usage of Spring #Autowire annotation on entity(JPA) classes.
In our project we are using #Autowire annotaion to inject Entity but my friend suggesting not to use #Autowire annotaions on entity classes. When I asked why? He dont have the proper answer for that. So i just wanted to know are there any disadvantages using #Autowire annotaion on entity classes.
Also please explain when to go for #Autowire annotaion or not with example.
Thank in advance.
#Entity and #Autowire are not interchangeable.
#Entity annotation indicates that the JavaBean is a persistent entity.This is actually a JPA annotation and not a Spring Annotation.
#Entity will be used in the sessionFactory by the packagesToScan poroerty.
#Autowired: inject a resource by-type, i.e. by the class or by the interface of the annotated field or contractor. See my answer Inject and Resource and Autowired annotations
#Autowired is used to inject dependencies as an alternative to setting it via xml configurations
Maybe this answer will help you understand
Hibernate - spring annotated entities not scanned from within jar
UPDATE:
Following the comment bellow:
Company is your domain object, so you don't need to use spring in this case.
<bean id="company" class="xxx.Company"/>
The above will return the same instance with #autowire.
Even if you switch to scope="prototype" I don't see any reason to use spring for that.
You should have a service that will be used to CRUD company e.g.
CompanyService, this service will be a single tone so you will use #Autowire to inject it to the controller and it will use your JPA framework to implement CRUD's
To create a new company you will use:
Company c = new Company //this probably will be binded from your ui form
companyServic.saveOrUpdate(c);
See the following answer spring rest service - hibernate dao - annotations - pojo - namedqueries.
For common practice of DAO and services.
#Autowire is an annotation used to perform a dependency injection, its almost similar to the standard #Inject you can take a look at the spring reference manual to see the difference between those two annotations.
#Entity is a part of the jpa framework its used to mark a class as persistent, spring does not implement an equivalent annotation.

Use Spring annotation to inject dependency

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

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