Spring+mybatis Multiple Data source configuration (iii)--spring How to obtain information about a properties file

Source: Internet
Author: User

Strictly speaking, the contents of this blog post are not necessarily related to this series, the theme of this blog post is: How to use spring to get information about properties files. The use cases used in this blog post are related to this series, so let's put it here.

Through spring's configuration (applicationcontext.xml), such as: <context:property-placeholder location= "Classpath:config.properties"/ > (refer to the first two posts in this series), which is configured with spring to automatically load the Config.properties file under the current classpath, and this config.properties content is as follows:

# 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
The data sources in Applicationcontext.xml are configured as follows:

	<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}"/>
		Name= "username" 	value= "${ora_username}"/> <property name= "password" value= " 	${ora_password} "/>
	</bean>
You can see a value such as ${mysql_driver} that can automatically read to the corresponding field in the Config.properties file.

So how to get it in Java programs. And look at the following test case:

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

Run Result:

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

You can see in the code above that you can get config.properties values by using the annotation @Value ("${ora_driver}").

These are relatively brief configurations, and can also be configured for complex points. Can be put: <context:property-placeholder location= "Classpath:config.properties"/> This replacement into:

<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>
This can also be obtained by @value ("${ora_driver}"), or by the way @value ("#{configproperties[' Ora_driver ')}"), to see that the latter method is slightly more complex. The previous approach is recommended.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.