最近在看spring ioc源码,看到FactoryBean这个内容。这个和BeanFactory的区别

1. BeanFactory: 生成bean的工厂,是一个接口,定义了很多方法

2. FactoryBean: 是一个Bean,生产bean的bean

下面是一个demo,用于获取Person的FactoryBean

Person.java

/**
* @Author: <guanxianseng@163.com>
* @Description:
* @Date: Created in : 2018/12/10 8:01 AM
**/
public class Person {
private String name;
private int age;
private String city; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", city='" + city + '\'' +
'}';
}
}

PersonFactoryBean.java

import org.springframework.beans.factory.FactoryBean;

/**
* @Author: <guanxianseng@163.com>
* @Description:
* @Date: Created in : 2018/12/10 8:02 AM
**/
public class PersonFactoryBean implements FactoryBean<Person> {
private String personInfo; public Person getObject() throws Exception {
Person person = new Person();
String[] personInfos = personInfo.split(",");
person.setName(personInfos[0]);
person.setAge(Integer.parseInt(personInfos[1]));
person.setCity(personInfos[2]);
return person;
} public Class<?> getObjectType() {
return Person.class;
} public boolean isSingleton() {
return true;
} public String getPersonInfo() {
return personInfo;
} public void setPersonInfo(String personInfo) {
this.personInfo = personInfo;
}
}

beans.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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="person" class="com.gxf.mybatis.TestFactoryBean.PersonFactoryBean">
<property name="personInfo">
<value>guanxianseng,28,chengdu</value>
</property>
</bean> </beans>

Test.java测试一下

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @Author: <guanxianseng@163.com>
* @Description:
* @Date: Created in : 2018/12/10 8:05 AM
**/
public class Test { public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mytestbeans.xml");
PersonFactoryBean personFactoryBean = (PersonFactoryBean) applicationContext.getBean("&person");
System.out.println("personFactoryBean:" + personFactoryBean);
System.out.println("personFactoryBean.getObject()" + personFactoryBean.getObject());
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
}
}

这里主要测了两个内容

1. FactoryBean.getObject()返回Person

2. 从spring容器中获取factoryBean实例的前缀 &

TODO:

1. factorybean 和其他bean实例化有什么不同

2. factorybean 和其他bean从spring中get出来有什么不同

Spring FactoryBean用法的更多相关文章

  1. Java Spring AOP用法

    Java Spring AOP用法 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制 ...

  2. Java Spring IOC用法

    Java Spring IOC用法 Spring IoC 在前两篇文章中,我们讲了java web环境搭建 和 java web项目搭建,现在看下spring ioc在java中的运用,开发工具为In ...

  3. Spring JdbcTemplate用法整理

    Spring JdbcTemplate用法整理: xml: <?xml version="1.0" encoding="UTF-8"?> <b ...

  4. 转:Spring FactoryBean源码浅析

    http://blog.csdn.net/java2000_wl/article/details/7410714 在Spring BeanFactory容器中管理两种bean 1.标准Java Bea ...

  5. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client

  6. Spring FactoryBean应用

    Spring 中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean 即 FactoryBean.FactoryBean跟普通Bean不同,其返回的对象不是指定类的一个实例,而是该Facto ...

  7. Spring Aspect 用法略讲

    『配置Aspect』 若要启用AspectJ风格的注解则必须额外的导入AspectJ的jar包,此外还需要在spring的配置文件中进行配置,配置方式有两种; 一.在配置文件的Schema中进行配置 ...

  8. Spring @Value 用法小结,#与$的区别

    20161016更新:这货其实是SpEL的功能,来这里看看吧: Spring 4 官方文档学习(五)核心技术之SpEL 起因 一直的用法是 @Value("${jdbc.driverClas ...

  9. spring assert 用法

    spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...

随机推荐

  1. C#取得控制台应用程序的根目录方法

    如有雷同,不胜荣幸,若转载,请注明 取得控制台应用程序的根目录方法1:Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径2:AppDomain.Curren ...

  2. linux中创建一个回收站

      1. mkdir /tmp/trash_tmp 建立一个回收站目录 2. vi /bin/trash 编辑一个文件     mv $@ /tmp/trash_tmp     :wq 保存退出 3. ...

  3. iOS --UIScrollView的学习(一)

    1.为什么使用UIScrollView 因为移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限,当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容普通的UIV ...

  4. OmniMarkupPreviewer 404

    Here is the answer: Sublime Text > Preferences > Package Settings > OmniMarkupPreviewer > ...

  5. Springmvc之表单验证

    1.需要的相关jar 这里采用的是hibernate-validator-5.2.4.Final 和validation-api-1.1.0.Final 两个jar包.Hibernate Valida ...

  6. 使用hexo+coding搭建免费个人博客

    1.检测node和npm 先检测一下有没有node.js和npm $ node -v //如果有,说明node.js安装成功! $ node -v v8.4.0 //如果有,说明npm安装成功! $n ...

  7. C#数组,List,Dictionary,IQueryable,IEnumerable的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  8. JS正则表达式端口号,IP地址

    端口号:65535 正则:/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6 ...

  9. [BZOJ5248][2018九省联考]一双木棋

    题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=5248   Solution 我们首先考虑放棋子的操作 发现它一定放棋子的部分是一个联通块 ...

  10. (转)jieba中文分词的.NET版本:jieba.NET

    简介 平时经常用Python写些小程序.在做文本分析相关的事情时免不了进行中文分词,于是就遇到了用Python实现的结巴中文分词.jieba使用起来非常简单,同时分词的结果也令人印象深刻,有兴趣的可以 ...