实体bean:

Car.java:

package spring.beans.factorybean;

public class Car {
private String name;
private int price;
public Car(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
CarFactoryBean.java:
package spring.beans.factorybean;

import org.springframework.beans.factory.FactoryBean;

/**
* 通过factoryBean来创建实例
* @author Administrator
*
*/
public class CarFactoryBean implements FactoryBean<Car> { private String name;
private int price; public void setName(String name) {
this.name = name;
} public void setPrice(int price) {
this.price = price;
} /**
* 返回一个bean实例
*/
@Override
public Car getObject() throws Exception { return new Car(name, price);
} /**
* 返回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配置文件:

<?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来创建实例
我们设置的是CarFactoryBean的属性,
返回的是Car的实例
-->
<bean id="car" class="spring.beans.factorybean.CarFactoryBean">
<property name="name" value="奥迪"></property>
<property name="price" value="300000"></property>
</bean> </beans>

Spring初学之FactoryBean配置Bean的更多相关文章

  1. Spring工厂方法(factory-bean)配置bean

    在spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  2. Spring基础——在Spring Config 文件中配置 Bean

    一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...

  3. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  4. Spring学习记录(十)---使用FactoryBean配置Bean

    之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...

  5. Spring(十四):使用FactoryBean配置Bean

    FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...

  6. Spring 通过FactoryBean配置Bean

    1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...

  7. spring FactoryBean配置Bean

    概要: 实例代码具体解释: 文件夹结构 Car.java package com.coslay.beans.factorybean; public class Car { private String ...

  8. 12.Spring通过FactoryBean配置Bean

    为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...

  9. Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean

    1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...

随机推荐

  1. Linux I/O 进阶

    非阻塞I/O 阻塞I/O对应于低速的系统调用,可能会使进程永远阻塞.非阻塞I/O可以使我们发出open.read.write这样的I/O操作,并使这些操作不会永远阻塞.如果这种操作不能完成,则调用立即 ...

  2. FFF at Valentine(强连通分量缩点+拓扑排序)

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 从“关于Java堆与栈的思考”一帖看错误信息的传播

    我对转贴的信息一直有敌意,原因如下:首先,除了制造更多的信息垃圾,转贴不会带来新的价值,想收藏的话一个链接足矣:其次,将错误信息以讹传讹,混淆视听.不妨选一个典型的例子说明一二. 相信<关于Ja ...

  4. Connection cannot be null when 'hibernate.dialect' not set

    严重: Exception sending context initialized event to listener instance of class [org.springframework.w ...

  5. Redis3.2.5配置主从服务器遇到的一些错误

    注意:关闭主从服务器的防火墙 问题一: WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net ...

  6. 深入struts2.0(六)--ActionProxy类

    1.1     ActionProxy接口以及实现 ActionProxy在struts框架中发挥着很关键的数据. 通过webwork和xwork交互关系图能够看出.它是action和xwork中间的 ...

  7. Sql多条件排序

    多条件排序可以通过在order by语句后面使用case when then条件语句来实现. end 例子: 1.创建表case_test 共有id,case_type,case_location,c ...

  8. nodejs中全栈开发框架meteor的文档

    http://wiki.jikexueyuan.com/project/discover-meteor/routing.html,   这本书的源码地址: https://github.com/Dis ...

  9. Eclipse Find/Replace

    1.Eclipse内容助手 选中Regular expressions,使用正则表达式进行匹配.图中出现了小黄灯,Ctrl+Space显示出帮助信息. 2.Wrap search(循环检索)选中后,检 ...

  10. 算法寒假实习面试经过之 十一贝(offer) 联想研究院(电话一面 被拒)

    联想研究院 1面 自我介绍 聊比赛,讲了讲jdd的. 感觉都快要背过了... 之前重复的问题就不说了,说一下印象比较深的 adaboost 与gbdt的区别 随机森林,如果有t个特征,n个树,每个树深 ...