关键词搜索

源码搜索 ×
×

Spring+Mybatis多数据源配置(三)——Spring如何获取Properties文件的信息

发布2015-11-20浏览7316次

详情内容

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/java/spring-mybatis-multi-datasource-props-3/

严格来说,本博文所阐述的内容和这个系列来说,没有什么必要的关系,本博文的主题是:如何使用spring获取properties文件的信息。本博文所采用的用例都和这个系列有关,所以就放在这里讲了。

通过spring的配置(applicationContext.xml),如:<context:property-placeholder location="classpath:config.properties"/>(具体位置可以参考本系列的前两篇博文),这个就配置了spring自动加载当前classpath下的config.properties文件,这个config.properties的内容如下:

 

# oracle configuration
 ora_driver=oracle.jdbc.driver.OracleDriver
 ora_url=jdbc:oracle:thin:@10.10.195.185:1521:sp5000
 ora_username=shr
 ora_password=shr

 #mysql configuration
 mysql_driver=com.mysql.jdbc.Driver
 mysql_url=jdbc:mysql://10.10.193.111:3306/sp5000?useUnicode=true&characterEncoding=UTF-8
 mysql_username=shr
 mysql_password=shr

 dataSource=oracle

对于applicationContext.xml中的数据源配置如下:

 

 

  1. <bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  2. <property name="driverClassName" value="${mysql_driver}"/>
  3. <property name="url" value="${mysql_url}"/>
  4. <property name="username" value="${mysql_username}"/>
  5. <property name="password" value="${mysql_password}"/>
  6. </bean>
  7. <bean id="oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  8. <property name="driverClassName" value="${ora_driver}"/>
  9. <property name="url" value="${ora_url}"/>
  10. <property name="username" value="${ora_username}"/>
  11. <property name="password" value="${ora_password}"/>
  12. </bean>

可以看到如${mysql_driver}这个可以自动读取到config.properties文件中的相应字段的值。

那么java程序中如何获取呢?且看下面这个测试用例:

 

 

  1. package com.shr.service.userManage;
  2. import java.util.List;
  3. import javax.inject.Inject;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import org.springframework.test.context.transaction.TransactionConfiguration;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import com.shr.dao.model.userManage.UserListInfo;
  12. @RunWith(SpringJUnit4ClassRunner.class)
  13. @ContextConfiguration("file:WebContent/WEB-INF/applicationContext.xml")
  14. @Transactional
  15. @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
  16. public class UserManageServiceTest {
  17. @Inject
  18. private UserManageService userManageService;
  19. @Value("${ora_driver}")
  20. private String driver;
  21. @Value("${ora_url}")
  22. private String url;
  23. @Value("${ora_username}")
  24. private String username;
  25. @Value("${ora_password}")
  26. private String password;
  27. @Test
  28. public void testConfigProperties()
  29. {
  30. System.out.println(driver);
  31. System.out.println(url);
  32. System.out.println(username);
  33. System.out.println(password);
  34. }
  35. }

 

运行结果:

 

oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@10.10.195.185:1521:sp5000
shr
shr

 

在上面的代码中可以看到通过使用注解 @Value("${ora_driver}")就可以获取config.properties的值。

这些是比较简要的配置,也可以配置的复杂点。可以把:<context:property-placeholder location="classpath:config.properties"/>这个替换成:

 

  1. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  2. <property name="locations">
  3. <list>
  4. <value>classpath:*.properties</value>
  5. </list>
  6. </property>
  7. </bean>
  8. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  9. <property name="properties" ref="configProperties" />
  10. </bean>

这样同样可以通过@Value("${ora_driver}")这样的方式获取值,也可以通过@Value("#{configProperties['ora_driver']}")的方式获取值,可以看出后一种方法稍微复杂点,建议采用前一种方式。
 

欢迎跳转到本文的原文链接:https://honeypps.com/java/spring-mybatis-multi-datasource-props-3/

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载