Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why @Qualifier not allowed above constructor?

Tags:

java

spring

I am learning spring but while i tried below it doesn't work but at the place of constructor while I use method then it works why? Is there any specific reason behind it? My question is why spring designers decided not to allow @Qualifier above constructor but above method?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {
    private Company comp;
    @Autowired
    @Qualifier(value="beanId")
    private Employee(Company comp) {
        this.comp=comp;
    }
    public Company getComp() {
        return comp;
    }
}

@Qualifier within argument works.. say below works it's ok

private Employee(@Qualifier(value="beanId") Company comp) {
        this.comp=comp;
}

But @Qualifier works fine above method like below why?

@Qualifier(value="beanId")
private void getEmpDetails(Company comp) {
        this.comp=comp;
}
like image 665
PANKAJ DUBEY Avatar asked Feb 20 '17 17:02

PANKAJ DUBEY


1 Answers

Yes for constructors you can not use @Qualifier as you use it for other methods.
@Qualifier annotation can be used only for the constructor arguments.

See this offcial article for more reference.

like image 123
Chetan Agarwal Avatar answered Sep 21 '22 15:09

Chetan Agarwal