Spring配置bean的方法(工厂方法和Factorybean)
通过工厂方法配置bean
通过调用静态工厂方法创建bean
通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中。当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节。
要声明通过静态方法创建的bean,需要在bean的class属性里指定拥有该工厂的方法的类,通知在factory-method属性里指定工厂方法的名称,
最后,使用<constructor-arg>元素为该方法传递方法参数
通过调用实例工厂方法创建bean
实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时,只需要简单的调用该实例方法而不需关心对象的创建细节。
要声明通过实例工厂方法创建的bean:
-在bean的factory-bean属性里指定拥有该工厂方法的bean
-在factory-method属性里指定该工厂方法的名称
-使用constructor-arg元素为工厂方法传递方法参数
静态工厂类:
package com.yl.factory; import java.util.HashMap;
import java.util.Map; /**
* 静态工厂方法:直接调用某一个类的静态方法就可与返回bean的实例
* @author yul
*
*/
public class StaticCarFactory { private static Map<String, Car> cars = new HashMap<String, Car>(); static {
cars.put("audi", new Car("audi", 300000));
cars.put("ford", new Car("ford", 300000));
}
/**
* 静态工厂方法
* @param name
* @return
*/
public static Car getCar(String name) { return cars.get(name);
}
}
实例工厂类:
package com.yl.factory; import java.util.HashMap;
import java.util.Map;
/***
* 实例工厂方法:实例工厂的方法,即现需要创建工厂本身,在调用工厂的实例方法来返回bean的实例
* @author yul
*
*/
public class InstanceCarFactory { private Map<String, Car> cars = new HashMap<String, Car>(); public InstanceCarFactory() {
cars = new HashMap<String, Car>();
cars.put("audi", new Car("audi", 300000));
cars.put("ford", new Car("ford", 400000));
} public Car getCar(String brand) {
return cars.get(brand);
}
}
工厂方法的配置文件:
<?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="car1"
class="com.yl.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean> <!-- 配置工厂的实例 -->
<bean id="carFactory" class="com.yl.factory.InstanceCarFactory"></bean> <!--
factory-bean:指向实例工厂方法的bean
factory-method:指向实例工厂方法的名字
constructor-arg:如果实例工厂方法需要传入参数,则使用constructor-arg来配置参数
-->
<!-- 通过实例工厂方法来配置bean -->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
通过Factorybean配置bean
继承Factorybean的实现类:
package com.yl.factorybean; import org.springframework.beans.factory.FactoryBean;
//自定义的Factorybean需要实现FactoryBean接口
public class CarFactoryBean implements FactoryBean<Car> { private String brand; public void setBrand(String brand) {
this.brand = brand;
} /**
* 返回bean的对象
*/
@Override
public Car getObject() throws Exception {
// TODO Auto-generated method stub
return new Car("BMW", 600000);
}
/**
* 返回bean的类型
*/
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Car.class;
} @Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return true;
} }
配置文件:
<?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="car" class="com.yl.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>
Spring配置bean的方法(工厂方法和Factorybean)的更多相关文章
- Spring-配置bean的方法(工厂方法和Factorybean)【转】
通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声 ...
- spring的bean注入扫瞄方法和mybatis的dao bean注入扫描方法
spring的bean注入扫面方法:@ComponentScan(basePackages = "com.pingan.property.icore.pap.*")mybatis的 ...
- spring 配置bean的方法及依赖注入发方式
Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...
- Spring入门第二课:Spring配置Bean的细节
1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...
- Spring配置Bean,为属性赋值
SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...
- 普通Java类获取Spring的Bean的方法
普通Java类获取Spring的Bean的方法 在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方 ...
- Spring -- 配置bean的三种方法
配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- spring 配置bean
Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...
随机推荐
- linux下tengine安装
1.什么是tengine? 说到tengine,首先还是得说下nginx了,大家对于nginx并不陌生,对于基本的需求都能满足,如果是涉及高级性能,那么就必须使用商用版nginx plus了,一谈到商 ...
- Spring AOP 实现读写分离
原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...
- docker基于Dockerfile命令创建支持ssh服务的镜像
首先,创建一个sshd_centos工作目录: [root@localhost ~]# mkdir sshd_centos [root@localhost ~]# cd sshd_centos [ro ...
- java工具类-读配置文件
///读配置文件 import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.M ...
- RelativeLayout相对布局 各个属性详解
RelativeLayout相对布局 相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一.它灵活性大很多,当然属性也多,操作难 ...
- USACO 2016 February Contest, Gold解题报告
1.Circular Barn http://www.usaco.org/index.php?page=viewproblem2&cpid=621 贪心 #include <cstd ...
- hadoop启动时,报ssh: Could not resolve hostname xxx: Name or service not known
本文转载自:http://blog.csdn.net/wodewutai17quiet/article/details/76795951 问题:hadoop启动时,报ssh: Could not re ...
- jsp中9个隐含对象
在JSP中一共有9个隐含对象,这个9个对象我可以在JSP中直接使用.因为在service方法已经对这个九个隐含对象进行声明及赋值,所以可以在JSP中直接使用. - pageContext 类型:Pag ...
- web攻击之一:XSS跨站脚本
一.浏览器安全 同源策略 影响源的因素:host,子域名,端口,协议 a.com通过以下代码: <script scr=http://b.com/b.js> 加载了b.com上的b.js, ...
- CentOS6.5 安装mysql-5.7.9
转自:http://forrest-lv.iteye.com/blog/2260703 安装前,需要检查是否已经有mysql服务进程,是否已经装过mysql; 这点很重要,我之前安装CentOS的同 ...