java - spring配置文件如何实现判断语句

 飞隔热条二 发布于 2022-10-29 16:04

        
            
        
        
         
        
        
        
        
        

不知道有spring的配置文件有没有以上的实现,根据判断条件来决定注入哪一个对象
++++++++++++++++++++++++++分隔线+++++++++++++++++++++++++++++++++

我所想到的另一途径是
1、新建一个.properties文件,来定义常量,如dubboOrSql=1
2、新建一个工具类ConstantsConfig来读取上面的.properties文件的常量。

3、在serviceImpl类中,对此进行判断 ,来决定实例化哪个对象.

ISystemData sd;
String dubboOrSql = ConstantsConfig.getDubboOrSql();
if("1".equals(dubboOrSql)){
    sd = app.getBean("exap1");
}else if("0".equals(dubboOrSql)){
    sd = app.getBean("exap2");
}

//后面就是调用sd的一些方法了。
4 个回答
  • 不知道spel能不能解决问题,你可以搜下看看

    2022-10-31 01:08 回答
  • 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;
        }
    }
    2022-10-31 01:09 回答
  • 可以使用Spring的profile来解决这个问题。profile可以用于在不同的环境下对应不同配置(例如数据库配置),可以实现你的需求。

    下面配置了三个同一个bean在三个profile(dev、test、product)下的不同属性:

    <beans profile="dev">
        <bean id="systemUserService" class="com.alan.SystemUserServiceImpl">
            <property name="exm1" ref="exm1"></property>
        </bean>
    </beans>
    <beans profile="test">
        <bean id="systemUserService" class="com.alan.SystemUserServiceImpl">
            <property name="exm2" ref="exm2"></property>
        </bean>
    </beans>
    <beans profile="product">
        <bean id="systemUserService" class="com.alan.SystemUserServiceImpl">
            <property name="exm3" ref="exm3"></property>
        </bean>
    </beans>

    如何在程序启动是启用某个profile,有多种方式,选其一即可:

    代码方式

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().setActiveProfiles("dev");

    java命令-Dspring.profiles.active="dev"

    java -jar test.jar -Dspring.profiles.active="dev"

    web.xml

    如果是web项目,可以配置web.xml

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>

    使用不同的profile启动,就会加载不同的bean和不同的配置。

    2022-10-31 01:09 回答
  • 用属性配置文件可以解决
    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命名的规律性。
    2022-10-31 01:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有