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);
}
}
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 phones = new HashMap();
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 phones = null;
public InstanceFactory() {
phones = new HashMap();
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
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 {
//配置属性 这里只是为了演示可以这样配置
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;
}
}
beans_factorybean.xml
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]