spring下獲取Properties方式
比如已有的commonConfig.properties
main.db.driverClassName=com.mysql.jdbc.Driver main.db.url=jdbc\:mysql\://cloudpkdbrw.xxx.com\:3306/huagang?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull main.db.username=huagang main.db.password=xxxHGtest
在spring中引用commonConfig.properties
第1種.直接在spring的xml中使用
<!-- 加載配置文件 --> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:/resources/config/commonConfig.properties</value> </property> </bean>
<!--
或者
引入多配置文件
classpath:/resources/config/commonConfig.properties
<context:property-placeholder location=
"
,classpath:XXX.properties"
/>
-->
<!-- 配置數據源 --> <bean id="ajbDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!--驅動類 --> <property name="driverClass"> <value>${main.db.driverClassName}</value> </property> <!--url連接串 --> <property name="jdbcUrl"> <value>${main.db.url}</value> </property> <!--用戶名 --> <property name="user"> <value>${main.db.username}</value> </property> <!--密碼 --> <property name="password"> <value>${main.db.password}</value> </property> <!-- 連接池中保留的最小連接數 最小鏈接數 --> <property name="minPoolSize"> <value>1</value> </property> <!--連接池中保留的最大連接數 最大連接數 --> <property name="maxPoolSize"> <value>4</value> </property> <!-- 最大空閑的時間,單位是秒,無用的鏈接再過時后會被回收 --> <property name="maxIdleTime"> <value>1800</value> </property> <!-- 在當前連接數耗盡的時候,一次獲取的新的連接數 --> <property name="acquireIncrement"> <value>1</value> </property> <!--JDBC的標准參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements 屬於單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。 如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0--> <property name="maxStatements"> <value>0</value> </property> <!-- 連接池初始化時獲取的鏈接數,介於minPoolSize和maxPoolSize之間 --> <property name="initialPoolSize"> <value>1</value> </property> <!--每1分鍾檢查所有連接池中的空閑連接。Default: 0 --> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <!--定義在從數據庫獲取新連接失敗后重復嘗試的次數。Default: 30 --> <property name="acquireRetryAttempts"> <value>30</value> </property> <!-- #每100ms嘗試一次 --> <property name="acquireRetryDelay"> <value>100</value> </property> <!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試獲取連接。如果設為true,那么在嘗試 獲取連接失敗后該數據源將申明已斷開並永久關閉。Default: false --> <property name="breakAfterAcquireFailure"> <value>false</value> </property> <!-- 防止長時間閑置而導致被mysql斷開 因性能消耗大請只在需要的時候使用它。如果設為true那么在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提升連接測試的性能。Default: false --> <property name="testConnectionOnCheckout"> <value>false</value> </property> <!--如果設為true那么在取得連接的同時將校驗連接的有效性。Default: false --> <property name="testConnectionOnCheckin"> <value>true</value> </property> <!--定義所有連接測試都執行的測試語句。在使用連接測試的情況下這個一顯著提高測試速度。注意: 測試的表必須在初始數據源的時候就存在。Default: null--> <property name="preferredTestQuery"> <value>select 1 from dual</value> </property> </bean>
第2種:在java 啟動加Conifg庫中或者在controller中調用
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Config { @Value("${main.db.url}") public String jdbcUrl; }
controller
@RequestMapping("/service/**") @Controller public class TestController{ @Value("${main.db.url}") private String jdbcUrl; //直接在Controller引用 @RequestMapping(value={"/test"}) public ModelMap test(ModelMap modelMap) { modelMap.put("jdbcUrl", Config.jdbcUrl); return modelMap; } }
第3種,不要在spring.xml中引用commonConfig.properties,在類注入時引用,然后使用Environment獲取它的值
import org.apache.commons.lang3.tuple.Pair; import org.redisson.Config; import org.redisson.Redisson; import org.redisson.SentinelServersConfig; import org.redisson.SingleServerConfig; import org.redisson.client.RedisClient; import org.redisson.client.RedisConnection; import org.redisson.client.protocol.RedisCommands; import org.redisson.codec.SerializationCodec; import org.redisson.misc.URIBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; @Configuration @PropertySource( "classpath:resources/config/commonConfig.properties" ) public class RedissonConfig { @Autowired private Environment env; @Bean public SerializationCodec serializationCodec() { return new SerializationCodec(); } @Bean public Config reddissonConfig() throws Exception { String jdbcUrl= env.getProperty("main.db.url"); } //此為代碼片段
第4種,不需要借用spring,直接在類中讀取.但要注意:(redisson.properties配置文件中不能有.句號),否則將報錯
import java.util.ResourceBundle; public class RedissionParamsUtil { /** 配置文件地址 */ private final String configPath = "resources/config/redisson.properties"; private static RedissionParamsUtil paramsUtil; ResourceBundle bundle = null; /** * 單例模式獲取實例 * @return MenuService */ public static RedissionParamsUtil getInstance(){ if(null==paramsUtil){ paramsUtil = new RedissionParamsUtil(); } return paramsUtil; } /** * 構造方法 */ private RedissionParamsUtil(){ bundle = ResourceBundle.getBundle(configPath); } public String getValue(String key){ return bundle.getString(key); } public static void main(String[] args) { System.out.println(RedissionParamsUtil.getInstance().getValue("jdbc_url")); } }