Bean 的配置方式

Bean 的配置方式 的三种方式:

                              ①. 通过全类名(反射)

                              ②. 通过工厂方法(静态工厂方法 & 实例工厂方法)

                              ③. FactoryBean

1.  通过全类名(反射)

package com.baidu.spring.beans.test;

public class Person {
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	public void showName() {
		System.out.println("Person's name: " + name);
	}
	
}

通过全类名配置Bean

<?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 id="person" class="com.baidu.spring.beans.test.Person">
		<property name="name" value="HanMeiMei"></property>
		<property name="age" value="35"></property>
	</bean>
	
</beans>

2. 通过工厂方法


Phone.java

package com.baidu._StaticFactory;

public class Phone {
	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 "Phone [brand=" + brand + ", price=" + price + "]";
	}
	
	public Phone() {
		System.out.println("Phone`s Constructor....");
	}
	public Phone(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}
}


静态工厂方法

StaticPhoneFactory.java

package com.baidu._StaticFactory;

import java.util.HashMap;
import java.util.Map;
/**
 * 静态工厂方法:直接调用某一个类的静态方法就可以返回Bean 的实例
 */
public class StaticPhoneFactory {
	
	private static Map<String, Phone> phones = new HashMap<String, Phone>();
	
	static {
		
		phones.put("HuaWei", new Phone( "HuaWei",4899));
		phones.put("Mi", new Phone( "Mi",1999));
	}
	//静态工厂方法
	public static Phone getPhone(String brand) {
		return phones.get(brand);
	}
}

实例工厂方法

InstanceFactory.java

package com.baidu._InstanceFactory;

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

import com.baidu._StaticFactory.Phone;
/**
 * 实例工厂方法:实例工厂的方法,即需要先创建工厂本身,在调用工厂的实例方法来返回bean 的实例
 */
public class InstanceFactory {
	
	private Map<String, Phone> phones = null;
	
	public InstanceFactory() {
		phones = new HashMap<String, Phone>();
		phones.put("HuaWei", new Phone( "HuaWei",4899));
		phones.put("Mi", new Phone( "Mi",1999));
	}
	
	public  Phone getPhone(String brand) {
		return phones.get(brand);
	}
}

配置Bean

 beans_staticefactory_instancefactory.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="phone1" class="com.baidu._StaticFactory.StaticPhoneFactory"
		factory-method="getPhone">
		<constructor-arg value="HuaWei"></constructor-arg>
		</bean>


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

	<!-- 配置工厂的实例 -->
	<bean id="phoneFactory" class="com.baidu._InstanceFactory.InstanceFactory"></bean>
	<!-- 通过实例工厂方法来配置bean
	
		factory-bean 属性: 指向实例工厂方法的bean
		factory-method:指向实例工厂方法的名字
		constructor-arg: 如果工厂方法需要传入参数,则使用constructor-arg 来配置参数
	 -->
	<bean id="phone2" factory-bean="phoneFactory" factory-method="getPhone">
		<constructor-arg value="Mi"></constructor-arg>
	</bean>	

</beans>


测试:

TestStaticFactoryAndInstanceFactory.java

package com.baidu._StaticFactory;

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

public class TestStaticFactoryAndInstanceFactory {
	
	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(" beans_staticefactory_instancefactory.xml");
		
		Phone phone1 = (Phone) applicationContext.getBean("phone1");
		System.out.println(phone1);
		
		Phone phone2= (Phone) applicationContext.getBean("phone2");
		System.out.println(phone2);
	}
}

运行结果:

Phone [brand=HuaWei, price=4899.0]
Phone [brand=Mi, price=1999.0]

3.  FactoryBean



Phone.java

package com.baidu.FactoryBean;

public class Phone {
	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 "Phone [brand=" + brand + ", price=" + price + "]";
	}
	
	public Phone() {
		System.out.println("Phone`s Constructor....");
	}
	public Phone(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}
}
PhoneFactoryBean.java

package com.baidu.FactoryBean;

import org.springframework.beans.factory.FactoryBean;
//自定义的FactoryBean 需要实现FactoryBean 接口
public class PhoneFactoryBean implements FactoryBean<Phone> {
	
	//配置属性 这里只是为了演示可以这样配置
	private String brand;
	
	public void setBrand(String brand) {
		this.brand = brand;
	}

	//返回bean 的对象
	@Override
	public Phone getObject() throws Exception {
		return new Phone(brand, 4889);
	}

	//返回bean 的类型
	@Override
	public Class<?> getObjectType() {
		return Phone.class;
	}

	//返回bean 是否为单实例
	@Override
	public boolean isSingleton() {
		return true;
	}
}


配置bean

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">

	<!-- 
		通过FactoryBean 来配置Bean 的实例
		class:指向FactoryBean 的全类名
		property: 配置FactoryBean 的属性
	
		但是,实际返回的实例是FactoryBean 的getObject() 方法返回的实例!
	 -->
	<bean id="phone" class="com.baidu.FactoryBean.PhoneFactoryBean">
		<property name="brand" value="HuaWei"></property>
	</bean>

</beans>


测试方法:

package com.baidu.FactoryBean;

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

public class TestSpringFactoryBean {
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_factorybean.xml");
		
		Phone phone = (Phone) applicationContext.getBean("phone");
		System.out.println(phone);
	}
}
运行结果:

Phone [brand=HuaWei, price=4889.0]



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值