使用spring注入列舉型別作為引數

2022-09-25 21:54:11 字數 1530 閱讀 3812

目錄

//定義列舉型別

public ewww.cppcns.comnum reporttype

//使用列舉型別

public class reportjob

//spring配置檔案注入

注意:列舉型別要想注入到類中,一定要先使用org.springframework.beans.factory.config.fieldretrievingfactorybean類將列舉型別進行轉換,即

將reporttype.day轉換為day這個bean,然後在要注入的bean中使用引用即可。

通過構造方法實現引數注入,依賴於構造方法,即實體類中一定存在對於的構造方法。

新建實體類如下所示:

package com.spring.entity;

public class student

//構造方法二

public student(string name, integer age)

public student()

@override

public string tostring() ';}}

方法一配置檔案:在配置檔案中使用constructor-arg標籤進行引數注入的配置,其中標籤中的name屬性指向的是構造方法中傳入的引數名,spring會根據引數名的對應匹配來進行對應的引數注入,下面**段,spring將執行第二個構造方法為引數賦值。

方法二通過index屬性來指定引數,並為其賦值。type型別唯一指定引數的型別。有了型別的唯一確定,就可以避免因為兩個構造方法的引數同名而造成的混淆。若引數是基本型別,type屬性直接填寫為int。如:type=「int」。若引數型別為包裝類,則type需要寫全包裝類所在的包名,類名。下面的引數注入會執行第乙個構造方法為引數賦值。

j**a.lang.string">

通過set方法實現引數注入,依賴於實體類中屬性的set方法。

實體類構建如下:

package com.spring.entity;

public class student

public void setname(string name)

public string getpassword()

public void setpassword(string password)

public integer getage()

public void setage(integer age)

public student()

@override

public string tostring() ';}}

配置檔案:通過set方法實現引數注入,使用property標籤來實現。如下所示:name對應實體類中的屬性名稱,value填寫你想賦的值。也可以寫標籤引用bean。

在配置檔案中(標籤)新增乙個命名空間:

然後在標籤中直接進行配置:

本文標題: 使用spring注入列舉型別作為引數

本文位址: /ruanjian/j**a/425274.html

spring注入列舉型別作為引數

定義列舉型別 public enum reporttype 使用列舉型別 public class reportjob spring配置檔案注入 注意 列舉型別要想注入到類中,一定要先使用org.springframework.beans.factory.config.fieldretrieving...

使用列舉型別

using system using system.data using system.configuration using system.collections using system.web using system.web.security using system.web.ui usin...

spring註解設定名稱注入還是型別注入

autowire 預設按照型別裝配,預設情況下它要求依賴物件必須存在如果允許為null,可以設定它required屬性為false,如果我們想使用按照名稱裝配,可 以結合 qualifier註解一起使用 resource預設按照名稱裝配,當找不到與名稱匹配的bean才會按照型別裝配,可以通過name...