三种实例化bean的方式
在spring中有三中实例化bean的方式:
一、使用构造器实例化;(90%通常使用的一个方法)
二、使用静态工厂方法实例化;
三、使用实例化工厂方法实例化。
每种实例化所采用的配置是不一样的:
一、使用构造器实例化;
这种实例化的方式可能在我们平时的开发中用到的是最多的,因为在xml文件中配置简单并且也不需要额外的工厂类来实现。
- <!--applicationContext.xml配置:-->
- <bean id="personService" class="cn.mytest.service.impl.PersonServiceBean"></bean>
id是对象的名称,class是要实例化的类,然后再通过正常的方式进调用实例化的类即可,比如:
- public void instanceSpring(){
- //加载spring配置文件
- ApplicationContext ac = new ClassPathXmlApplicationContext(
- new String[]{
- "/conf/applicationContext.xml"
- });
- //调用getBean方法取得被实例化的对象。
- PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService");
- psb.save();
- }
采用这种实例化方式要注意的是:要实例化的类中如果有构造器的话,一定要有一个无参的构造器。
二、使用静态工厂方法实例化;
根据这个中实例化方法的名称就可以知道要想通过这种方式进行实例化就要具备两个条件:(一)、要有工厂类及其工厂方法;(二)、工厂方法是静态的。OK,知道这两点就好办了,首先创建工程类及其静态方法:
- package cn.mytest.service.impl;
- /**
- *创建工厂类
- *
- */
- public class PersonServiceFactory {
- //创建静态方法
- public static PersonServiceBean createPersonServiceBean(){
- //返回实例化的类的对象
- return new PersonServiceBean();
- }
- }
然后再去配置spring配置文件,配置的方法和上面有点不同,这里也是关键所在
- <!--applicationContext.xml配置:-->
- <bean id="personService1" class="cn.mytest.service.impl.PersonServiceFactory" factory-method="createPersonServiceBean"></bean>
id是实例化的对象的名称,class是工厂类,也就实现实例化类的静态方法所属的类,factory-method是实现实例化类的静态方法。
然后按照正常的调用方法去调用即可:
- public void instanceSpring(){
- //加载spring配置文件
- ApplicationContext ac = new ClassPathXmlApplicationContext(
- new String[]{
- "/conf/applicationContext.xml"
- });
- //调用getBean方法取得被实例化的对象。
- PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService1");
- psb.save();
- }
三、使用实例化工厂方法实例化。
这个方法和上面的方法不同之处在与使用该实例化方式工厂方法不需要是静态的,但是在spring的配置文件中需要配置更多的内容,,首先创建工厂类及工厂方法:
- package cn.mytest.service.impl;
- /**
- *创建工厂类
- *
- */
- public class PersonServiceFactory {
- //创建静态方法
- public PersonServiceBean createPersonServiceBean1(){
- //返回实例化的类的对象
- return new PersonServiceBean();
- }
- }
然后再去配置spring配置文件,配置的方法和上面有点不同,这里也是关键所在
- <!--applicationContext.xml配置:-->
- <bean id="personServiceFactory" class="cn.mytest.service.impl.PersonServiceFactory"></bean>
- <bean id="personService2" factory-bean="personServiceFactory" factory-method="createPersonServiceBean1"></bean>
这里需要配置两个bean,第一个bean使用的构造器方法实例化工厂类,第二个bean中的id是实例化对象的名称,factory-bean对应的被实例化的工厂类的对象名称,也就是第一个bean的id,factory-method是非静态工厂方法。
然后按照正常的调用方法去调用即可:
- public void instanceSpring(){
- //加载spring配置文件
- ApplicationContext ac = new ClassPathXmlApplicationContext(
- new String[]{
- "/conf/applicationContext.xml"
- });
- //调用getBean方法取得被实例化的对象。
- PersonServiceBean psb = (PersonServiceBean) ac.getBean("personService2");
- psb.save();
- }
三种实例化bean的方式的更多相关文章
- (转)Spring的三种实例化Bean的方式
http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...
- spring三种实例化bean的方式
1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...
- Spring第四弹—–Spring的三种实例化bean的方式
1.使用类构造器实例化 1 <bean id=“orderService" class="cn.itcast.OrderServiceBean"/> 2. ...
- Spring中四种实例化bean的方式
本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- Spring三种实例化Bean的方法
1.实例化bean的三种方法:(1) 构造器<!-- 体验1 --><bean id="personService" class="com.persia ...
- Spring入门学习(二)三种实例化bean的方法
前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...
- 三种定义bean的方式
方法一:基于XML的bean定义(需要提供setter方法) 1.首先编写student.java和teacher.java两个类 Student.java: public class Student ...
- 三种实例化委托的方式(C# 编程指南)
1.定义的委托和方法 delegate void TestDelegate(string s); static void M(string s) { Console.WriteLine(s); } 2 ...
随机推荐
- C/C++的一些备忘
今天使用source insight阅读videoserver源码,有一些符号ctrl+左键点击显示找不到,先是rebuild工程和同步,没有效果,然后Options->Preferences- ...
- [STL][C++]MAP
参考链接:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html map头文件 #include <map> map添加数据: map&l ...
- python :生产者和消费者模型 即简单的协程
def consumer(name): print('%s开始准备吃包子了' %name) while True: baozi=yield print('[%s]包子来了,被[%s]吃了' %(bao ...
- Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24867 Accepted: 12958 Descr ...
- 关于接收json以及使用json
Common: FileIO.cs using System; using System.Collections.Generic; //using System.Linq; using System. ...
- view class source code with JAD plugin in Eclipse
The default class viewer doesn't decompile the class file so you cannot open and check the source co ...
- 2016年10月12日 星期三 --出埃及记 Exodus 18:23
2016年10月12日 星期三 --出埃及记 Exodus 18:23 If you do this and God so commands, you will be able to stand th ...
- SSH配置私钥登陆服务器
前言 本文基于实际Linux管理工作,实例讲解工作中使用ssh证书登录的实际流程,讲解ssh证书登录的配置原理,基于配置原理,解决实际工作中,windows下使用SecureCRT证书登录的各种问题, ...
- iOS开发debug跟release版本屏蔽NSLog方法
1.在***-Prefix.pch里面添加 #ifndef __OPTIMIZE__ # define NSLog(...) NSLog(__VA_ARGS__) #else # define NSL ...
- Python3基础 三元表达式实例
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...