三 Spring之屬性賦值和自動裝配

2021-10-04 21:13:39 字數 3809 閱讀 2106

1、基本數值

2、可以寫spel; #{}

3、可以寫${};取出配置檔案【properties】中的值(在執行環境變數裡面的值)

這樣可以給person類的屬性賦預設值

public class person ")

private integer age;

@value("$")

private string nickname;

}

給方法引數賦值

@bean("testdatasource")

public datasource datasourcetest(@value("$")string pwd) throws exception

使用@propertysource讀取外部配置檔案中的k/v儲存到執行的環境變數中;載入完外部的配置檔案以後使用${}取出配置檔案的值。
@propertysource(value=)

屬性value是陣列,可以寫多個配置檔案。指定 類路徑classpath下檔案或檔案file下路徑。

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@repeatable(propertysources.class)

public @inte***ce propertysource .

* resource location wildcards (e.g. **/*.properties) are not permitted;

* each location must evaluate to exactly one resource.

* $ placeholders will be resolved against any/all property sources already

* registered with the . see

* for examples.

* each location will be added to the enclosing as its own

* property source, and in the order declared.

*/string value();

測試:

public class ioctest_propertyvalue
可以指定多個 @propertysource 

自動裝配;

spring利用依賴注入(di),完成對ioc容器中中各個元件的依賴關係賦值

下面的例子中,會找到兩個型別為bookdao的例項,但會根據bookservice 的屬性bookdao2  優先注入 id為bookdao2 的例項。

@service

public class bookservice

@bean

public bookdao bookdao()

@resource:

可以和@autowired一樣實現自動裝配功能;預設是按照元件名稱進行裝配的;

可以手動寫明name屬性 指定例項id注入。

沒有能支援@primary功能沒有支援@autowired(reqiured=false);

@service

public class bookservice

有參構造器:引數car就會從容器中取

@component

public class boss

構造器引數上:不管怎麼放,都是從ioc容器中獲取例項

@component

public class boss

如果類只有乙個有參構造器,容器只能使用這個構造器去構建物件,甚至不用使用@autowired 就可以自動注入:

@component

public class boss

public class color 

public void setcar(car car)

color 用@bean注入, 其屬下car 依然是從容器中自動尋找。 

@bean

public color color(car car)

自定義元件實現***aware;

比如beannameaware 可以獲得例項bean的名字;

比如embeddedvalueresolveraware,可以獲得spring的值解析器,可以獲取配置檔案引數。

測試:

@component

@override

// todo auto-generated method stub

} @override

public void setbeanname(string name)

@override

public void setembeddedvalueresolver(strin**alueresolver resolver) 我是 #");

system.out.println("解析的字串:"+resolvestrin**alue);

每個***aware ,都有對應的***awareprocessor,其實現了 beanpostprocessor,成為後置處理器。

後置處理器 postprocessbeforeinitialization 方法中,呼叫了invokeawareinte***ces 方法,判斷了 當前bean是否實現了各aware介面,如果實現了對應介面,就會將對應的spring 底層元件 設定進bean。

private final strin**alueresolver embeddedvalueresolver;

/***/

} @override

public object postprocessbeforeinitialization(final object bean, string beanname) throws bean***ception

if (acc != null)

}, acc);

} else

return bean;

} private void invokeawareinte***ces(object bean)

if (bean instanceof embeddedvalueresolveraware)

if (bean instanceof resourceloaderaware)

}if (bean instanceof messagesourceaware)

}} }

三 Spring註解 屬性賦值

person類 author gaoyuzhe date 2018 3 12.public class person 省略set get 註冊類 propertyconfig configuration public class propertyconfig 測試類 public class pro...

Spring屬性賦值

spring中通過 value給bean賦值 value賦值方式有 基本數值 可以寫spel表示式 可以寫 取出配置檔案中的值 在執行環境變數裡面的值 例如 package com.xiaochao.pojo import lombok.allargsconstructor import lombo...

Spring給屬性賦值

在spring中,我們給屬性賦值一般使用 value註解。按功能分可以將 value分為三類 value 張三 private string name value private int age 這裡賦值18 value private string clname cname.properties ...