#AutoWired without #Qualifier - spring

If there are two beans of the same type but with different names. Will spring Autowire the bean based on the name without us adding #Qualifier on the variable? I saw in the documentation, "As a fallback Spring uses the bean name as a default qualifier value".
#Component
class A{
}
#Component
class B extends A{
}
class C{
#AutoWired
A a;
//Will a be of type class A, even without #Qualifier...?
}

If there are two beans of the same type but with different names. Will spring Autowire the bean based on the name without us adding #Qualifier on the variable?
#Autowire in the first place cares about type, later about the name. You will get exception saying that there are multiple candidates for injection while only 1 is expected.
#Resource on the other hand, cares about name first, type later.

In this case, B will get injected because there is an A and a B for Spring to choose from and only one of them matches what the #Autowired field is asking for (Class B, because A is not assignable to B).
However, if you had two B's, you would have to qualify it or mark one of them as the primary.
For example, given this:
#Configuration
public class MyConfig {
#Bean
public B example1() {
return new B();
}
#Bean
public B example2() {
return new B();
}
}
Well, now you have two instances of B, with different names. You can fix this one of two ways:
Qualify your Autowire
Note that I'm using field injection here, you really should use constructor, I'm doing it to save space.
#Component
public class SomeComponent {
#Autowired
#Qualifier("example1")
private B b;
}
Or
Mark A Bean as Primary
Redefine the beans, marking one as #Primary
#Configuration
public class MyConfig {
#Bean
#Primary // <-------- NEW!
public B example1() {
return new B();
}
#Bean
public B example2() {
return new B();
}
}
And then inject without needing to name it:
#Component
public class SomeComponent {
#Autowired // (Will pick Primary)
private B b;
}

Related

Spring: Qualifying different types with the same name

I have two classes that I want to autowire using spring
#Component
public class Restaurant {
#Autowired
#Qualifier("HighClass")
private CoffeeMaker coffeeMaker;
}
and:
public class CappuccinoMaker implements CoffeeMaker{
#Autowired
#Qualifier("HighClass")
int numOfSpoons;
}
Then injecting:
#Bean(name="HighClass")
#Scope("prototype")
public CoffeeMaker HighClassCoffeeMakerGenerator() {
return new CappuccinoMaker();
}
#Bean(name="HighClass")
public int getNumOfSpoons() {
return 3;
}
I'd like to qualify both the int and the CoffeeMaker with "HighClass". In Guice it is possible to annotate different types with the same annotation and inject them correctly.
It seems like in spring this is not allowed. When I try injecting the fields I get an error that the required bean was not found. Did I miss anything?
Bean name in Spring is unique. Use #Profile in order to pick a bean from several competing variants.

Autowiring of beans in spring boot with #Qualifier annotation

I am learning autowiring in Spring Boot using #Primary and #Qualifier annotations. I am able to understand that #Primary wires the annotated class as a dependency and in case more than one satisfying classes are found #Qualifier can come to help.
#Component
public class VehicleBean {
#Autowired
#Qualifier("car")
Vehicle car;
public void check() {
car.details();
}
public Vehicle getCar() {
return car;
}
public void setCar(Vehicle car) {
this.car = car;
}
}
Bike
#Component
//#Primary
#Qualifier("car")
public class Bike implements Vehicle {
#Override
public void details() {
System.out.println("Bike is driving");
}
}
Car
#Component
//#Primary
#Qualifier("bike")
public class Car implements Vehicle {
#Override
public void details() {
System.out.println("Car is driving");
}
}
When I add #Qualifier("car") on my autowired dependency named as "bike" and have #Qualifier("car") on Car and #Qualifier("bike") on Bike, it picks up Car. However, when I interchange the #Qualifier on Bike and Car(e.g - #Qualifier("bike") on Car and vice versa) it picks up the bike. Also when I change the #Qualifier to "bike on my autowired dependency named as "car" and have #Qualifier("car") on Bike and vice verse, it is picking Car. I was expecting Bike to be picked. What am I missing?
The use of qualifier annotation here is incorrect, the qualifier annotation is used on a field, a parameter or a method to pick the write candidate bean while authowiring,
This annotation may be used on a field or parameter as a qualifier for
candidate beans when autowiring. (JAVA DOC)
In simple word, you define the beans by name and you tell spring what name to pick by adding #Qualifier, in your example you have to add the bean name in #Component.
#Component("bike")
public class Bike implements Vehicle {
#Override
public void details() {
System.out.println("Bike is driving");
}
}
#Component("car")
public class Car implements Vehicle {
#Override
public void details() {
System.out.println("Car is driving");
}
}
Coming to your question, why it picks Car? , because the AnnotationBeanNameGenerator generate a default name if no one specified and the built name is based on the short name of the class (with the first letter lower-cased):
If the annotation's value doesn't indicate a bean name, an appropriate
name will be built based on the short name of the class (with the
first letter lower-cased). For example:
com.xyz.FooServiceImpl -> fooServiceImpl (JAVA DOC)
#Qualifier annotation won't work here #Component #Qualifier("car"). It can be used on a field, a parameter or a method for autowiring.
You need to pass the name with #Component annotation only, like #Component("car")

Autowiring a list in spring, without configuration

How can I autowire a list using java, without configuration?
Say I have the following classes:
public abstract class A {
public abstract doStuff();
}
public class B extends A {
#Override
public void doStuff() {
System.out.println("I'm B");
}
}
public class C extends A {
#Override
public void doStuff() {
System.out.println("I'm C");
}
}
And I have the class
public class Aggregator {
#Autowired
private List<A> stuffDoers;
private void doAllStuff() {
for(A a:stuffDoers) {
a.doStuff();
}
}
}
How can I autowire some of A's implementation into the Aggregator without configuring a list in XML?
EDIT: I'm looking for a way to be able to control the members of a list
#Autowired always works with instances of a class, not types. You have defined 3 types: A, B and C, but have not created any instances from them.
To autowire, you need to create these instances, and also register them with Spring. This is where the XML config or Java config comes in. It is basically a short form for creating Spring-registered instances. So you can specify:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class ApplicationConfiguration {
#Bean
public B someA() {
return new B();
}
#Bean
public C anotherA() {
return new C();
}
#Bean
public B evenMoreA() {
return new B();
}
}
This gives you 3 independent instance beans (not a list). For more details, see Java-based container configuration.
Now, Spring will do the job of finding all beans of type A in those packages, and populate your Aggregator class with all three beans correctly.
NOTE: These beans don't need to be in the same file. They can be declared anywhere in your #ComponentScan packages.
As asked in a comment, what if you want to have only some of those instances?
If you want only some of the beans added to your list, the situation is more tricky. You will need to move the excluded beans to a separate #Configuration class, in a different package. You should not add that new package to the Spring #ComponentScan packages, so Spring will not find those beans to add to the list. As far as I know, this is the only way.
Of course, if you want only a single bean, then as usual, you have to autowire it using #Qualifier and specify the bean name. In this case, you don't use a List, just the variable of type A.

Implement factory design pattern with spring annotaion

I have a factory class that should return me an instance of classA or classB. These classes implement interface XYZ:
interface xyz;
getInstance()
#service
classA implements xyz{
public void checkStatus(){
}
}
#service classB implements xyz{
public void checkStatus(){
}
}
Factory class:
#component
class ABCFactory{
#Autowire classA A;
public static getInstance(str a){
return classA;
}
}
Client code:
Class A a = ABCFactory.getInstance("A");
a.checkStatus();
I get null pointer exception -- probably a is returned as null?
What is the best approach to implement the factory pattern with spring annotation and autowired bean?
It's difficult to say with any certainty why your auto-wiring isn't taking place without seeing your Spring config. My first guess is that you don't have component scanning switched on:
<context:component-scan base-package="org.example"/>
Adding something like that to your spring config file (with the correct package) will tell Spring to scan the package structure for fields that need to be auto-wired, and services that can be used for auto-wiring.
static is the root of all evil. How did you managed to access classA here?
#Component
class ABCFactory{
#Autowire classA A;
public static getInstance(str a){
return classA;
}
}
A field is not static while getInstance() method is - your code won't compile.
Furthermore, dependency injection works on instances, not on static classes. Thus you should get rid of static:
#Component
class ABCFactory {
#Autowire classA A;
public xyz getInstance(str a){
return A;
}
}
and inject ABCFactory where you need it (e.g. in other services or controllers):
#Autowired
private ABCFactory factory;
BTW your design looks supicious in Spring environment, what do you want to achieve?

Problem with Autowiring & No unique bean

I have 2 classes (B,C) extends class A.
#Service
public class A extends AbstratClass<Modele>{
#Autowired
A(MyClass br) {
super(br);
}
#Service
public class B extends A{
#Autowired
B (MyClass br) {
super(br);
}
#Service
public class C extends A{
#Autowired
C (MyClass br) {
super(br);
}
But i have this message:
No unique bean of type [A] ] is defined: expected single matching bean but found 2: [A, B, moveModeleMarshaller]
I really cant get why i have this message & how to resolve even after reading Spring documentation.
Thanks in advance.
You should rewrite your class to something like this with the #Qualifier annotation.
#Service
#Qualifier("a")
public class A extends AbstratClass<Modele>{
#Autowired
A(MyClass br) {
super(br);
}
#Service
#Qualifier("b")
public class B extends A{
#Autowired
B (MyClass br) {
super(br);
}
#Service
#Qualifier("c")
public class C extends A{
#Autowired
C (MyClass br) {
super(br);
}
You must also use the #Qualifier annotation on the instance of type A you're autowiring the Spring bean into.
Something like this:
public class Demo {
#Autowired
#Qualifier("a")
private A a;
#Autowired
#Qualifier("b")
private A a2;
public void demo(..) {..}
}
If you don't like to have this Spring configuration in your production code, you have to write the dependency injection logic with XML or Java configuration instead.
You can also specify a default bean of type A with the #Primary annotation above one of your service classes that extends type A. Then Spring can autowire without specifying the #Qualifier annotation.
Since Spring will never try to guess which bean to inject, you have to specify which one or mark one of them with #Primary as long as its more than one bean of a type.
You are trying (somewhere else) to autowire a bean of type A. Something like:
#Autowired
private A beanA;
But you have 2 beans that conform to this.
You can resolve this by using #Resource and specifying which bean exactly:
#Resource("b")
private A beanA;
(where "b" is the name of the injected bean) or using the #Qualifier annotation.
Generally you will get this error when defined two beans with same class
<bean id="a" class="com.package.MyClass"/>
<bean id="b" class="com.package.MyClass"/>
if you address the above two line we have two beans with same class.
when you trying to autowire this class in any other classed you will get this type of error
You have two solutions
First Method
use qualifier by defining a bean id init
like this
#Autowired
#Qualifier("a")
MyClass a;
#Autowired
#Qualifier("b")
MyClass b;
Second Method
use JSR250 api(its a jar file you can put into your class path
Then do autowriring like below
#Resource("a")
MyClass a
#Resource("b")
MyClass a

Resources