PANKAJ DUBEY
PANKAJ DUBEY

Reputation: 173

why @Qualifier not allowed above constructor?

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;
}

Upvotes: 13

Views: 22503

Answers (1)

Chetan Agarwal
Chetan Agarwal

Reputation: 86

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.

Upvotes: 7

Related Questions