欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring @Profile注解詳解

 更新時(shí)間:2019年08月16日 09:27:13   作者:碼莎拉蒂  
這篇文章主要介紹了Spring @Profile注解詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(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、測(cè)試環(huán)境test、生產(chǎn)環(huán)境master

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

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

1) 加了環(huán)境標(biāo)識(shí)的bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊(cè)到容器中。默認(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、測(cè)試環(huán)境test、生產(chǎn)環(huán)境master
 * 數(shù)據(jù)源:(/dev) (/test) (/master)
 *
 * @Profile:指定組件在哪個(gè)環(huán)境的情況下才能被注冊(cè)到容器中,不指定,任何環(huán)境下都能注冊(cè)這個(gè)組件
 * 
 * 1) 加了環(huán)境標(biāo)識(shí)的bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊(cè)到容器中。默認(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. 注冊(cè)主配置類
 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();
 }
}

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

相關(guān)文章

  • Spring?createBeanInstance實(shí)例化Bean

    Spring?createBeanInstance實(shí)例化Bean

    這篇文章主要為大家介紹了Spring?createBeanInstance實(shí)例化Bean源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 一篇文章讓你徹底了解Java可重入鎖和不可重入鎖

    一篇文章讓你徹底了解Java可重入鎖和不可重入鎖

    最近正在閱讀Java ReentrantLock源碼,始終對(duì)可重入和不可重入概念理解不透徹,今天特地整理了本篇文章,讓你徹底了解Java可重入鎖和不可重入鎖,需要的朋友可以參考下
    2021-06-06
  • Java代理模式的深入了解

    Java代理模式的深入了解

    這篇文章主要為大家介紹了Java代理模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • SpringBoot常見錯(cuò)誤圖文總結(jié)

    SpringBoot常見錯(cuò)誤圖文總結(jié)

    最近在使用idea+Springboot開發(fā)項(xiàng)目中遇到一些問題,這篇文章主要給大家介紹了關(guān)于SpringBoot常見錯(cuò)誤總結(jié)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)詳解(解決響應(yīng)超時(shí)問題)

    Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)詳解(解決響應(yīng)超時(shí)問題)

    當(dāng)后端對(duì)于數(shù)據(jù)量較大的處理或是某些耗時(shí)的操作時(shí),需要先對(duì)請(qǐng)求接口的請(qǐng)求進(jìn)行響應(yīng),下面這篇文章主要給大家介紹了關(guān)于Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)(解決響應(yīng)超時(shí)問題)的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • java獲取當(dāng)前日期和時(shí)間的二種方法分享

    java獲取當(dāng)前日期和時(shí)間的二種方法分享

    這篇文章主要介紹了java獲取當(dāng)前日期和時(shí)間的二種方法,需要的朋友可以參考下
    2014-03-03
  • Java字段初始化的規(guī)律解析

    Java字段初始化的規(guī)律解析

    這篇文章主要介紹了Java字段初始化的規(guī)律解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java的接口解耦方式

    java的接口解耦方式

    這篇文章主要介紹了java的接口解耦方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java中ThreadLocal的使用

    Java中ThreadLocal的使用

    這篇文章主要介紹了Java中ThreadLocal的使用,靜態(tài)內(nèi)部類的加載是在程序中調(diào)用靜態(tài)內(nèi)部類的時(shí)候加載的,和外部類的加載沒有必然關(guān)系, 但是在加載靜態(tài)內(nèi)部類的時(shí)候 發(fā)現(xiàn)外部類還沒有加載,那么就會(huì)先加載外部類 ,加載完外部類之后,再加載靜態(tài)內(nèi)部類,需要的朋友可以參考下
    2023-09-09
  • Java多線程模式之Balking模式詳解

    Java多線程模式之Balking模式詳解

    這篇文章主要介紹了Java多線程模式之Balking模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了Balking模式的原理、用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06

最新評(píng)論