学习Spring(二) 调用静态工厂方法创建Bean
1,创建抽象的产品类
package com.robert.spring.shop;
public abstract class Product {
}
2,创建具体产品类
package com.robert.spring.shop;
public class Battery extends Product {
private String name;
private double price;
public Battery(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "name=" + name + "\tprice=" + price;
}
}
package com.robert.spring.shop;
public class Disc extends Product {
private String name;
private double price;
public Disc(String name,double price){
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "name="+name+"\tprice="+price;
}
}
3,创建工厂类
package com.robert.spring.shop;
public class ProductCreator {
public static Product createProduct(String productId){
if("aaa".equals(productId)){
return new Battery("AAA",2.5);
} else if("cdrw".equals(productId)){
return new Disc("CD-RW",1.5);
}
throw new IllegalArgumentException("Unknown product");
}
}
4,创建测试类
package com.robert.spring.shop; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProductTest { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/products.xml");
Product battery = (Battery)context.getBean("aaa");
System.out.println(battery.toString()); Product cdrw = (Disc)context.getBean("cdrw");
System.out.println(cdrw.toString());
}
}
Spring配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Application context definition. -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
default-autowire="byName"> <bean id="aaa" class="com.robert.spring.shop.ProductCreator" factory-method="createProduct">
<constructor-arg value="aaa"/>
</bean> <bean id="cdrw" class="com.robert.spring.shop.ProductCreator" factory-method="createProduct">
<constructor-arg value="cdrw"/>
</bean>
</beans>
输出结果为:
name=AAA price=2.5
name=CD-RW price=1.5
学习Spring(二) 调用静态工厂方法创建Bean的更多相关文章
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...
- Spring 工厂方法创建Bean 学习(三)
1, 静态工厂方法创建Bean 调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节. 要声明通过静态方法创建 ...
- Spring初学之通过工厂方法配置Bean
工厂方法配置bean分为两种,一种是实例工厂方法,另一种是静态工厂方法. 先来看看实体bean: Car.java: package spring.beans.factory; public clas ...
- Spring4学习回顾之路07- 通过工厂方法配置Bean
一:通过静态工厂配置Bean 建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Des ...
- HttpSolrServer-采用静态工厂方法,创建HttpSolrServer单实例
HttpSolrServer线程安全,如果使用下面构造器,必须对所有的请求重用相同的实例.如果实例在运行中创建的,它可能会导致连接泄漏.推荐的做法就是保持每个solr服务url的HttpSolrSer ...
- 实例/静态工厂方法得到bean
<bean id="a" class="com.yundaex.wms.config.TestBeanChild" /> <bean id=& ...
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)
通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...
- 静态工厂方法和实例工厂方法及普通的bean
容纳你的bean bean工厂:最简单的容器,提供了基础的依赖注入支持.创建各种类型的Bean. 应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务. ...
随机推荐
- 彻底解决mysql中文乱码的办法,修改mysql解压缩版(免安装版或zip版)字符编码
MySQL会出现中文乱码的原因不外乎下列几点:1.server本身设定问题,例如server字符编码还停留在latin12.table的语系设定问题(包含character与collation)3.客 ...
- UVa 297 Quadtrees -SilverN
A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...
- [PHP]swoole_server几个进程的分工
readme.md-/Users/zjh/Documents/我的文章/[PHP]swoole_server几个进程的分工 html{font-family: sans-serif;-ms-text- ...
- 通过Hander进行界面刷新
Timer timer; TimerTask task; Handler handler;//先声明这3个变量 //在onCreate方法内 handler = new Handler(){ @Ove ...
- easyUI的dateBox控制时间格式
<input type='text' name='yearQuery' class='easyui-datebox ' data-options="formatter:myformat ...
- [No00000E]PPT快捷键大全 PowerPoint2013/2010/2007/2003常用快捷
熟练掌握PowerPoint快捷键可以让我们更快速的制作PPT模板,大大的节约时间成本.想提高工作效率吗?请熟悉PowerPoint快捷键吧!想成为高手吗?请先了解PPT快捷键吧!想制作出一个优秀的P ...
- 转:Configure your eclipse for C++
from: http://omtlab.com/configure-your-eclipse-for-c/ Configure your eclipse for C++ July 7, 2013 Th ...
- python日期格式化与绘图
画一个量随着时间变化的曲线是经常会遇到的需求,比如画软件用户数的变化曲线.画随时间变化的曲线主要用到的函数是matplotlib.pyplot.plot_date(date,num).由于其第一个变量 ...
- 自定义右键菜单中bug记录
今天客服部提出一个Bug,拖动滚动条到底部右键表格下方的数据,然后点击拨打电话打出的是表格上面的号码,看了下代码发现bug的原因是因为获取表格中电话号码的方式是通过给tr绑定了mouseover事件, ...
- BZOJ 3294: [Cqoi2011]放棋子
3294: [Cqoi2011]放棋子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 628 Solved: 238[Submit][Status] ...