Spring註解驅動開發 Profile環境切換

2021-08-20 10:32:18 字數 3097 閱讀 4107

profile:spring提供的可以根據當前環境(開發、測試、生產),動態的啟用和切換一系列的元件的功能,可以使用@profile註解實現,比如資料來源根據環境的切換。@profile註解用於指定元件在哪個環境下會註冊到ioc容器中,若不加該註解則在所有環境下都會註冊到容器中:

@propertysource("classpath:/config.properties")

@configuration

public class myprofileconfig implements embeddedvalueresolveraware ")

private string user;

private string driverclass;

@bean

public datasource devdatasource(@value("$") string password) throws propertyvetoexception

@bean

public datasource testdatasource(@value("$") string password) throws propertyvetoexception

@bean

public datasource prodatasource(@value("$") string password) throws propertyvetoexception

//可以在此處給driverclass賦值說明此方法的呼叫時機在bean建立之前

public void setembeddedvalueresolver(stringvalueresolver resolver) ");

}}

沒有加@profile註解,則容器中會有三個資料來源物件,這裡使用了三種不同的方式為屬性賦值(@value或者springvalueresolver都可以)

@propertysource("classpath:/config.properties")

@configuration

public class myprofileconfig implements embeddedvalueresolveraware ")

private string user;

private string driverclass;

@bean

@profile("dev")

public datasource devdatasource(@value("$") string password) throws propertyvetoexception

@bean

@profile("test")

public datasource testdatasource(@value("$") string password) throws propertyvetoexception

@bean

@profile("pro")

public datasource prodatasource(@value("$") string password) throws propertyvetoexception

public void setembeddedvalueresolver(stringvalueresolver resolver) ");

}}

加了環境標識(@profile)的bean,只有在這個環境被啟用時該bean才會被註冊到ioc容器中,容器的預設環境是default,即在不設定容器的環境的情況下所有的@profile("default")和不加@profile註解的bean都會註冊到容器中,那麼怎麼改變容器的環境呢?

1、使用命令列引數在run configuration的vm arguments中設定:-dspring.profiles.active的值

-dspring.profiles.active=test
此時則容器中只有testdatasource

2、使用**的方式為容器設定啟用環境:

public void test1() throws exception
this();

register(annotatedclasses);

refresh();}

屬性檔案:

db.user=root

db.password=123456

db.driverclass=com.mysql.jdbc.driver

也可以將@profile註解標註在配置類上,此時該配置類只有在與ioc容器環境一致時才會被註冊到ioc容器中

@profile()

@configuration

@propertysource("classpath:/config.properties")

public class myprofileconfig implements embeddedvalueresolveraware ")

private string user;

private string driverclass;

@bean

@profile("dev")

public datasource devdatasource(@value("$") string password) throws propertyvetoexception

@bean

@profile("test")

public datasource testdatasource(@value("$") string password) throws propertyvetoexception

@bean

@profile("pro")

public datasource prodatasource(@value("$") string password) throws propertyvetoexception

public void setembeddedvalueresolver(stringvalueresolver resolver) ");

}}

Spring註解驅動開發 01

匯入依賴 spring context spring aop spring bean spring core commons logging spring expression 註解式開發 配置類 config 等同於配置檔案 configuration 告訴spring這是乙個配置類 bean 給...

Spring註解驅動開發 AOP面向切面

aop 在程式執行期間,動態的將某段 切入到指定方法執行時的指定時機執行,其實就是動態 spring提供了對aop很好的支援,使用時需要匯入spring aspects包。業務邏輯類 要求在業務方法執行時列印日誌 public class mathcalculator 切面類 類上需要註解 aspe...

Spring註解驅動開發 AOP原理簡述

要有aop功能必須要加 enableaspectjautoproxy註解 enableaspectjautoproxy註解會給容器中新增乙個後置處理器,這個後置處理器會在bean的建立前後被呼叫,bean建立完成後,會檢查這個bean是否需要增強,如果需要增強,就會通過動態 技術生成乙個 物件,物件...