Spring 借助FactoryBean来配置Bean
发布时间:2021-11-18 12:23:19 所属栏目:教程 来源:互联网
导读:Spring 利用FactoryBean来配置Bean 在之前的 文章 已经介绍可以利用Java反射机制 和 工厂方法(Factory Method)的方法来在bean config file里 配置beans 本文简单介绍下第三种方法 FactoryBean。 FactoryBean 用法可以与Factory Method有点类似,我们同样需
Spring 利用FactoryBean来配置Bean 在之前的 文章 已经介绍可以利用Java反射机制 和 工厂方法(Factory Method)的方法来在bean config file里 配置beans 本文简单介绍下第三种方法 FactoryBean。 FactoryBean 用法可以与Factory Method有点类似,我们同样需要写1个工厂类, 只不过spring提供了1个叫做FactoryBean的接口。 我们的工厂类必须实现这个接口。 例子 我们首先写1个Car类 package com.home.factoryBean; public class Car { private int id; private String name; private int price; @Override public String toString() { return "Car [id=" + id + ", name=" + name + ", price=" + price + "]"; } public Car(){ } public Car(int id, String name, int price) { super(); this.id = id; this.name = name; this.price = price; } } 接下来必须写1个工厂类CarBeanFactory package com.home.factoryBean; import org.springframework.beans.factory.FactoryBean; public class CarBeanFactory implements FactoryBean<Car>{ private int id; private String brand; public void setId(int id) { this.id = id; } public void setBrand(String brand) { this.brand = brand; } @Override public Car getObject() throws Exception { return new Car(id, brand, 0); } @Override public Class<?> getObjectType() { return Car.class; } @Override public boolean isSingleton() { return true; } } 首先上面这个工厂必须实现Spring提供的FactoryBean这个接口。 然后重写里面的3个方法 getObject() -> 这个就是你想利用工厂类生产的bean对象, 通常在里面new 1个 对象就ok getObjectType() -> 你必须指明上面bean的对象的class isSingleton() -> 这个方法决定了这个bean是否单例的。 bean 配置文件 <!-- FactroyBean: * We should specify the full class name of Factory to the property "class=" * Actually this bean will return the object from the method "getObject" of the Factory class. --> <bean id="car1" class="com.home.factoryBean.CarBeanFactory"> <property name="id" value="1"></property> <property name="brand" value="Ford"></property> </bean> 里面bean的配置写法跟反射机制的十分类似。 但是一般利用反射机制的bean配置, class= 的值就是bean对象的全类名, 但是利用FactoryBean的方式中, class= 必须是工厂类的全类名。 一但这个工厂类实现了FactoryBean接口, 那么这个bean item 返回的就是它里面的getObject()方法return的对象。 client代码 package com.home.factoryBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class FactoryBeanClient { public static void f(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-FactoryBean.xml"); Car car1 = (Car)ctx.getBean("car1"); System.out.println(car1); } 输出结果 Jun 04, 2016 1:26:00 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@627e9505: startup date [Sat Jun 04 13:26:00 CST 2016]; root of context hierarchy Jun 04, 2016 1:26:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [bean-FactoryBean.xml] Car [id=1, name=Ford, price=0] ![]() (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |