最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 獲取properties的幾種方式

 更新時(shí)間:2021年04月20日 09:33:28   作者:幸福眼淚  
這篇文章主要介紹了Java 獲取properties的幾種方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下

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>  <!--或者 引入多配置文件
       <context:property-placeholder location="classpath:/resources/config/commonConfig.properties,classpath:XXX.properties"/> --> 
 
      
    <!-- 配置數(shù)據(jù)源 -->
    <bean id="ajbDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!--驅(qū)動(dòng)類 -->
        <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>
        <!-- 連接池中保留的最小連接數(shù) 最小鏈接數(shù) -->
        <property name="minPoolSize">
            <value>1</value>
        </property>
        <!--連接池中保留的最大連接數(shù) 最大連接數(shù) -->
        <property name="maxPoolSize">
            <value>4</value>
        </property>
        <!-- 最大空閑的時(shí)間,單位是秒,無(wú)用的鏈接再過(guò)時(shí)后會(huì)被回收 -->
        <property name="maxIdleTime">
            <value>1800</value>
        </property>
        <!-- 在當(dāng)前連接數(shù)耗盡的時(shí)候,一次獲取的新的連接數(shù) -->
        <property name="acquireIncrement">
            <value>1</value>
        </property>
        <!--JDBC的標(biāo)準(zhǔn)參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。但由于預(yù)緩存的statements
              屬于單個(gè)connection而不是整個(gè)連接池。所以設(shè)置這個(gè)參數(shù)需要考慮到多方面的因素。
              如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0-->
        <property name="maxStatements">
            <value>0</value>
        </property>
        <!-- 連接池初始化時(shí)獲取的鏈接數(shù),介于minPoolSize和maxPoolSize之間 -->
        <property name="initialPoolSize">
            <value>1</value>
        </property>
        <!--每1分鐘檢查所有連接池中的空閑連接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
        <!--定義在從數(shù)據(jù)庫(kù)獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
        <!-- #每100ms嘗試一次 -->
        <property name="acquireRetryDelay">
            <value>100</value>
        </property>
        <!--獲取連接失敗將會(huì)引起所有等待連接池來(lái)獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效 保留,并在下次調(diào)用getConnection()的時(shí)候繼續(xù)嘗試獲取連接。如果設(shè)為true,那么在嘗試 
            獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關(guān)閉。Default: false -->
        <property name="breakAfterAcquireFailure">
            <value>false</value>
        </property>
        <!-- 防止長(zhǎng)時(shí)間閑置而導(dǎo)致被mysql斷開 因性能消耗大請(qǐng)只在需要的時(shí)候使用它。如果設(shè)為true那么在每個(gè)connection提交的 時(shí)候都將校驗(yàn)其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 
            等方法來(lái)提升連接測(cè)試的性能。Default: false -->
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
        <!--如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。Default: false --> 
        <property name="testConnectionOnCheckin">
            <value>true</value>
        </property>
        <!--定義所有連接測(cè)試都執(zhí)行的測(cè)試語(yǔ)句。在使用連接測(cè)試的情況下這個(gè)一顯著提高測(cè)試速度。注意:
            測(cè)試的表必須在初始數(shù)據(jù)源的時(shí)候就存在。Default: null-->
        <property name="preferredTestQuery">
            <value>select 1 from dual</value>
        </property>
    </bean>

第2種:在java 啟動(dòng)加Conifg庫(kù)中或者在controller中調(diào)用

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,在類注入時(shí)引用,然后使用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配置文件中不能有.句號(hào)),否則將報(bào)錯(cuò)

import java.util.ResourceBundle;


public class RedissionParamsUtil {
    
    /** 配置文件地址 */
    private final String configPath = "resources/config/redisson.properties";
 
 
    private static RedissionParamsUtil paramsUtil;
    
    ResourceBundle bundle = null;
    



    /**
     * 單例模式獲取實(shí)例
     * @return MenuService
     */
    public static RedissionParamsUtil getInstance(){
        if(null==paramsUtil){
            paramsUtil = new RedissionParamsUtil();
        }
        return paramsUtil;
    }
    /**
     * 構(gòu)造方法
     */
    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"));
    }
     
}

以上就是Java 獲取properties的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于Java 獲取properties的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于Spring Boot DevTools實(shí)現(xiàn)開發(fā)過(guò)程優(yōu)化

    基于Spring Boot DevTools實(shí)現(xiàn)開發(fā)過(guò)程優(yōu)化

    這篇文章主要介紹了基于Spring Boot DevTools實(shí)現(xiàn)開發(fā)過(guò)程優(yōu)化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java的文檔注釋之生成幫助文檔的實(shí)例

    Java的文檔注釋之生成幫助文檔的實(shí)例

    下面小編就為大家分享一篇Java的文檔注釋之生成幫助文檔的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Java通過(guò)SSLEngine與NIO實(shí)現(xiàn)HTTPS訪問(wèn)的操作方法

    Java通過(guò)SSLEngine與NIO實(shí)現(xiàn)HTTPS訪問(wèn)的操作方法

    這篇文章主要介紹了Java通過(guò)SSLEngine與NIO實(shí)現(xiàn)HTTPS訪問(wèn),需要在Connect操作、Connected操作、Read和Write操作中加入SSL相關(guān)的處理即可,需要的朋友可以參考下
    2021-08-08
  • Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié)

    Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié)

    這篇文章主要介紹了Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié),需要的朋友可以參考下
    2017-06-06
  • Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧分享

    Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧分享

    最近一直在學(xué)習(xí)Spring Boot,今天將其中的從前臺(tái)過(guò)來(lái)的參數(shù)傳遞總結(jié)一下,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Spring Security中防護(hù)CSRF功能詳解

    Spring Security中防護(hù)CSRF功能詳解

    這篇文章主要介紹了Spring Security中防護(hù)CSRF功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例

    Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例

    這篇文章主要介紹了Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring中@Transactional用法詳細(xì)介紹

    Spring中@Transactional用法詳細(xì)介紹

    這篇文章主要介紹了Spring中@Transactional用法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • JMeter中的后端監(jiān)聽器的實(shí)現(xiàn)

    JMeter中的后端監(jiān)聽器的實(shí)現(xiàn)

    本文主要介紹了JMeter中的后端監(jiān)聽器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring源碼學(xué)習(xí)之動(dòng)態(tài)代理實(shí)現(xiàn)流程

    Spring源碼學(xué)習(xí)之動(dòng)態(tài)代理實(shí)現(xiàn)流程

    這篇文章主要給大家介紹了關(guān)于Spring源碼學(xué)習(xí)之動(dòng)態(tài)代理實(shí)現(xiàn)流程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評(píng)論

海原县| 蚌埠市| 阳高县| 孙吴县| 湖南省| 葵青区| 南京市| 响水县| 宜良县| 嘉鱼县| 华容县| 阜新市| 诸暨市| 丹寨县| 榆中县| 孟村| 六安市| 乐山市| 罗田县| 惠水县| 揭东县| 广河县| 四子王旗| 肇州县| 瑞昌市| SHOW| 宜兰县| 崇阳县| 宝坻区| 蒲城县| 黎川县| 讷河市| 江陵县| 甘孜| 南丰县| 榆中县| 霍林郭勒市| 新竹县| 响水县| 望江县| 荣昌县|