欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Spring@Profile注解詳解

@Profile注解詳解

成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),臨西網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:臨西等地區(qū)。臨西做網(wǎng)站價(jià)格咨詢:18982081108

@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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:Spring@Profile注解詳解
鏈接URL:http://chinadenli.net/article10/pijedo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、虛擬主機(jī)網(wǎng)站收錄、動(dòng)態(tài)網(wǎng)站網(wǎng)站制作、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)