spring FactoryBean 是創建 復雜的bean,一般的bean 直接用xml配置即可,如果一個bean的創建過程中涉及到很多其他的bean 和復雜的邏輯,用xml配置比較困難,這時可以考慮用FactoryBean
例子如下:
1:創建一個Car類(是為了簡便)一般不能直接給出Car類,如果是這樣直接注入就可以或者Car對象了,這里只是為了簡便。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.myapp.core.factorybean; public class Car { private String make; private int year; public String getMake() { return make; } public void setMake(String make) { this .make = make; } public int getYear() { return year; } public void setYear( int year) { this .year = year; } } |
2:一個FactoryBean的實現擁有創建car
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.myapp.core.factorybean; import org.springframework.beans.factory.FactoryBean; public class MyCarFactoryBean implements FactoryBean<Car>{ private String make; private int year; public void setMake(String make) { this .make = make; } public void setYear( int year) { this .year = year; } @Override public Car getObject() throws Exception { // TODO Auto-generated method stub //Here is a complex car object created // wouldn't be a very useful FactoryBean // if we could simply instantiate the object! Car car = new Car(); if (year != 0 ){ car.setYear( this .year); } if ( "make" .equals(make)){ car.setMake( "we are making bla bla bla" ); } else { car.setMake( this .make); } return car; } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Car. class ; } @Override public boolean isSingleton() { // TODO Auto-generated method stub return false ; } } |
以上中創建car太簡單了,如果太簡單就沒有必要用FactoryBean創建了,可以寫的復雜些。
3:Person 引用一個car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.myapp.core.factorybean; public class Person { private Car car; public Car getCar() { return car; } public void setCar(Car car) { this .car = car; } public String toString(){ return car.getMake()+ "::::" +car.getYear(); } } |
4:配置引用xml格式:
1
2
3
4
5
6
7
8
9
10
|
< bean id = "car" class = "com.myapp.core.factorybean.MyCarFactoryBean" > < property name = "make" value = "makeing car" /> < property name = "year" value = "123" /> </ bean > < bean id = "person" class = "com.myapp.core.factorybean.Person" > < property name = "car" ref = "car" /> </ bean > |
5:編寫測試類測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.myapp.core.factorybean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "resource/others.xml" ); Person person = (Person)context.getBean( "person" ); // Car car = (Car)context.getBean("car"); // System.out.println(car); System.out.println(person); } } |
測試結果 makeing car::::123
利用FactoryBean創建car成功
只是為了說明思想。因為這個接口太重要了。在Spring中有很多類實現了該接口。
以上所述是小編給大家介紹的詳解Spring中的FactoryBean,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/topwqp/article/details/8678061