3 回答

TA贡献1772条经验 获得超6个赞
用属性配置文件可以解决
1、新增一属性文件,如config.propertiest,定义键值对dao.prefix=.
2、在spring配置文件中配置这个属性文件。
3、即可在<bean>中使属性文件的变量,如${dao.prefix}
4、在service中可以进行选择性注入:
<bean id='sysService' class='com.alan.***Impl>
<property name='***' ref='system${dao.prefix}Dao'>
</bean>
<bean id='systemDao' class='***'/>
<bean id='systemDubboDao class='***'/>
<bean id='systemOtherDao class='***'/>
这样配置的话,就会根据dao.prefix的值来决定注入哪一个DAO了。当dao.prefix=null(即什么都不填是),ref='systemDao'。关键在于bean命名的规律性。

TA贡献2037条经验 获得超6个赞
SystemUserServiceImpl implements InitializingBean, ApplicationContextAware {
private ApplicationContext applicationContext;
public void afterPropertiesSet() throws java.lang.Exception {
if (***) {
this.exm1 = applicationContext.getBean("exap1");
} else {
this.exm2 = applicationContext.getBean("exap2");
}
}
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.applicationContext= ctx;
}
}
添加回答
举报