Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to inject parent class property with spring annotation

Tags:

java

spring

parent class is like this:

public class BaseDAO{
    private DBRoute defaultDB;

    public DBRoute getDefaultDB()
    {
        return this.defaultDB;
    }

    public void setDefaultDB(DBRoute defaultDB)
    {
        this.defaultDB = defaultDB;
    }
}

I have create beans like below:

<bean id="adsConfigDB" class="net.flyingfat.common.dbroute.config.DBRoute">
    <constructor-arg value="adsConfig" />
</bean>

<bean id="adsBizDateDB" class="net.flyingfat.common.dbroute.config.DBRoute">
    <constructor-arg value="adsBizDate" />
</bean>

I want to inject superclass property defaultDB in subclass through byName, not byType, which is in subclass inject defaultDB using adsConfigDB or adsBizDateDB. Is there any way to do this with spring annotations? I already tried Autowired or Resource with constructor which doesn't work. By the way, I already know this can be done using XML.

like image 293
TonyArcher Avatar asked Nov 09 '22 10:11

TonyArcher


1 Answers

@Qualifier annotation – This annotation is used to avoid conflicts in bean mapping and we need to provide the bean name that will be used for autowiring. This way we can avoid issues where multiple beans are defined for same type. This annotation usually works with the @Autowired annotation. For constructors with multiple arguments, we can use this annotation with the argument names in the method.

Your code will be like this..

@Autowired
@Qualifier("adsConfig")
private DBRoute defaultDB;
like image 164
Bhargav Kumar R Avatar answered Nov 14 '22 22:11

Bhargav Kumar R