简体   繁体   中英

how to inject parent class property with spring annotation

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM