Spring_通过 FactoryBean 配置 Bean
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的更多相关文章
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring工厂方法(factory-bean)配置bean
在spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- 通过FactoryBean配置Bean
这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...
- Spring(十四):使用FactoryBean配置Bean
FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...
- spring FactoryBean配置Bean
概要: 实例代码具体解释: 文件夹结构 Car.java package com.coslay.beans.factorybean; public class Car { private String ...
- spring4-2-bean配置-10-通过FactoryBean配置bean
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk8AAAFHCAIAAAA3Hj/JAAAgAElEQVR4nO2dzdX0rA2Gp6asclwQTW
- 12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...
随机推荐
- Cocos2d-x 3.3Bate0 ExpandedListView
之前写的ExpandedListView版本号因为版本号升级这里提供Cocos2d-x 3.3Bate0 版本号 代码下载:http://download.csdn.net/detail/qqmcy/ ...
- uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)
option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...
- linux配置网关
linux配置网关 输入账号root 再输入安装过程中设置的密码,登录到系统 vi /etc/sysconfig/network-scripts/ifcfg-eth0 #编辑配置文件,添加修改以下内容 ...
- IOS开发报错之Undefined symbols for architecture armv6
本文转载至 http://blog.csdn.net/sanpintian/article/details/7575434 今天在项目中引入SVSegmentedControl.h/.my以及SVS ...
- Spring Data JPA 一:环境搭建
搭建开发环境是最麻烦的事情,各种冲突各种异常,记一下搭建过程,仅供大家参考: 用的gradle搭建的项目,先亮一下项目的大概目录: 注意一定要是这个 web工程用spring/src/main/web ...
- HTML中条件注释的高级应用
在页面头部加入 <!--[if lt IE 9]><html class="ie"><![endif]--> 可简单CSS Hack,IE6.I ...
- Powershell运行Invoke-Sqlcmd命令的先决条件
运行Invoke-Sqlcmd命令,使用这个命令需满足如下条件: 1.在运行服务器中安装SQL Server 2008 R2 Management ObjectsI 2.在运行命令 invoke-sq ...
- Casperjs中fill提交表单遇到的问题
1.if you access internet with proxy please add --ignore-ssl-errors=true --ssl-protocol=a ...
- Squid 缓存代理服务器的完整配置
Squid 缓存代理服务器 Squid 的作用 1.通过缓存的方式为用户提供web访问加速 2.对用户的web访问进行过滤控制 缓存代理服务器又分为普通代理服务器,透明代理服务器,和反向代理服务器. ...
- win7查看某个端口被占用的解决方法
1.开始---->运行---->cmd,或者是window+R组合键,调出命令窗口. 2.输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是491 ...