Autowiring spring bean by name using annotation - spring

In Springs latest version, we can autowire a bean using annotation as #Autowired. This will autowire the bean using its type(or constructor, if applied on it).
Is there any way I can use the #Autowired annotation based on the bean name which we were doing without annotation in Spring's XML file as autowire="byName"?

You can use:
#Autowired
#Qualifier("beanname")
According to the #Qualifier javadoc
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring

You can use JSR-250 #Resource for by-name bean autowiring, unless you need constructor injection or multi-parameter method injection.
From the docs:
If you intend to express annotation-driven injection by name, do not primarily use #Autowired, even if is technically capable of referring to a bean name through #Qualifier values. Instead, use the JSR-250 #Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself e.g. #Service (“employeeManager”).
Then using below code you can enable autowire by Name
#Autowired
#Qualifier("employeeManager")
private EmployeeManagerService employeeManagerService;

I was using bean name proxy which was messing up autowiring by name. #Resource didn't have that issue since it doesn't care about type. So now I know one reason for this recommendation by Spring developers :-) Just FYI

Use #Component("beanname") in the java class definition of your bean
Then while autowiring use JSR 330
#Inject
#Named(Value="beanname")

Related

Is there a way to mark a bean as not primary in Spring?

I am having two beans in different packages out of which one of them is a library so cannot edit, and execution gives the
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type '' available:
For current purpose I want the bean in the library to be the primary bean, is there a way to avoid using the second bean which i can edit , without deleting the bean?
So far have only seen the #Primary annotation
when spring is trying to inject the dependency it found to to beans for it .
so you have add #Qualifier annotation and give the corresponding class in it which you want to inject

How does #Autowired work with parameterized types that are not initialized?

The example I am looking at is KafkaTemplate.
The KafkaTemplate is Autowired as KafkaTemplate.
The configuration I am using has a ProducerFactory created.
Both constructors of KafkaTemplate require the use of this kind of factory.
I want to guess that any new instance of a class autowired is using Class.isAssignableFrom(Class) to find an appropriate dependency for creating the parameterized instance.
Note: The original reason this spawned investigation is the change in behavior between autowiring in KafkfaTemplate and KafkaTemplate(No types).
Is this assumption correct?
If so, what is the best way to design similar configuration?
Is there a reason to use Kafka's implementation over using the interface 'FactoryBean'?
Notice that Autowired has required property, you can use to mark bean as required
required
Declares whether the annotated dependency is required.
#Autowired(required=true)
KafkaTemplate kafkaTemplate

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.

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 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.

Resources