实体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. Centos7配置外部网络访问

    Centos7配置外部网络访问 一.安装步骤中的重要配置: 默认是动态ip配置,有需要可以改成静配置 BOOTPROTO="static" 二.如果不能联网,按照如下步骤设置网络: ...

  2. mysql 日期加减天数

    MySQL 为日期增加一个时间间隔:date_add() now()       //now函数为获取当前时间 select date_add(now(), interval 1 day); - 加1 ...

  3. MVC5学习系列

    前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...

  4. 剑指offer 面试17题

    面试17题: 题目:打印从1到最大的n位数 题:输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1.2.3一直到最大的3位数999. 解题思路:需要考虑大数问题,这是题目设置的陷 ...

  5. Python之迭代器和生成器(Day17)

    一.可迭代对象(iterable) 刚才说过,很多容器都是可迭代对象,此外还有更多的对象同样也是可迭代对象,比如处于打开状态的files,sockets等等.但凡是可以返回一个迭代器的对象都可称之为可 ...

  6. Oracle处理Clob类型数据入库(String入库)

    从网上查找一堆参考,要么语焉不详,要么不可行.自己鼓捣了一堆可以正常入库了.请看最后: insert into CP_V_INFO" + "(ID, "+ "P ...

  7. Android:日常学习笔记(4)——探究活动(1)

    Android:日常学习笔记(4)——探究活动 什么是活动: 活动是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户进行交互. 手动创建活动 创建空活动 1.新建活动时选择Add ...

  8. extern "C" 有关问题

    之前帮老板编译一个库的代码,遇到了一些问题,后来发现问题出现在extern "C"语法上. 1. C/C++语法extern 关键字 extern是C/C++语言中表明函数和全局变 ...

  9. 转:MFC中屏蔽ESC和回车关闭对话框

    解决方法是在 CDialog::PreTranslateMessage() 的重载函数中将ESC和回车按键的消息处理掉. 直接上代码: CResultCollectorDlg::PreTranslat ...

  10. Nginx负载均衡案例

    nginx负载均衡配置,windows版本和linux版本的nginx除了启动方式其他基本无差异. 1.Niginx安装 参考:https://www.cnblogs.com/zwcry/p/9454 ...