Spring的实例工厂方法和静态工厂方法实例代码

2025-05-27 0 51

Spring的实例工厂方法和静态工厂方法都可以用来实例化bean,本文我们就来看看相关实例。

静态工厂方法:直接调用静态方法可以返回Bean的实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
package com.zhu.string.factory;

import java.util.HashMap;

import java.util.Map;

public class StaticCarFactory {

/**

* 静态工厂方法:直接调用静态方法可以返回Bean的实例

*

*/

private static Map<String ,Car > cars=new HashMap<String , Car>();

static{

cars.put("audi", new Car(3000, "aodi"));

cars.put("fodo", new Car(3000, "aodi"));

}

//静态工厂方法

public static Car getCar(String name){

return cars.get(name);

}

}

实例工厂方法。即调用工厂本身,再调用工厂的实例方法来返回bean实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
package com.zhu.string.factory;

import java.util.HashMap;

import java.util.Map;

public class InstanceCarFactory {

/**

* 实例工厂方法。即调用工厂本身,再调用工厂的实例方法来返回bean实例

*/

private Map<String ,Car> cars=null;

public InstanceCarFactory() {

// TODO Auto-generated constructor stub

cars=new HashMap<String, Car>();

cars.put("audi", new Car(1000,"audi"));

cars.put("dffdas", new Car(2000,"audi"));

}

public Car getCar(String brand){

return cars.get(brand);

}

}

beans-factory.xml

?

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
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 通过静态方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例

-->

<!--

class属性:指向静态方法的全类名 factory-method:指向静态方法的名字

constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配

置参数

-->

<bean id="car1" factory-method="getCar"

class="com.zhu.string.factory.StaticCarFactory">

<constructor-arg value="audi"></constructor-arg>

</bean>

<!-- 配置工厂的实例 -->

<bean id="carFactory" class="com.zhu.string.factory.InstanceCarFactory">

</bean>

<bean id="car2" factory-bean="carFactory" factory-method="getCar">

<constructor-arg value="audi"></constructor-arg>

</bean>

</beans></span>

Car.java实体类

?

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
package com.zhu.string.factory;

public class Car {

private double price;

private String brand;

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

@Override

public String toString() {

return "Car [brand=" + brand + ", price=" + price + "]";

}

public Car(){

System.out.println("cars....constructor");

}

public Car(double price, String brand) {

super();

this.price = price;

this.brand = brand;

}

}

Main.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
package com.zhu.string.factory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext cx=new ClassPathXmlApplicationContext("beans-

factory.xml");

Car car1=(Car) cx.getBean("car1");

System.out.println(car1);

Car car2=(Car) cx.getBean("car2");

System.out.println(car2);

}

}

运行结果:

Car [brand=aodi, price=3000.0]
Car [brand=audi, price=1000.0]

总结

以上就是本文关于Spring的实例工厂方法和静态工厂方法实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/zhupengqq/article/details/51799320

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring的实例工厂方法和静态工厂方法实例代码 https://www.kuaiidc.com/76606.html

相关文章

发表评论
暂无评论