Spring @Profile注解詳解
@Profile注解詳解
@Profile:Spring為我們提供的可以根據(jù)當前環(huán)境,動態(tài)的激活和切換一系列組件的功能;
開發(fā)環(huán)境develop、測試環(huán)境test、生產(chǎn)環(huán)境master
數(shù)據(jù)源:(/dev) (/test) (/master)
@Profile:指定組件在哪個環(huán)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個組件
1) 加了環(huán)境標識的bean,只有這個環(huán)境被激活的時候才能注冊到容器中。默認是default環(huán)境
2) 寫在配置類上,只有是指定的環(huán)境的時候,整個配置類里面的所有配置才能開始生效
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ù)當前環(huán)境,動態(tài)的激活和切換一系列組件的功能; * * 開發(fā)環(huán)境develop、測試環(huán)境test、生產(chǎn)環(huán)境master * 數(shù)據(jù)源:(/dev) (/test) (/master) * * @Profile:指定組件在哪個環(huán)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個組件 * * 1) 加了環(huán)境標識的bean,只有這個環(huán)境被激活的時候才能注冊到容器中。默認是default環(huán)境 * 2) 寫在配置類上,只有是指定的環(huán)境的時候,整個配置類里面的所有配置才能開始生效 * */ @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. 使用命令行動態(tài)參數(shù):在虛擬機參數(shù)位置加載 -Dspring.profiles.active=test //2. 使用代碼的方式激活某種環(huán)境; @Test public void test01() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class); //1. 創(chuàng)建一個applicationContext //2. 設(shè)置需要激活的環(huán)境 applicationContext.getEnvironment().setActiveProfiles("dev","master"); //3. 注冊主配置類 applicationContext.register(MainConfigOfProfile.class); //4. 啟動刷新容器 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)文章
Spring?createBeanInstance實例化Bean
這篇文章主要為大家介紹了Spring?createBeanInstance實例化Bean源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Springboot調(diào)整接口響應(yīng)返回時長詳解(解決響應(yīng)超時問題)
當后端對于數(shù)據(jù)量較大的處理或是某些耗時的操作時,需要先對請求接口的請求進行響應(yīng),下面這篇文章主要給大家介紹了關(guān)于Springboot調(diào)整接口響應(yīng)返回時長(解決響應(yīng)超時問題)的相關(guān)資料,需要的朋友可以參考下2023-01-01