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的方式的更多相关文章

  1. Spring第四弹—–Spring的三种实例化bean的方式

    1.使用类构造器实例化   1 <bean id=“orderService" class="cn.itcast.OrderServiceBean"/> 2. ...

  2. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  3. Spring中四种实例化bean的方式

    本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...

  4. spring三种实例化bean的方式

    1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...

  5. 三种实例化bean的方式

    在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化所采用的配置是不一样的: 一. ...

  6. Spring三种实例化Bean的方法

    1.实例化bean的三种方法:(1) 构造器<!-- 体验1 --><bean id="personService" class="com.persia ...

  7. Spring(三)实例化Bean以及注入对象

    使用xml实例化bean 在xml中实例化bean的三种方式 <bean id="springService" class="com.zhiyou100.crm.t ...

  8. Spring的几种注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  9. Spring框架几种创建bean的方式

    Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题. 一.使用XML显 ...

随机推荐

  1. apache 安装及配置

    近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.or ...

  2. rtmplib rtmp协议过程分析

    转自:http://chenzhenianqing.cn/articles/1009.html 写的很好,收藏如下,向作者致敬! 没事碰到了librtmp库,这个库是ffmpeg的依赖库,用来接收,发 ...

  3. 让ie支持css3的一些htc文件

    1. Dean Edwards的IE7.js (以及 IE8.js, IE9.js)这个玩意估计是试图让IE支持CSS3属性的鼻祖,还算蛮强大,就是性能开销较大,要解析很多文件脚本,给DOM添加大量的 ...

  4. 怎样在github上协同开发

    How to co-work wither parter via github. Github协同开发情景模拟 Github不仅有很多开源的项目可以参考,同样也是协同开发的最佳工具,接下来的就模拟一下 ...

  5. (水题)洛谷 - P1014 - Cantor表

    https://www.luogu.org/problemnew/show/P1014 很显然同一对角线的和是相等的.我们求出前缀和然后二分. 最后注意奇偶的顺序是相反的. #include<b ...

  6. 51nod1270 【dp】

    思路: dp[i][0]代表第i个位置取1,dp[i][1]代表第i个位置取b[i]. #include <bits/stdc++.h> using namespace std; type ...

  7. Codeforces702C【二分】

    题意: 给你几个城市,蜂窝塔量: 给出城市和塔的坐标可以重叠,非递减的方式给出: 输出最小的r,以至于所有的城市能被覆盖到: 思路: 目的就是要使每个城市覆盖到,那我对每个城市找离最近塔的距离,然后在 ...

  8. C#批量插入Sybase数据库,Anywhere 8

    数据库版本是Adaptive Server Anywhere 8 1.添加引用,程序集 iAnywhere.Data.AsaClient.这个和SQLServer的System.Data.SqlCli ...

  9. the little schemer 笔记(10)

    第十章 What Is  the Value of All of This? entry条目 是由list表组成的 pair 对,pair 对的第一个list表是集合 set.另外,两个list表的长 ...

  10. background-size属性

    background-size:属性有 auto:length :百分比 length 如:10px 20px 固定的 或者是写成一个 ,10px  另外一个就默认为 auto; 写成百分比的形式 是 ...