12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean:
在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适。
public class Car {
private String brand;
private double price; public Car() {
System.out.println("com.cn.Car's Constructor");
} public Car(String brand, double price) {
this.brand = brand;
this.price = price;
} public void setBrand(String brand) {
System.out.println("setBrand");
this.brand = brand;
} public String getBrand() {
return brand;
} public void setPrice(double price) {
this.price = price;
} public double getPrice() {
return price;
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
/**
* 自定义的FactoryBean需要实现FactoryBean接口
*/
public class CarFactoryBean implements FactoryBean<Car>{
//FactoryBean自己的属性
private String brand; public void setBrand(String brand) {
this.brand = brand;
} //返回bean的对象
@Override
public Car getObject() throws Exception {
return new Car("BMW", 500000);
} @Override
public Class<?> getObjectType() {
return Car.class;
} @Override
public boolean isSingleton() {
return true;
}
}
<?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="factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("12-1.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOIAAAAnCAIAAACdVwOHAAADaElEQVR4nO2ZsY7UMBCG73V2UiEEFRKcdO7oTteuWwQPMOIhmJKOinkJbOleDQonjhPbu9lLlqzh/3RF5HUc2/NlbOfu3r2/f/X6Tfx7enrqMn5/+RQvZn+zOmlJrfBGIPrw/Pn44y0N149fifbuFChzd6mmZ8tzj29T067rHj8eYz+f7+Ho7bJIUwD2BZqCBoCmoAGgKWgAaAoaAJqCBoCmoAGgKWiAq2tqxTkn9u/+g4fIyhYP3aqd/weiPuDbNrtIUyvOe++9v/TxMcx977cL+ekGiawom+GnUNkPKJv+dmVDhtU5J8fDUZyyCTcm3U7a2Zb178BsXK7vs2F13ns39HyrklIHCnWu8W6f19Swiu0fafmymKU93rz3tQaJDKuyoWJlMqy9lMHyUXcrTqxhdSKhbqGdm6I4AzFeZFjYbFiSU6yzj6asL39kcOKqmgbhZhmlVrnXVPnhcBSnqsqWVVVHKzm0ppXA7EieO4+H42xKiUyMV1gKHg4Pm5Tk6Sl/lhkDvfHrfV7ToluGNUyW2GRBD+vnxEun05dMxJ2+q9iyTDe4ZOW0lDkxxmF5ip0REWUek6eIrtM07fNkgzEdaXHTEjYg6eaq35JUop5OaTJjk7yQqrympLRqVZdKw7rtgeQlmk53nMmkZ+9clk3TtbRwV63lNADJ/Rek51I2FWvFey92+MmK915VQ8hfPKdLRtploaU4WUOWmoy0skEksjYsu1ZcHNeumu6TTYuL/vjeT61aMpL8YknLtYuLF/00DIY1HJsSTYVD4UpNF4x0rum8TrrKu3MjHca1zRK/etHf4QglMWbhCLUwDF1N00mOKcz1OTuH+5NUfZZyNqXpuT5Ya7fTtD7SrqBpP56u64y1hij1oPqgKEefwmg81ljR2WFoXUlOsc4+msaPDulSGHeH3vtfP799T/Lf9N4x/xNZ1vm+s5AVqi2Pm7lQx6lqtr0rMjt8JFsIw2yzN+HULC+JQfq42kjLhcPYx5DHtaW26I/b9DjP1/ogVUorpQ9Sl6SPhfybn/evx5IvqddIJ63Q78J3+bwPAku+pNZWCbAGaAoaAJqCBoCmoAGgKWgAaAoaAJqCBoCmoAGgKWgAaAoaAJqCBoCmoAGgKWgAaAoaAJqCBoCmoAGgKWgAaAoaAJqCBvgD55Bufbghe3gAAAAASUVORK5CYII=" alt="" />
12.Spring通过FactoryBean配置Bean的更多相关文章
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- 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
概要: 实例代码具体解释: 文件夹结构 Car.java package com.coslay.beans.factorybean; public class Car { private String ...
- Spring初学之FactoryBean配置Bean
实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...
- 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
随机推荐
- 8.20~8.25刷散题记录 By cellur925
记录一些散题 / 价值不大但还是想记下来的题目 / 没正八经写博客的题目 8.24 Luogu P1508 沙雕题数字三角形的二维升级版,但是注意阅读理解,李大水牛从桌子最后一行下侧开始吃,而本题是自 ...
- 标准块CP功能实现
#include<stdio.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_fp; int src_ret; ]={}; ) ...
- _bzoj1088 [SCOI2005]扫雷Mine【dp】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 简单的状压dp(话说本题的编号减1,即1087,也是一道状压dp),不解释. #inc ...
- ios 创建和绘画pdf文件 -转
转自:http://blog.csdn.net/ant1239/article/details/7761676 本方法为项目中画pdf的一个方法,画pdf,一共分为几步,1,获取地址,有两种获取地址方 ...
- [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串
题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...
- Vue.js学习笔记--2.基础v-指令
整理自官网教程 -- https://cn.vuejs.org/ 1. v-bind绑定Class与Style a. 绑定Class 语法:v-bind:class="{classname: ...
- swiper4初始化/swiper-init/data-swiper
用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- Beta测试团队
---恢复内容开始--- Beta版本测试 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/?page ...
- 异常详细信息: System.ComponentModel.Win32Exception: 信号灯超时时间已到