在实例化bean时,除了setter,constructor方法外,还有实例工厂方法,和静态工厂方法。

看代码:

People类的代码如下:

package com.timo.domain;

public class People {
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

//工厂方法如下:

package com.timo.domain;

public class PeopleFactory {
private static People people=new People();
//this is non-static method.
public People createPeopleInstance(){
return people;
}
}

配置文件

applicationContext-instance-factory.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">
<import resource="applicationContext.xml"/>
<import resource="applicationContext2.xml"/>
<bean id="peopleFactory" class="com.timo.domain.PeopleFactory"></bean>
<bean id="people" factory-bean="peopleFactory" factory-method="createPeopleInstance">
<property name="name" value="曹操"/>
<property name="age" value=""/>
</bean> <!-- more bean definitions go here --> </beans>

测试类如下:

package com.timo.test;

import com.timo.domain.People;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test5 {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-instance-factory.xml");
People people = applicationContext.getBean(People.class);
System.out.println("name="+people.getName()+" age="+people.getAge());
}
}

最新文章

  1. BZOJ 3159决战
  2. MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份]
  3. IOS UIScrollView中 使用 touch 无法响应的问题
  4. 转载:C# 之泛型详解
  5. 关于SVN版本控制器的问题与解决方法
  6. NSDictionary初始化,使用@{}方法,插入nil时会报空指针异常
  7. js 正则之检测素数
  8. Xshell利用登录脚本从服务器登录到另外一个服务器
  9. DOUAudioStreamer 中的DOUAudioFileProvider理解笔记
  10. SSH框架的多表查询(方法二)增删查改
  11. Javaweb-request与response
  12. hdu-4763(kmp+拓展kmp)
  13. hdfs 操作 入门api
  14. python if elif else判断语句
  15. CSS实现鼠标经过网页图标弹出微信二维码
  16. NXP ARM Vector Table CheckSum
  17. 2018.09.15 vijos1053Easy sssp(最短路)
  18. iOS开发证书与配置文件的使用
  19. SpringBoot 线程池配置 实现AsyncConfigurer接口方法
  20. C#.NET常见问题(FAQ)-如何批量增加或取消注释

热门文章

  1. ctf题目writeup(4)
  2. POJ1679(次小生成树)
  3. JS控制文本框输入的内容
  4. Linux下启动Oracle服务和监听程序步骤
  5. 【IIS】 常见问题
  6. SGU刷题之路,开始了
  7. 26、js阶段性复习
  8. Qt QPainter::end: Painter ended whith 2 saced states
  9. 爬取妹子图(requests + BeautifulSoup)
  10. 企业级Tomcat部署配置