<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的一些方法了。
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:
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):
How to enable a profile when the program starts? There are many ways, just choose one:
Code method
java command-Dspring.profiles.active="dev"
web.xml
If it is a web project, you can configure web.xml
Starting with different profiles will load different beans and different configurations.
I don’t know if spel can solve the problem, you can search it and see