spring FactoryBean配置Bean
概要:
实例代码具体解释:
文件夹结构
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的更多相关文章
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
- Spring工厂方法(factory-bean)配置bean
在spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- Spring(十四):使用FactoryBean配置Bean
FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- 12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- 通过FactoryBean配置Bean
这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...
- spring4-2-bean配置-10-通过FactoryBean配置bean
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk8AAAFHCAIAAAA3Hj/JAAAgAElEQVR4nO2dzdX0rA2Gp6asclwQTW
- Spring_通过 FactoryBean 配置 Bean
beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...
随机推荐
- VS2008配置OpenGl 亲测可行
OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性.1.与C语言紧密结合.OpenGL命令最初就是用C语言函数来进行描述的,对于学习过C语言的人来讲,OpenGL是 ...
- C语言:冒泡排序法:将若干字符串按字母顺序(由小到大)排序输出
#include<stdio.h> #include<string.h> void sort(char *a[]); void print(char *a[]); int ma ...
- mac重装系统
通过 macOS 恢复功能启动macOS 恢复功能会根据您在电脑启动时按下的组合键来安装不同版本的 macOS.在按电源按钮打开 Mac 后,立即按住以下组合键之一.然后在看到 Apple 标志或旋转 ...
- linux下man手册简介
Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可.Linux 的man手册共有以下几个章节: 1.Standard commands (标准命令)2.S ...
- [转] 职业规划:一个老鸟眼中“IT民工”的发展方向
IT行业“挣钱太容易”,“IT不像政府管房产这么严,想干嘛就干嘛,另外都跑到境外去上市,没干两年市值翻好多倍,利润比地产高出几十倍几千倍,我们投入10块钱赚1块钱,IT行业投入10块钱赚1000块钱, ...
- "高级"数据库小结
刚刚考完高级数据库,这里有多高级等后面考完组合数学和算法再细聊.试卷在往年基础上改进了部分题目(貌似有人知道新题目的来源,如查询优化树),也可能是两份试卷交叉年份考.总之会的还是会,没看过的当然不会只 ...
- struts2请求两次即action方法执行两次
如果方法使用get开头,也会出现重复执行问题....对struts2无语!!! [转]http://www.xuebuyuan.com/301066.html 就把get开头的方法修改了一下,程序就运 ...
- 在WEB开发的时候导入各种jar包
使用eclipse导入很简单 右击你的project,选择properties,然后选择java build path,接着选择libraries,点击add external jars即可 如果你还 ...
- Android.util.Log 关于Android开发中打印log
日常Android开发真机调试过程经常会遇到系统日志过多过快,想看的内容一闪而过的问题.而自定义些log可以很好的解决这些问题. 代码中添加 log androidsdk中提供了log输出的ap ...
- #include <algorithm>中sort的一般用法
1.sort函数的时间复杂度为n*log2(n),执行效率较高. 2.sort函数的形式为sort(first,end,method)//其中第三个参数可选. 3.若为两个参数,则sort的排序默认是 ...