概要:

实例代码具体解释:

文件夹结构

Car.java

package com.coslay.beans.factorybean;

public class Car {
private String brand;
private double 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 + "]";
} public Car() {
System.out.println("Car's Constructor...");
} public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
} }

CarFactoryBean.java

package com.coslay.beans.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 {
return new Car("BMW",5000000);
} //返回bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
} @Override
public boolean isSingleton() {
return true;
} }

Main.java

package com.coslay.beans.factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-beanfactory.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
}
}

beans-beanfactory.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来配置Bean的实例
class:指向FactoryBean的全类名
property:配置FactoryBean的属性 但实际返回的实例确是FactoryBean的getObject()方法返回的实例
-->
<bean id="car" class="com.coslay.beans.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean> </beans>

spring FactoryBean配置Bean的更多相关文章

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

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

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

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

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

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

  4. Spring 通过FactoryBean配置Bean

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

  5. 12.Spring通过FactoryBean配置Bean

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

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

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

  7. 通过FactoryBean配置Bean

    这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...

  8. spring4-2-bean配置-10-通过FactoryBean配置bean

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk8AAAFHCAIAAAA3Hj/JAAAgAElEQVR4nO2dzdX0rA2Gp6asclwQTW

  9. Spring_通过 FactoryBean 配置 Bean

    beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...

随机推荐

  1. win10 U盘安装ubuntu16.04双系统

    所需工具U盘,软件ultralISO.ubuntu16.04,自己使用的系统是win10 一.制作U盘启动盘 打开ultraISO软件 2 2  3 4 开始写入—>直到完成大概五分的样子 二. ...

  2. django1.8forms读书笔记

    一.HttpRequest对象的一些属性或方法 request.path,The full path, not including the domain but including the leadi ...

  3. spring mvc get方式乱码

    使用spring get方式,向后台传递参数的时候,出现乱码,网上搜索了好久,最后发现原因是server.xml中默认的配置有问题,修改如下就正确了,图片: 文本信息:     <Connect ...

  4. 【备用】SQL SERVER存储过程执行速度慢的问题

    今天看到了但是暂时没有时间研究,先留着备用(Parameter Sniffing) http://blog.csdn.net/emili/article/details/2192081 http:// ...

  5. 写个关于使用cocostudio Armature实现动画自由切换的小demo

    这是一个关于使用cocostudio实现动画自由切换的小demo auto sprite =Sprite::create("background.png"); sprite-> ...

  6. 使用OleDB组件连接和访问Oracle数据库

    访问 Oracle 数据库的步骤 .在 Oracle 中,创建一个名为 TestTable 的表,如下所示: Create Table TestTable (c1 )); .将数据插入到 TestTa ...

  7. python 字符串 大小写转换 以及一系列字符串操作技巧

    总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...

  8. Ubuntu下迁移MySQL数据库文件目录

    用Ubuntu的apt包管理工具安装的mysql数据库,默认将数据库文件保存在/var/lib/mysql目录下,时间久了数据库越来越大,所以准备挂载个新的硬盘专门存放mysql数据库. 1.确定my ...

  9. [转][SQL] SSIS 简单应用 数据库汇入导出设定& SQL Agent定期排程

    前言 本篇不是要說高深的SSIS 技巧,而是用實例的方式,說明如何應用 Visual Studio 的「Business Intelligence Projects」來建立「Integration S ...

  10. 支持移动触摸设备的简洁js幻灯片插件

    lory是一款支持移动触摸设备的简洁的js幻灯片插件.该幻灯片插件可以通过纯js调用,也可以将该幻灯片插件作为jQuery插件来使用.该幻灯片的过渡动画具有硬件加速功能,并且可以定制是否使用easin ...