日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring(19)——Profile(二)

發布時間:2025/3/21 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring(19)——Profile(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

19.2 指定啟用的profile

前面已經介紹了profile的指定,我們知道指定了profile后則表示對應的內容只有在特定的profile下才會生效。當前應用究竟使用的是哪個profile,或者是哪些profile,這是需要我們來指定的。說的專業一點就叫激活,即只有處于激活狀態的profile對應的定義才會生效,當然也包括那些沒有指定profile的定義。

在Spring中激活哪個profile是通過參數spring.profiles.active來指定的,我們可以把它定義為一個系統環境變量、JVM參數,或者是在web.xml中的一個ServletContext參數。如下就是通過JVM參數指定激活的profile為dev的示例。

-Dspring.profiles.active=dev

如下是通過在web.xml文件中通過ServletContext的參數指定激活的profile的示例,其激活的profile是dev。

<context-param><param-name>spring.profiles.active</param-name><param-value>dev </param-value> </context-param>

當然,我們也可以同時激活多個profile,同時激活多個profile時,多個profile之間以逗號隔開。如下示例即表示同時激活dev和production兩個profile。(其它如JVM參數指定等是同樣的規則)

<context-param><param-name>spring.profiles.active</param-name><param-value>dev,production</param-value> </context-param>

除了使用spring.profiles.active參數進行指定外,我們還可以通過在程序中動態的指定激活的profile。如下示例中我們就通過獲取當前ApplicationContext的Environment對象,然后通過該對象指定激活的profile為production。使用程序指定激活的profile時需要注意先構建一個空的ApplicationContext對象,然后再通過該對象的Environment對象指定激活的profile,再指定對應的bean定義對應的資源位置,最后通過調用refresh()方法讓ApplicationContext對象解析對應的bean定義。

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();context.getEnvironment().setActiveProfiles("production");context.setConfigLocation("classpath:applicationContext.xml");context.refresh();

如果需要同時指定多個激活的profile,則可以給setActiveProfiles()方法指定多個參數,其對應的參數是一個可變參數。

context.getEnvironment().setActiveProfiles("dev","production");

19.3 默認profile

此外,我們還可以給我們的應用指定默認的profile。我們知道如果一個<beans/>沒有指定profile,且其上級的<beans/>也沒有指定profile,那么對應<beans/>中定義的所有的bean無論激活的何種profile,它們都是可用的。而默認profile的概念是我們定義一個默認的profile,然后如果一個<beans/>指定的profile為默認的profile,則當沒有激活的profile時,對應默認profile的<beans/>中定義的bean都是可用的,但是一旦有激活的profile,那么對應默認profile的<beans/>就是不可用的。如果我們默認的bean定義不指定profile的話,那么對應的bean定義將在所有的情況下都是可用的,一旦我們改變profile,那么可能就會存在兩個相同類型的bean定義。又或者我們將默認的bean定義與特定的bean定義定義為不同的兩個profile,這樣的結果是我們必須指定一個激活的profile。所以說默認profile這種機制也是非常有用的,即我們可以通過默認profile來定義默認的bean定義,然后通過改變profile來改變對應的bean定義。

Spring中默認profile的名稱是“default”,即默認情況下我們將一個<beans/>的profile指定為default,即表示其對應默認的profile。如下示例中我們定義了在沒有處于激活狀態的profile時hello_default是可用的,而在名稱為production的profile處于激活狀態時hello_production是可用的。

<!-- 只有在激活了名稱為production的profile時其中定義的bean才是可用的 --> <beans profile="production"><bean id="hello_production" class="com.app.Hello"/> </beans><!-- 默認profile,即只有在沒有激活任何profile的情況下其中定義的bean才是可用的 --> <beans profile="default"><bean id="hello_default" class="com.app.Hello"/> </beans>

19.3.1 更改默認profile的名稱

默認profile的名稱是“default”,我們也可以通過spring.profiles.default參數進行更改,更改方式類似于通過參數spring.profiles.active指定激活的profile。

1、如下是通過JVM參數指定默認的profile為production。

-Dspring.profiles.default=production

2、如下是通過ServletContext的參數指定默認的profile為production(供ContextLoaderListener使用)。

<context-param><param-name>spring.profiles.default</param-name><param-value>production</param-value> </context-param>

對于這種直接通過參數spring.profiles.default指定默認profile的情況,我們也可以同時指定多個profile,多個profile之間以逗號隔開。

3、也可以通過程序化的方式獲取ApplicationContext對應的Environment對象,然后通過該對象設置對應的默認profile。如下示例表示我們設置默認的profile為“default”和“production”。setDefaultProfiles()方法接收的是一個可變參數,所以我們可以同時指定多個默認的profile。

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();context.getEnvironment().setDefaultProfiles("production", "default");context.setConfigLocation("classpath:applicationContext.xml");context.refresh();

(注:本文是基于Spring4.1.0所寫)

總結

以上是生活随笔為你收集整理的Spring(19)——Profile(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。