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

Spring @Profile注解詳解

 更新時(shí)間:2019年08月16日 09:27:13   作者:碼莎拉蒂  
這篇文章主要介紹了Spring @Profile注解詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

@Profile注解詳解

@Profile:Spring為我們提供的可以根據(jù)當(dāng)前環(huán)境,動(dòng)態(tài)的激活和切換一系列組件的功能;

開發(fā)環(huán)境develop、測試環(huán)境test、生產(chǎn)環(huán)境master

數(shù)據(jù)源:(/dev) (/test) (/master)

@Profile:指定組件在哪個(gè)環(huán)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個(gè)組件

1) 加了環(huán)境標(biāo)識的bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊到容器中。默認(rèn)是default環(huán)境
2) 寫在配置類上,只有是指定的環(huán)境的時(shí)候,整個(gè)配置類里面的所有配置才能開始生效  

package com.spring.config;
 
import java.beans.PropertyVetoException;
 
import javax.sql.DataSource;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
/**
 * Profile:
 * Spring為我們提供的可以根據(jù)當(dāng)前環(huán)境,動(dòng)態(tài)的激活和切換一系列組件的功能;
 * 
 * 開發(fā)環(huán)境develop、測試環(huán)境test、生產(chǎn)環(huán)境master
 * 數(shù)據(jù)源:(/dev) (/test) (/master)
 *
 * @Profile:指定組件在哪個(gè)環(huán)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個(gè)組件
 * 
 * 1) 加了環(huán)境標(biāo)識的bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊到容器中。默認(rèn)是default環(huán)境
 * 2) 寫在配置類上,只有是指定的環(huán)境的時(shí)候,整個(gè)配置類里面的所有配置才能開始生效
 * 
 */
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware{
 
 @Value("${db.user}")
 private String user;
 
 private String driverClass;
 
 @Profile("default")
 @Bean("test")
 public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setUser(user);
 dataSource.setPassword(password);
 dataSource.setDriverClass(driverClass);
 return dataSource;
 }
 
 @Profile("dev")
 @Bean("dev")
 public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setUser(user);
 dataSource.setPassword(password);
 dataSource.setDriverClass(driverClass);
 return dataSource;
 }
 
 @Profile("master")
 @Bean("master")
 public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setUser(user);
 dataSource.setPassword(password);
 dataSource.setDriverClass(driverClass);
 return dataSource;
 }
 
 public void setEmbeddedValueResolver(StringValueResolver resolver) {
 String driverClass = resolver.resolveStringValue("${db.driverClass}");
 this.driverClass = driverClass;
 }
 
}
package com.spring.test;
 
import java.util.Arrays;
 
import javax.sql.DataSource;
 
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.spring.config.MainConfigOfProfile;
 
 
public class IOCTestProfile {
 //1. 使用命令行動(dòng)態(tài)參數(shù):在虛擬機(jī)參數(shù)位置加載 -Dspring.profiles.active=test
 //2. 使用代碼的方式激活某種環(huán)境;
 @Test
 public void test01() {
 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
 //1. 創(chuàng)建一個(gè)applicationContext
 //2. 設(shè)置需要激活的環(huán)境
 applicationContext.getEnvironment().setActiveProfiles("dev","master");
 //3. 注冊主配置類
 applicationContext.register(MainConfigOfProfile.class);
 //4. 啟動(dòng)刷新容器
 applicationContext.refresh();
 
 String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
 System.out.println(Arrays.toString(beanNamesForType));
 
 applicationContext.close();
 }
 
 
  @Test
 public void test02() {
 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
 
 String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
 System.out.println(Arrays.toString(beanNamesForType));
 
 applicationContext.close();
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java的Integer緩存池用法

    Java的Integer緩存池用法

    Java的Integer緩存池主要為了提升性能和節(jié)省內(nèi)存,它緩存了-128到127范圍內(nèi)的Integer對象,因此這些對象在比較時(shí)會(huì)直接比較引用,而不是值,其他包裝類如Byte、Short、Character也有類似的緩存池
    2025-02-02
  • Java對稱與非對稱加密算法原理詳細(xì)講解

    Java對稱與非對稱加密算法原理詳細(xì)講解

    對稱加密算法指加密和解密使用相同密鑰的加密算法。對稱加密算法用來對敏感數(shù)據(jù)等信息進(jìn)行加密,非對稱加密算法指加密和解密使用不同密鑰的加密算法,也稱為公私鑰加密
    2022-11-11
  • SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹

    SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹

    本篇文章為大家展示了如何在SpringBoot中統(tǒng)一處理邏輯異常,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲
    2022-09-09
  • JVM進(jìn)程緩存Caffeine的使用

    JVM進(jìn)程緩存Caffeine的使用

    本文主要介紹了JVM進(jìn)程緩存Caffeine的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java函數(shù)式編程(十二):監(jiān)控文件修改

    Java函數(shù)式編程(十二):監(jiān)控文件修改

    這篇文章主要介紹了Java函數(shù)式編程(十二):監(jiān)控文件修改,本文是系列文章的第12篇,其它文章請參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • 使用SpringDataJpa創(chuàng)建中間表

    使用SpringDataJpa創(chuàng)建中間表

    這篇文章主要介紹了使用SpringDataJpa創(chuàng)建中間表,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java中Exception和Error的區(qū)別詳解

    Java中Exception和Error的區(qū)別詳解

    這篇文章主要介紹了Java中Exception和Error的區(qū)別詳解,通過類的關(guān)系分析兩者的區(qū)別與應(yīng)用場景,包含代碼實(shí)例,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • java通過方向鍵控制小球移動(dòng)的小游戲

    java通過方向鍵控制小球移動(dòng)的小游戲

    這篇文章主要為大家詳細(xì)介紹了java通過方向鍵控制小球移動(dòng)的小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)

    springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)

    在我們開發(fā)項(xiàng)目過程中,經(jīng)常需要定時(shí)任務(wù)來幫助我們來做一些內(nèi)容,本文介紹了springboot中設(shè)置定時(shí)任務(wù)的三種方法,主要包括@Scheduled注解,Quartz框架和xxl-job框架的實(shí)現(xiàn),感興趣的可以了解一下
    2023-12-12
  • Android圖片轉(zhuǎn)換器代碼分享

    Android圖片轉(zhuǎn)換器代碼分享

    本文給大家總結(jié)了下在安卓程序中進(jìn)行圖片轉(zhuǎn)換的方法,非常的實(shí)用,小伙伴們可以參考下。
    2015-10-10

最新評論

嘉兴市| 抚州市| 绥滨县| 靖州| 筠连县| 平凉市| 陇西县| 安丘市| 长汀县| 赞皇县| 天镇县| 上蔡县| 北宁市| 德化县| 隆回县| 邢台市| 桦南县| 新干县| 赤壁市| 定日县| 扎鲁特旗| 沙雅县| 韩城市| 阜南县| 湛江市| 舟曲县| 越西县| 永修县| 新巴尔虎右旗| 波密县| 抚顺县| 江孜县| 海门市| 景泰县| 武邑县| 日喀则市| 乌兰县| 桃园市| 湘潭县| 图们市| 昭觉县|