spring注入枚舉類型作為參數
1
2
3
4
5
6
7
8
9
|
//定義枚舉類型 public enum ReportType { MONTH,WEEK,DAY } //使用枚舉類型 public class ReportJob { private ReportType reportType; } |
1
2
3
4
5
6
7
|
//spring配置文件注入 < bean id = "DAY" class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" > < property name = "staticField" value = "com.test.ReportType.DAY" /> </ bean > < bean id = "dayReportJob" class = "com.test.ReportJob" > < property name = "reportType" ref = "DAY" /> </ bean > |
注意:
枚舉類型要想注入到類中,一定要先使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean類將枚舉類型進行轉換,即
1
2
3
|
< bean id = "DAY" class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" > < property name = "staticField" value = "com.test.ReportType.DAY" /> </ bean > |
將ReportType.Day轉換為DAY這個bean,然后在要注入的bean中使用<property name="reportType" ref="DAY" />引用即可。
Spring參數注入
1、通過構造方法實現參數注入
通過構造方法實現參數注入,依賴于構造方法,即實體類中一定存在對于的構造方法。
新建實體類如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.spring.entity; public class Student { private String name; private String password; private Integer age; //構造方法一 public Student(String name, String password) { this .name = name; this .password = password; } //構造方法二 public Student(String name, Integer age) { this .name = name; this .age = age; } public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\ '' + ", password='" + password + '\ '' + ", age=" + age + '}' ; } } |
方法一
配置文件:在配置文件中使用constructor-arg標簽進行參數注入的配置,其中標簽中的name屬性指向的是構造方法中傳入的參數名,spring會根據參數名的對應匹配來進行對應的參數注入,下面代碼段,spring將執行第二個構造方法為參數賦值。
1
2
3
4
|
< bean id = "Stu" class = "com.spring.entity.Student" > < constructor-arg name = "name" value = "小段" ></ constructor-arg > < constructor-arg name = "age" value = "123" ></ constructor-arg > </ bean > |
方法二
通過index屬性來指定參數,并為其賦值。type類型唯一指定參數的類型。有了類型的唯一確定,就可以避免因為兩個構造方法的參數同名而造成的混淆。若參數是基本類型,type屬性直接填寫為int。如:type=“int”。若參數類型為包裝類,則type需要寫全包裝類所在的包名,類名。下面的參數注入會執行第一個構造方法為參數賦值。
1
2
|
< constructor-arg index = "0" value = "小段" type = "java.lang.String" ></ constructor-arg > < constructor-arg index = "1" value = "123456" type = "java.lang.String" ></ constructor-arg > |
2、通過set方法注入
通過set方法實現參數注入,依賴于實體類中屬性的set方法。
實體類構建如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.spring.entity; public class Student { private String name; private String password; private Integer age; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\ '' + ", password='" + password + '\ '' + ", age=" + age + '}' ; } } |
配置文件:通過set方法實現參數注入,使用property標簽來實現。如下所示:name對應實體類中的屬性名稱,value填寫你想賦的值。也可以寫標簽引用bean。
1
2
3
4
5
|
< bean id = "Stu" class = "com.spring.entity.Student" > < property name = "name" value = "小段" ></ property > < property name = "password" value = "123456" ></ property > < property name = "age" value = "12" ></ property > </ bean > |
3、P命名空間注入
在配置文件中(標簽)新增一個命名空間:
1
|
xmlns:http=http://www.springframework.org/schema/p |
然后在標簽中直接進行配置:
1
|
< bean id = "Stu" class = "com.spring.entity.Student" http:password = "1234566" http:age = "123" http:name = "小段" > |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/yingkongshi99/article/details/22991931