登录

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

<bean id="systemUserService" class="com.alan.SystemUserServiceImpl">
        <if ***>
            <property name="exm1" ref="exm1"></property>
        </if>
        <else if ***>
         <property name="exm2" ref="exm2"></property>
        </else if>
        <else>
        <property name="exm3" ref="exm3"></property>
        </else>
        
</bean>

不知道有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的一些方法了。
# Java
ringa_lee ringa_lee 2285 天前 580 次浏览

全部回复(4) 我要回复

  • 大家讲道理

    大家讲道理2017-04-18 09:07:01

    用属性配置文件可以解决
    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命名的规律性。

    回复
    0
  • PHP中文网

    PHP中文网2017-04-18 09:07:01

    可以使用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和不同的配置。

    回复
    0
  • 迷茫

    迷茫2017-04-18 09:07:01

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

    回复
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:07:01

    不知道spel能不能解决问题,你可以搜下看看

    回复
    0
  • 取消 回复 发送