码迷,mamicode.com
首页 > 编程语言 > 详细

Spring_通过工厂方法配置 Bean

时间:2016-08-23 23:32:36      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

beans-factory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"
class="com.hy.spring.beans.factory.StaticCarFactory"
factory-method="getCar" >
<constructor-arg value="audi"></constructor-arg>
</bean>

<!-- 配置工厂实例 -->
<bean id="carFactory" class="com.hy.spring.beans.factory.InstanceCarFactory">
</bean>

<!-- 通过实例工厂方法来配置bean -->
<!--
class 属性:指向实例工厂方法的bean
factory-method : 指向静态工厂方法的名字
constructor-arg :如果工厂方法需要传入参数,则使用constructor-arg来配置参数。
-->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>

Car.java

package com.hy.spring.beans.factory;

public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}

public Car(){
System.out.println("Car‘s is Counstructor ....");
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
}

StaticCarFactory.java

 

package com.hy.spring.beans.factory;

 

import java.util.HashMap;
import java.util.Map;

 

/**
* 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
*/
public class StaticCarFactory {

 

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

static{
cars.put("audi", new Car("audi",30000));
cars.put("ford", new Car("ford",40000));
}

//静态工厂方法。
public static Car getCar(String name){
return cars.get(name);
}
}


InstanceCarFactory.java

package com.hy.spring.beans.factory;

import java.util.HashMap;
import java.util.Map;

/**
* 实例工厂方法:实例工厂的方法,即现需要创建工厂本身,
* 在调用工厂的实例方法来返回bean的实例
*/
public class InstanceCarFactory {

private Map<String,Car> cars = null;

public InstanceCarFactory(){
cars= new HashMap<String, Car>();
cars.put("audi", new Car("audi",30000));
cars.put("ford", new Car("ford",40000));
}

public Car getCar(String brand){
return cars.get(brand);
}

}


Main.java

package com.hy.spring.beans.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car = (Car) ctx.getBean("car1");
System.out.println(car);
car = (Car) ctx.getBean("car2");
System.out.println(car);
}

}

结果:

技术分享

 

Spring_通过工厂方法配置 Bean

标签:

原文地址:http://www.cnblogs.com/yang-hao/p/5800966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!