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. 面试题思考:interface和abstract的区别

    抽象类(abstract) 含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象. 含有abstract方法的类必须定义为abstract class,abstra ...

  2. boost::interprocess::shared_memory_object(1)(基本类型)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...

  3. linux系统中利用vagrant创建虚拟开发环境

    Vagrant简介 作为程序员,可能需要同时开发多个项目,使用多种编程语言,需要使用各种操作系统,如果将很多东西放在同一个电脑上,肯定会被各种配置环境搞晕.一个比较好的办法就是每个项目都有一个干净的开 ...

  4. 报错 findMergedAnnotation activemq

    springmvc 集成activemq引入activemq-all-5.14.4有冲突 springmvc 4.2.9集成activemq-5.14.4时报错,错误信息如下 NoSuchMethod ...

  5. Less-mixin判断(守卫)一

    mixin卫士--判断 类似于JavaScript的if/else example: .test(@a) when (@a>10){//当大于10 font-size:18px; } .test ...

  6. Active Object pattern

    http://www.ibm.com/developerworks/cn/java/j-lo-activeobject/ 之所以叫, 主动对象, 区别于被动对象, 只能被动被别人调用的对象, 而主动对 ...

  7. PHP数组遍历详解

    一.PHP数组简介 1.PHP数组的分类 按照下标的不同分为关联数组和索引数组①索引数组:下标从0开始依次增长②关联数组:下标为字符串格式,每个下标字符串与数组的值一一对应,(有点像对象的键值对) 下 ...

  8. &lt;context-param&gt;与&lt;init-param&gt;的差别与作用

    <context-param>的作用: web.xml的配置中<context-param>配置作用 1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...

  9. kotlin-plugin-1.1.2-release-Studio2.3-1.zip 下载地址

    1 官方下载地址,下载较慢,我家100m联通光纤,下载也就120k左右 http://jetbrains-plugins.s3.amazonaws.com/6954/34562/kotlin-plug ...

  10. Nodejs关闭windows服务进程

    1.根据端口号,查询进程信息命令: netstat -aon | findstr "端口号" 2.根据pid杀死进程命令: taskkill /F /pid 进程号 完整代码: c ...