java - spring配置文件如何实现判断语句
ringa_lee
ringa_lee 2017-04-18 09:05:01
0
4
861
<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的一些方法了。
ringa_lee
ringa_lee

ringa_lee

reply all(4)
大家讲道理

It can be solved by using property configuration files
1. Add a property file, such as config.propertiest, and define the key-value pair dao.prefix=.
2. Configure this property file in the spring configuration file.
3. You can use attributes file variables in <bean>, such as ${dao.prefix}
4. Selective injection can be done in 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命名的规律性。
洪涛

You can use Spring's profile to solve this problem. Profiles can be used to correspond to different configurations (such as database configuration) in different environments to meet your needs.

The following configures three different attributes of the same bean under three profiles (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>

How to enable a profile when the program starts? There are many ways, just choose one:

Code method

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

java command-Dspring.profiles.active="dev"

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

web.xml

If it is a web project, you can configure web.xml

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

Starting with different profiles will load different beans and different configurations.

迷茫
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;
    }
}
巴扎黑

I don’t know if spel can solve the problem, you can search it and see

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template