`
MauerSu
  • 浏览: 495562 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

spring factory-method

 
阅读更多
源:http://blog.sina.com.cn/s/blog_5f1fe33f0100hyx7.html
评:

配置工厂Bean
通常由应用程序直接使用new创建新的对象,为了将对象的创建和使用相分离,采用工厂模式,即应用程序将对象的创建及初始化职责交给工厂对象.
一般情况下,应用程序有自己的工厂对象来创建bean.如果将应用程序自己的工厂对象交给Spring管理,那么Spring管理的就不是普通的bean,而是工厂Bean.
调用getBean()方法,Spring返回的不是直接创建的Bean的实例,而是由工厂Bean创建的Bean实例.
一般在Spring中配置工厂Bean,有3中不同类型的工厂Bean的配置.
1.静态工厂
创建具体Bean实例的是静态方法

import java.util.Random;
public class StaticFactoryBean {
public static Integer createRandom() {
return new Integer(new Random().nextInt());
}
}
将其纳入Spring容器来管理,需要通过factory-method指定静态方法名称
<bean id="random"
class="example.chapter3.StaticFactoryBean"
factory-method="createRandom" //createRandom方法必须是static的,才能找到
scope="prototype"
/>
测试:
public static void main(String[] args) {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("random").toString());
//StaticFactoryBean sfb = (StaticFactoryBean)factory.getBean("random");
//System.out.println(sfb.createRandom().toString());
//调用getBean()时,返回随机数.如果没有指定factory-method,会返回StaticFactoryBean的实例,即返回工厂Bean的实例
}

2.实例工厂
创建具体Bean实例的是实例,不是静态方法

import java.text.SimpleDateFormat;
import java.util.Date;
public class InstanceFactoryBean {
private String format = "yy-MM-dd HH:mm:ss";
public void setFormat(String format) {
this.format = format;
}
public String createTime() {
return new SimpleDateFormat(format).format(new Date());
}
}
配置文件需要配置两个bean:第一个Bean和普通的Bean没有区别,第二个bean定义如何通过工厂Bean获取Bean,需要指定工厂Bean的名称和方法名称
<bean id="instanceFactoryBean" class="example.chapter3.InstanceFactoryBean">
<property name="format" value="yyyy-MM-dd HH:mm:ss" />
</bean>
<bean id="currentTime"
factory-bean="instanceFactoryBean"
factory-method="createTime"
/>
测试:
public static void main(String[] args) {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("currentTime"));

}
3.实现FactoryBean接口
public class PiFactoryBean implements FactoryBean {
public Object getObject() throws Exception {
return new Double(3.14159265358979);
}
public Class getObjectType() {
return Double.class;
}
public boolean isSingleton() {
return true;
}
}
实现了FactoryBean接口的Bean,不再被视为普通的Bean.Spring会自动检测.
<bean id="pi" class="example.chapter3.PiFactoryBean" />
测试
public static void main(String[] args) throws Exception {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("pi"));//返回PiFactoryBean 的工厂方法getObject返回的Double对象实例
//PiFactoryBean p = (PiFactoryBean)factory.getBean("&pi"); //加"&"返回工厂Bean的实例.
//System.out.println(p.getObject());
}
分享到:
评论

相关推荐

    C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式) (Level 300)

    C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式) (Level 300)

    Spring的BeanFactory的接口注解

    对BeanFactory的相关所有接口的功能的关键点进行了总结描述,中文版本

    spring 实例的工厂方法 依赖注入属性,都使用property 元素确

    ·都需使用factory-method属性指定产生bean 实例的工厂方法。 ·工厂方法如果需要参数,都使用constructor-arg属性确定参数值。 ·其他依赖注入属性,都使用property 元素确定参数值。 Person.java ...

    spring 的parent属性示例

    spring框架中bean标签parent属性的各类使用范例 总计12个范例,涵盖了class、init-method、 factory-method、lazy-init、 depends-on、autowire 多种属性的组合使用

    spring-amqp-java-8:在 Java 8 上运行的示例项目

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.core.Queue]: Factory method 'clientQueue' threw exception; nested exception is org....

    Spring.html

    实例工厂 &lt;bean bean-factory="" factory-method=""&gt; 管理对象 对象关系DI 构造器注入&lt;construct-arg&gt; set注入 生命周期 scope:prototype/singleton init-method destroy-method API ...

    spring-framework-reference-4.1.2

    Instantiation with a static factory method .................................................... 30 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iii Instantiation using an ...

    SpringIOCInternals:IOCSpring内部

    SpringIOC内部IOCSpring内部使用Spring 4.2.1-概念涵盖:: scopeconstructor-argpropertylookup-methodfactory-methodfactory-beaninit-methoddestroy-methodlazy-initabstractparentprimary

    spring-framework-reference4.1.4

    Instantiation with a static factory method .................................................... 30 Spring Framework 4.0.0.RELEASE Spring Framework Reference Documentation iii Instantiation using an ...

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Error ...

    spring security 参考手册中文版

    6.4.1 &lt;global-method-security&gt;元素 65 使用protect-pointcut添加安全性切入点 66 6.5默认AccessDecisionManager 67 6.5.1自定义AccessDecisionManager 67 6.6验证管理器和命名空间 67 7.示例应用程序 69 7.1教程...

    spring applicationContext 配置文件

    &lt;bean id="mesDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"&gt; &lt;!-- userName--&gt; &lt;constructor-arg type="java.lang.String"&gt; &lt;value&gt;...

    org.springframework.transaction-3.1.2.RELEASE.zip

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...

    Spring MVC 入门实例

    6 &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; 7 8 9 &lt;value&gt;/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...

    springmvc-ibatis

    &lt;bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置數據源 --&gt; destroy-method="close"&gt; ${jdbc.driver}...

    spring-data-gosling-ldap

    org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.mapping.ResourceMappings]: Factory method 'resourceMappings' threw exception; nested ...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面...&lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:proxool.properties ...

    spring3.2+strut2+hibernate4

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath*:jdbc.properties &lt;!-- 数据源配置,主要用于开发测试...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    14.2. Using expressions for method-level security 14.2.1. Expressing method access rules 14.2.2. Filtering method inputs and outputs 14.3. Summary 4. Integrating Spring Chapter 15. Working with remote...

Global site tag (gtag.js) - Google Analytics