Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1、实现Spring 提供的FactoryBean接口
package com.spring.facoryBean;
import org.springframework.beans.factory.FactoryBean;
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
public Car getObject() throws Exception {
return new Car(brand);
}
public Class<?> getObjectType() {
return Car.class;
}
public boolean isSingleton() {
return true;
}
}
2、配置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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--
通过FactoryBean 来配置Bean 的实例
class : 指向FactoryBean的全类名
property : 配置FactoryBean的属性 实际返回的是FactoryBean 的 getObject 方法的得到的实例 -->
<bean id="car" class="com.spring.facoryBean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>
3、bean文件
package com.spring.facoryBean;
public class Car {
public Car(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return "Car [brand=" + brand + "]";
}
private String brand;
public void setBrand(String brand){
System.out.println("setBrand...");
this.brand = brand;
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
4、main方法实现
package com.spring.facoryBean; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-beanFactory.xml");
Car car = (Car)ctx.getBean("car");
System.out.println(car);
}
}
Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean的更多相关文章
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- Spring4.0学习笔记(1) —— 基础知识
1.基本定义 IOC: 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了 IOC之后,容器主动将资源推送给它所管理的组件,组件索要做 ...
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
- Spring框架学习笔记(3)——配置bean
1.属性注入 (1)根据setter方法属性注入,这里使用的是property标签.需要bean属性提供对应的setter方法,比如笔记(1)里的 HelloWorld使用的就是这种方法. <! ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- Spring4.0学习笔记(4) —— 使用外部属性文件
1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Spring4.0学习笔记(2) —— 自动装配
Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...
- Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库
整体配置 1.配置xml文件 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
随机推荐
- wpa_cli和wpa_supplicant使用,配置无线AP名和密码,静态ip地址
配置静态ip方法分享:通过串口命令行输入如下命令: 1. 添加无线网络接入点(AP) 及其 密码:# wpa_cli -p /data/misc/wpa_supplicantwpa_cli v0.5. ...
- 【模拟】【数学】CSU 1803 2016 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 题目大意: 给定n,m(n,m<=109)1<=i<=n,1& ...
- QTP自传之对象库编程
对象库编程是我们平时工作中使用最多的编程方式,在自动化脚本开发中起到举足轻重的作用,与描述性性编程相比,更直接和易于维护,今天就和大家简单的聊聊如何进行对象库编程. 既然是对象库编程,肯定要对已存在于 ...
- iTerm2 + oh my zsh代替mac自带的bash shell
使用Solarized dark配色方案 需要字体menlo for powerline oh-my-zsh主题使用agnoster,这个主题默认的路径是全路径,当路径很长的时候,就会占很长的空间,可 ...
- XPath与Xquery
XPath 和 XQuery 在某些方面很相似.XPath 还是 XQuery 完整不可分割的一部分.这两种语言都能够从 XML 文档或者 XML 文档存储库中选择数据.本文简要介绍了 XPath 和 ...
- php总结 --- 19. 其他小知识
1. PHP博物馆 php各个版本的代码库 2. PHP-GTK php桌面程序 3. Pecl 4. Pear 5. php调试器 目前还不清楚具体有什么大的优势,为什么要用, IDE不能满足吗 6 ...
- Git push本地代码到新建远程仓库
快速搞定 1.git init #初始化本地仓库 2.git remote add origin https://git.oschina.net/redArmy/springboot-swagger ...
- IOS UIButton使用详解
第一.UIButton的定义 UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种, typede ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...
- 给iOS开发者的GCD用户手册
Grand Central Dispatch,或者GCD,是一个极其强大的工具.它给你一些底层的组件,像队列和信号量,让你可以通过一些有趣的方式来获得有用的多线程效果.可惜的是,这个基于C的API是一 ...