spring 配置bean工厂的三种方式

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工厂,此处相当于java代码:
		Car car1 = MystaticBeanFactory.getCar("东风");
		return car1;
		class工厂类
		factory-method工厂静态方法,返回需要的对象
		<constructor-arg value="东风"></constructor-arg>静态方法传参
	 -->
	<bean id="car1" class="com.cwh.spring.bean.factory.MystaticBeanFactory" 
		factory-method="getCar">
		<constructor-arg value="东风"></constructor-arg>
	</bean>
	
	<!-- 实例bean工厂 -->
    <bean id="carFactory" class="com.cwh.spring.bean.factory.MyCarInstanceFactory"></bean>
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
    	<constructor-arg value="奔驰"></constructor-arg>
    </bean>
</beans>

beans-factoryBean.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 id="car1" class="com.cwh.spring.bean.beanfactory.CarFactoryBean">
		<property name="brand" value="奔驰"></property>
	</bean>
</beans>

MystaticBeanFactory.java

package com.cwh.spring.bean.factory;

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

import com.cwh.spring.bean.beanfactory.Car;

/**
 * 静态bean工厂
**/
public class MystaticBeanFactory {

	private static Map<String, Object> map = new HashMap<>();

	//静态代码块,程序开始就执行,不用实例化这个类
	static{
		System.out.println("静态代码块");
		Car car1 = new Car("东风",20.0); 
		Car car2 = new Car("红旗",30.0);
		map.put("东风",car1);
		map.put("红旗", car2);
	}
	
	//静态工厂方法
	public static Car getCar(String key){
		Car car = (Car) map.get(key);
		return car;
	}
}

MyCarInstanceFactory.java

package com.cwh.spring.bean.factory;

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

import com.cwh.spring.bean.beanfactory.Car;

/**
 * 实例bean工厂
 */

public class MyCarInstanceFactory {

	private Map<String, Object> map = null;

	public MyCarInstanceFactory() {
		System.out.println("实例工厂构造方法");
		map = new HashMap<>();
		map.put("东风", new Car("东风", 20));
		map.put("奔驰", new Car("奔驰", 30));
	}

	public Car getCar(String key) {
		return (Car) map.get(key);
	}
}

CarFactoryBean.java

package com.cwh.spring.bean.beanfactory;

import org.springframework.beans.factory.FactoryBean;

/**
 * spring本身的FactoryBean
 *
 */
public class CarFactoryBean implements FactoryBean<Car> {

	private String brand;
	
	public void setBrand(String brand) {
		this.brand = brand;
	}
	
	[@Override](https://my.oschina.net/u/1162528)
	//返回bean实例对象
	public Car getObject() throws Exception {
		// TODO Auto-generated method stub
		return new Car(brand, 50);
	}

	[@Override](https://my.oschina.net/u/1162528)
	//返回bean类型
	public Class<?> getObjectType() {
		// TODO Auto-generated method stub
		return null;
	}

	[@Override](https://my.oschina.net/u/1162528)
	//是否单例
	public boolean isSingleton() {
		// TODO Auto-generated method stub
		return true;
	}
}

测试类:

package com.cwh.spring.bean.beanfactory;

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

public class Main {

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

转载于:https://my.oschina.net/u/3780366/blog/1828091

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值