詳解Spring註解驅動開發之屬性賦值

2022-09-27 05:00:18 字數 1167 閱讀 1129

在per程式設計客棧son的屬性上使用@value註解指定注入值

public class person ") //spel表示式 #{}

private integer id;

@value("張三") //基本資料型別

private string namewww.cppcns.com;

}配置類

@configuration

public class mainconfigofpropertyvalues }測試

@test

public void testvalues()

person person = (person) context.getbean("person");

system.out.println(person);

}輸出結果:

配置類加上@propertysource註解,引入外部配置檔案

@propertysource()

@configuration

public class mainconfigofpropertyvalues

}使用$注入屬性值

public class person ") //spel表示式 #{}

private integer id;

@value("張三") //基本資料型別

private string name;

@value("$") //使用外部配置檔案注入屬性值

private integer age;

}輸出結果:

因為配置檔案中的值預設都載入到環境變數中,所有還可以通過環境變數來獲取配置檔案中的值

@test

public void testvalues()

person person = (person) context.getbean("person");

system.out.println(person);

environment environment = context.getenvironment();

string age = environment.getproperty("person.age");

system.out.println("a程式設計客棧ge = " + age);

}輸出結果:

Spring註解驅動開發 01

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

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

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

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

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