beans-factorybean.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.hy.spring.beans.factoryBean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>

Car.java

package com.hy.spring.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 is Counstructor ....");
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
}

CarFactoryBean.java

package com.hy.spring.beans.factoryBean;

import org.springframework.beans.factory.FactoryBean;

//自定义的 FactoryBean 需要spring 提供的 FactoryBean 接口
public class CarFactoryBean implements FactoryBean<Car> {

private String brand;

public void setBrand(String brand) {
this.brand = brand;
}

//返回Bean的对象
public Car getObject() throws Exception {
return new Car(brand,500000);
}

//返回bean的类型
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Car.class;
}

public boolean isSingleton() {
return true;
}

}

Main.java

package com.hy.spring.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);

}

}

Spring_通过 FactoryBean 配置 Bean的更多相关文章

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

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

  2. Spring 通过FactoryBean配置Bean

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

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

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

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

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

  5. 通过FactoryBean配置Bean

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

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

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

  7. spring FactoryBean配置Bean

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

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

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk8AAAFHCAIAAAA3Hj/JAAAgAElEQVR4nO2dzdX0rA2Gp6asclwQTW

  9. 12.Spring通过FactoryBean配置Bean

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

随机推荐

  1. NSTimer的循环引用

    在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...

  2. SQL Server 数据库分离与附加(图文教程)

    from:http://www.jb51.net/article/36624.htm 一.概述 SQL Server提供了“分离/附加”数据库.“备份/还原”数据库.复制数据库等多种数据库的备份和恢复 ...

  3. 面试心得与总结—BAT、网易、蘑菇街 - ImportNew

    1. 九种基本数据类型的大小,以及他们的封装类. 2. Switch能否用string做参数? 3. equals与==的区别. 4. Object有哪些公用方法? 5. Java的四种引用,强弱软虚 ...

  4. docker 中安装 FastDFS 总结

    如题,参考各资料后,安装FastDFS总结.基于已有docker镜像 https://hub.docker.com/r/luhuiguo/fastdfs/ docker pull luhuiguo/f ...

  5. 邮件发送模型及其Python应用实例

    SMTP(Simple Mail Transfer Protocol) 制定: First:RFC 788 in 1981 Last:RFC 5321 in 2008 端口: TCP 25(SMTP) ...

  6. [Algorithms] Graph Traversal (BFS and DFS)

    Graph is an important data structure and has many important applications. Moreover, grach traversal ...

  7. vue+node+mongoDB 火车票H5(二)---vux和less的配置

    vue基本环境配置好之后,就可以开始开发页面了 开发页面之前先了解一下项目结构,原始的目录结构为: config是配置文件,环境配置好了开发阶段可以不再去修改了,node_modules文件夹是打包的 ...

  8. 疯狂Html+CSS+JS 中JS总结

    来自:http://mzkmzk.github.io/blog/2015/10/05/amazeing-js/ 0 总结 本书的JS 第一章有讲语法有挺多常见的坑点和原理解释很不错 第二章DOM编程讲 ...

  9. Spoken English Practice( Believe it or not, I don't need to make believe its a big deal. (believe,deal, You don't say))

    音标复习                                                绿色:连读:红色:略读:蓝色:浊化:橙色:弱读 口语蜕变(2017/6/25) Sorry, t ...

  10. transaction 数据库事务 roolback 回滚

    事务是恢复和并发控制的基本单位 https://baike.baidu.com/item/数据库事务/9744607 事务有三种模型: 1.隐式事务是指每一条数据操作语句都自动地成为一个事务,事务的开 ...