Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置、用工厂方法配置,FactoryBean又是什么呢
有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适。
FactoryBean是一个接口,要用的话就要实现它。他有三个方法:
getObject() //返回bean对象
getObjectType() //返回bean的类型
isSingleton() //是否单例
弄个例子:
Car类
package com.guigu.spring.factorybean;
public class Car {
private String brand;
private double price;
public Car(){
}
public Car(String brand,double price){
this.brand=brand;
this.price=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 "Car [brand=" + brand + ", price="+price +"]";
}
}
CarFactoryBean.java
package com.guigu.spring.factorybean;
import org.springframework.beans.factory.FactoryBean;
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
//返回bean对象
@Override
public Car getObject() throws Exception {
return new Car(brand,500000);
}
//返回bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
}
//是否单例
@Override
public boolean isSingleton() {
return true;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
bean-beanfactory中要配置的bean
<bean id="car" class="com.guigu.spring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
main函数
public static void main(String[] args) {
ApplicationContext ctx =new ClassPathXmlApplicationContext("bean-beanfactory.xml");
Car car=(Car) ctx.getBean("car");
System.out.println(car);
}
输出:
Car[brand=BMW, price=500000]
学到这里,我想了想,好像和一开始用类名配置的没什么区别啊!
再看一下xml中的笔记:
<!--
通过 FactoryBean 来配置Bean 的实例
class : 指向 FactoryBean 的全类名
property :配置 FactoryBean 的属性
但实际返回实例确是 FactoryBean 的 getObject() 方法返回的实例! --> <bean id="car" class="com.guigu.spring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
但实际返回实例确是 FactoryBean 的 getObject() 方法返回的实例!
估计就是这里了,区别应该就是父接口带来的三个方法,后面会继续看
另外说一下,这个FactoryBean 不是前面的BeanFactory
之前的笔记:

Spring学习记录(十)---使用FactoryBean配置Bean的更多相关文章
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- Spring学习记录(十四)---JDBC基本操作
先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...
- Spring 学习记录6 BeanFactory(2)
主题 除了Spring 学习记录5 BeanFactory 里写的几个接口外,BeanFactory的实现类还实现了一些其他接口,这篇文章主要介绍这些接口和实现类. 结构 DefaultListabl ...
- 我的Spring学习记录(二)
本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
- 我的Spring学习记录(五)
在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...
- Spring 学习记录8 初识XmlWebApplicationContext(2)
主题 接上文Spring 学习记录7 初识XmlWebApplicationContext refresh方法 refresh方法是定义在父类AbstractApplicationContext中的. ...
- Spring 学习记录3 ConversionService
ConversionService与Environment的关系 通过之前的学习(Spring 学习记录2 Environment),我已经Environment主要是负责解析properties和p ...
随机推荐
- wpf,能够复制文字 及自动识别URL超链接的TextBlock
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- Win7下VS2008破解方法
在Win7系统下,无法像xp下通过“控制面板”卸载的方法重新输入序列号来破解VS2008. 但可以通过以下几个步骤来破解: 1.首先需要安装VS2008,可以安装VS2008专业版90天试用版或VS2 ...
- 【转】SHELL中的IFS详解
转自:http://smilejay.com/2011/12/bash_ifs/ 在bash中IFS是内部的域分隔符,manual中对其的叙述如下: IFS The Internal Field Se ...
- $(function(){}) 与(function(){})()在执行时的优先级
$(function(){}) 是在页面DOM元素加载完成后执行,这时页面中的DOM对象都可以找到; (function(){})()是匿名函数,按页面从上到下顺序,执行到它时才执行,这时可能有的在此 ...
- wex5中的星星评分
新建一个空白的.w文件,然后在页面上放5个img星星图片 重要的是图片路径不能是绝对路径,要用相对路径,不然js操作的时候会出bug 添加两个label标签(标签随你挑,在这我就用label) 你到时 ...
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- springMVC4 注解配置实例
结构: maven配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...
- 对Java初学者的忠告
1) 适合自己的图书才是最好的,最好的书并不一定适合你,看自己的情况. 如果你是一个Java初学者一上手就捧一本Thinking in Java在手里,我想你的日子是不会好过的,那样的书给有一定基础的 ...
- Intellij IDEA 常用快捷键
[常规] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...
- error C2678
自定义结构类型,为支持插入到stl set或者排序,一种方式是重载operator<运算符成员函数.如果忘记将函数标识为const,则在编译时会报 6>c:\program files ( ...