欢迎支持笔者新作:《深入理解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中的数据源配置如下:
- <bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${mysql_driver}"/>
- <property name="url" value="${mysql_url}"/>
- <property name="username" value="${mysql_username}"/>
- <property name="password" value="${mysql_password}"/>
- </bean>
- <bean id="oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${ora_driver}"/>
- <property name="url" value="${ora_url}"/>
- <property name="username" value="${ora_username}"/>
- <property name="password" value="${ora_password}"/>
- </bean>
可以看到如${mysql_driver}这个可以自动读取到config.properties文件中的相应字段的值。
那么java程序中如何获取呢?且看下面这个测试用例:
- package com.shr.service.userManage;
-
- import java.util.List;
-
- import javax.inject.Inject;
-
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.transaction.TransactionConfiguration;
- import org.springframework.transaction.annotation.Transactional;
-
- import com.shr.dao.model.userManage.UserListInfo;
-
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("file:WebContent/WEB-INF/applicationContext.xml")
- @Transactional
- @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
- public class UserManageServiceTest {
-
- @Inject
- private UserManageService userManageService;
-
- @Value("${ora_driver}")
- private String driver;
- @Value("${ora_url}")
- private String url;
- @Value("${ora_username}")
- private String username;
- @Value("${ora_password}")
- private String password;
-
- @Test
- public void testConfigProperties()
- {
- System.out.println(driver);
- System.out.println(url);
- System.out.println(username);
- System.out.println(password);
- }
- }
运行结果:
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"/>这个替换成:
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:*.properties</value>
- </list>
- </property>
- </bean>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
这样同样可以通过@Value("${ora_driver}")这样的方式获取值,也可以通过@Value("#{configProperties['ora_driver']}")的方式获取值,可以看出后一种方法稍微复杂点,建议采用前一种方式。
欢迎跳转到本文的原文链接:https://honeypps.com/java/spring-mybatis-multi-datasource-props-3/
欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。