在spring中有三中实例化bean的方式:

一、使用构造器实例化;(90%通常使用的一个方法)

二、使用静态工厂方法实例化;

三、使用实例化工厂方法实例化。

每种实例化所采用的配置是不一样的:

一、使用构造器实例化;

这种实例化的方式可能在我们平时的开发中用到的是最多的,因为在xml文件中配置简单并且也不需要额外的工厂类来实现。

  1. <!--applicationContext.xml配置:-->
  2. <bean id="personService" class="cn.mytest.service.impl.PersonServiceBean"></bean>

id是对象的名称,class是要实例化的类,然后再通过正常的方式进调用实例化的类即可,比如:

  1. public void instanceSpring(){
  2. //加载spring配置文件
  3. ApplicationContext ac = new ClassPathXmlApplicationContext(
  4. new String[]{
  5. "/conf/applicationContext.xml"
  6. });
  7. //调用getBean方法取得被实例化的对象。
  8. PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService");
  9. psb.save();
  10. }

采用这种实例化方式要注意的是:要实例化的类中如果有构造器的话,一定要有一个无参的构造器。

二、使用静态工厂方法实例化;

根据这个中实例化方法的名称就可以知道要想通过这种方式进行实例化就要具备两个条件:(一)、要有工厂类及其工厂方法;(二)、工厂方法是静态的。OK,知道这两点就好办了,首先创建工程类及其静态方法:

  1. package cn.mytest.service.impl;
  2. /**
  3. *创建工厂类
  4. *
  5. */
  6. public class PersonServiceFactory {
  7. //创建静态方法
  8. public static PersonServiceBean createPersonServiceBean(){
  9. //返回实例化的类的对象
  10. return new PersonServiceBean();
  11. }
  12. }

然后再去配置spring配置文件,配置的方法和上面有点不同,这里也是关键所在

  1. <!--applicationContext.xml配置:-->
  2. <bean id="personService1" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"></bean>

id是实例化的对象的名称,class是工厂类,也就实现实例化类的静态方法所属的类,factory-method是实现实例化类的静态方法。

然后按照正常的调用方法去调用即可:

  1. public void instanceSpring(){
  2. //加载spring配置文件
  3. ApplicationContext ac = new ClassPathXmlApplicationContext(
  4. new String[]{
  5. "/conf/applicationContext.xml"
  6. });
  7. //调用getBean方法取得被实例化的对象。
  8. PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService1");
  9. psb.save();
  10. }

三、使用实例化工厂方法实例化。

这个方法和上面的方法不同之处在与使用该实例化方式工厂方法不需要是静态的,但是在spring的配置文件中需要配置更多的内容,,首先创建工厂类及工厂方法:

  1. package cn.mytest.service.impl;
  2. /**
  3. *创建工厂类
  4. *
  5. */
  6. public class PersonServiceFactory {
  7. //创建静态方法
  8. public PersonServiceBean createPersonServiceBean1(){
  9. //返回实例化的类的对象
  10. return new PersonServiceBean();
  11. }
  12. }

然后再去配置spring配置文件,配置的方法和上面有点不同,这里也是关键所在

  1. <!--applicationContext.xml配置:-->
  2. <bean id="personServiceFactory" class="cn.mytest.service.impl.PersonServiceFactory"></bean>
  3. <bean id="personService2" factory-bean="personServiceFactory" factory-method="createPersonServiceBean1"></bean>

这里需要配置两个bean,第一个bean使用的构造器方法实例化工厂类,第二个bean中的id是实例化对象的名称,factory-bean对应的被实例化的工厂类的对象名称,也就是第一个bean的id,factory-method是非静态工厂方法。

然后按照正常的调用方法去调用即可:

  1. public void instanceSpring(){
  2. //加载spring配置文件
  3. ApplicationContext ac = new ClassPathXmlApplicationContext(
  4. new String[]{
  5. "/conf/applicationContext.xml"
  6. });
  7. //调用getBean方法取得被实例化的对象。
  8. PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService2");
  9. psb.save();
  10. }

三种实例化bean的方式的更多相关文章

  1. (转)Spring的三种实例化Bean的方式

    http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...

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

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

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

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

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

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

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

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

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

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

  7. Spring入门学习(二)三种实例化bean的方法

    前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...

  8. 三种定义bean的方式

    方法一:基于XML的bean定义(需要提供setter方法) 1.首先编写student.java和teacher.java两个类 Student.java: public class Student ...

  9. 三种实例化委托的方式(C# 编程指南)

    1.定义的委托和方法 delegate void TestDelegate(string s); static void M(string s) { Console.WriteLine(s); } 2 ...

随机推荐

  1. git 基本命令

    (命令总结内容来自 博客园  圣骑士Wind的博客) git init      在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git ...

  2. 【转】Cookie和Session的区别详解

    转载地址:http://www.phperzone.cn/portal.php?aid=541&mod=view 一.cookie机制和session机制的区别 具体来说cookie机制采用的 ...

  3. c# 客户端

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 2015-11-04 报表 (asp.net 部分)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Scrap_p.aspx.cs& ...

  5. 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏

    Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...

  6. 【TCP】超时与重传

    在TCP连接中假设发送方一开始便向网络发送多个报文段,直到达到接收方通告的窗口大小为止.当发送方和接收方处于同一个区域网段时,这种方式是可以的.但是如果发送方和接收方之间存在多个路由器和速率较慢的链路 ...

  7. HDU(1853),最小权匹配,KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...

  8. 灰色预测模型 c# 算法实现

     public class GrayModel    {        private double a0, a1, a2;        private int size;        priva ...

  9. Linux下统计出现次数最多的指定字段值

    假设桌面上有一个叫“data.txt”的文本,内容如下: {id='xxx' info='xxx' kk='xxx' target='111111' dd='xxx'}{id='xxx' info=' ...

  10. ContentProvider官方教程(5)ContentResolver插入、更新、删除 示例

    Inserting, Updating, and Deleting Data In the same way that you retrieve data from a provider, you a ...