(转)Spring的三种实例化Bean的方式
http://blog.csdn.net/yerenyuan_pku/article/details/52832793
Spring提供了三种实例化Bean的方式。
使用类构造器实例化。
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>不难看出,我们以前使用的就是该方式。上面的配置默认使用的是PersonServiceBean类的默认构造函数来实例化PersonServiceBean对象的。
使用静态工厂方法实例化。
我们在编码剖析Spring管理Bean的原理案例的基础上使用这种方式来实例化bean。
首先我们要在cn.itcast.service.impl包中创建一个工厂类——PersonServiceBeanFactory.java,其代码如下:public class PersonServiceBeanFactory {
public static PersonServiceBean createPersonServiceBean() {
return new PersonServiceBean();
}
}然后修改Spring的配置文件为:
<?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"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
<bean id="personService2" class=" cn.itcast.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean" /> </beans>最后,将SpringTest类的改为:
public class SpringTest { @Test
public void test() {
// ApplicationContext是接口
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
PersonService personService = (PersonService) ctx.getBean("personService2"); // 从Spring容器取得bean
personService.save();
} }测试test()方法,Eclipse控制台打印如下:

使用实例工厂方法实例化。
我们同样在编码剖析Spring管理Bean的原理案例的基础上使用这种方式来实例化bean。
首先我们要修改工厂类——PersonServiceBeanFactory.java的代码为:public class PersonServiceBeanFactory {
public static PersonServiceBean createPersonServiceBean() {
return new PersonServiceBean();
} public PersonServiceBean createPersonServiceBean2() {
return new PersonServiceBean();
}
}紧接着修改Spring的配置文件为:
<?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"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
<bean id="personService2" class=" cn.itcast.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean" />
<bean id="personServiceBeanFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceBeanFactory" factory-method="createPersonServiceBean2"></bean>
</beans>最后,将SpringTest类的改为:
public class SpringTest { @Test
public void test() {
// ApplicationContext是接口
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
PersonService personService = (PersonService) ctx.getBean("personService3"); // 从Spring容器取得bean
personService.save();
} }测试test()方法,Eclipse控制台打印如下:

Spring提供了三种实例化Bean的方式,那么到底该使用哪种方式较稳妥呢?应根据实际情况决定,但可这样说,90%的可能都是采用第一种方式,即使用类构造器实例化bean。源码可点击Spring的三种实例化Bean的方式进行下载。
(转)Spring的三种实例化Bean的方式的更多相关文章
- Spring第四弹—–Spring的三种实例化bean的方式
1.使用类构造器实例化 1 <bean id=“orderService" class="cn.itcast.OrderServiceBean"/> 2. ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- Spring中四种实例化bean的方式
本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...
- spring三种实例化bean的方式
1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...
- 三种实例化bean的方式
在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化所采用的配置是不一样的: 一. ...
- Spring三种实例化Bean的方法
1.实例化bean的三种方法:(1) 构造器<!-- 体验1 --><bean id="personService" class="com.persia ...
- Spring(三)实例化Bean以及注入对象
使用xml实例化bean 在xml中实例化bean的三种方式 <bean id="springService" class="com.zhiyou100.crm.t ...
- Spring的几种注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- Spring框架几种创建bean的方式
Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题. 一.使用XML显 ...
随机推荐
- [SoapUI] Learn materials
SoapUI Training : http://soapui-tutorial.com/index.php * Below are the details to access the onlin ...
- 【前端】CentOS 7 系列教程之二: 安装 git 最新版
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_2.html 这一篇我们来安装git高版本. 卸载yum安装的旧版本 yum remove git 安装 ...
- LOG.ZS.0001.基于Freetype的游戏字体渲染优化思路
Total Utf8-ucs2 Html_parse Layout Render_string Init_texture Ft_load_glyph 原始 2293 1 26 708 1556 2 1 ...
- 1 model的创建
extJs数据模型之Model博客分类: ExtJs 1 model的创建 //我们利用Ext.define来创建我们的模型类 //DB table person(name,age,email) ...
- python的time模块使用
在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...
- java中为什么inputstreamreader和buffered reader要配合着用
因为InputStreamReader是字节输出(汉字会被分为两个字节),而BufferedReader是它的“包装”(整行读取),效率更高,所以配合使用更好.可以通过BufferedReader 流 ...
- Jmeter学习之While Controller
参考 https://www.cnblogs.com/richered/p/8404641.html https://blog.csdn.net/rwang99/article/details/511 ...
- 用文件作为Swap分区
用文件作为Swap分区 1.创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小).# dd if=/dev/zero ...
- bzoj 1396: 识别子串 && bzoj 2865: 字符串识别【后缀数组+线段树】
根据height数组的定义,和当前后缀串i最长的相同串的长度就是max(height[i],height[i+1]),这个后缀贡献的最短不同串长度就是len=max(height[i],height[ ...
- 第九篇 .NET高级技术ref、out
普通参数是“值类型传递拷贝,引用类型传递引用”,但是都不能在函数内部修改外部变量的指向(p.Age=5不是可以吗?),这时候要用ref或者out(相当于把变量都传进去了),他们的作用不同:ref的作用 ...