从bean.xml中<bean>标签内容可以看出bean其实是一个管理对象的东西,我们只需要修改xml配置文件,就可以改变对象之间的依赖关系,不需要去修改任何源代码。我觉得学习好spring这个框架,对于配置文件以及bean的实例化是了解springIoc的关键。

    spring IoC容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean。在Spring IoC容器中根据Bean定义创建Bean主要有以下几种方式:

一.使用构造器进行定义,上篇文章在介绍搭建简单环境的时候,用的方法就是构造器的方法。

  这里面又包括了空构造器和有参构造器:我们看如下代码:(相关代码大家可以看我上一篇文章)

    

package com.spring.service.impl;
import com.spring.service.GreetingService;
public class GreetingServiceImpl implements GreetingService {
private String greeting; public void setGreeting(String greeting) {
this.greeting = greeting;
}
public GreetingServiceImpl(){
  System.out.println("空构造法");
}
public GreetingServiceImpl(String greeting){
this.greeting=greeting;
} public void sayGreeting(){
System.out.println(greeting);
}

我们在这个java中有两个构造函数:无参和有参;对于这个区别,在相应的beans.xml中肯定也要有相应变化了。我们在beans.xml中加入一个bean来使用空构造,beans.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-3.0.xsd">
<bean id="greetingService" class="com.spring.service.impl.HelloStaticFactory" factory-method="newInstance">
<!-- property name="greeting" value="45631"/-->
<!-- collaborators and configuration for this bean go here -->
<constructor-arg index="0" value="458214"/>
</bean>
<bean id="bean2" class="com.spring.service.impl.GreetingServiceImpl"/>
<bean id="bean3" class="com.spring.service.impl.GreetingServiceImpl">
<property name="greeting" value="bean3"/>
</bean>
</beans>

大家看bean2和bean3,其中bean2对应的是空构造法,bean3对应的事有参构造法(上面的greetingService后面会用到,先不用看)。对于有参构造法在传递参数是可以用  

    <property name="greeting" value="bean3"/>
     <constructor-arg index="0" value="458214"/>
然后在测试代码中测试:改变上篇文章中的getBean函数的参数就可以了。 二.静态构造法:因为上面中我已经把beans.xml中已经加入了这个bean,所以下面只给出HelloStaticFactory.java代码:
  
package com.spring.service.impl;
import com.spring.service.GreetingService;
public class HelloStaticFactory{
public static GreetingService newInstance(String Greeting){
return new GreetingServiceImpl(Greeting);
}
}

大家注意在beans.xml中,使用静态构造法,在<bean>标签中要插入factory-method属性。

三.实例工厂化方法实例bean:  HelloInstanceFactory.java  把上段代码中的static给去掉就行。如果不去掉因为是static,所以实例化工厂时会出错

package com.spring.service.impl;

import com.spring.service.GreetingService;

public class HelloInstanceFactory{
public GreetingService newInstance(String greeting){
return new GreetingServiceImpl(greeting);
}
}

下面我给出所有的xml代码,记得在尝试各种方法的时候要把无参的bean给注释起来 不然会解析这个bean,打印的结果就是两行了

<?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-3.0.xsd">
<!-- 静态构造 -->
<bean id="greetingService" class="com.spring.service.impl.HelloStaticFactory" factory-method="newInstance">
<!-- property name="greeting" value="45631"/-->
<!-- collaborators and configuration for this bean go here -->
<constructor-arg index="0" value="458214"/>
</bean>
<!-- 无参构造法 -->
<bean id="bean2" class="com.spring.service.impl.GreetingServiceImpl"/>
<!-- 有参构造法 -->
<bean id="bean3" class="com.spring.service.impl.GreetingServiceImpl">
<constructor-arg index="0" value="hello"/>
</bean>
<!-- 实例工厂构造法 -->
<!-- 定义实例工厂bean -->
<bean id="beanInstanceFactory"
class="com.spring.service.impl.HelloStaticFactory"/>
<!-- 使用实例化工厂bean创建bean -->
<bean id="bean4" factory-bean="beanInstanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello Spring!"/>
</bean>
</beans>

四、三种方式的理解和应用:

    其实这三种实例化bean还是相当于通过new一个对象执行某些函数,就像java一样。构造器方法和直接java实例化一个对象完全是一个道理;而静态工厂方法,就有点像我们在一个类中定义的一个静态函数式一样的,我们可以直接调用这个静态函数(内存中只有一份),而调用过程就是通过factory-method。下面在具体说说实例化工厂方法:

  以构造器方法为例,我们可以有多个构造方法,但是在调用各个方法时,我们每次其实都相当于重新new了一个对象,如果我们想调用一个类中不同的方法时,这样做就很浪费内存了,我们完全只要需要创建一个对象就可以了啊,所以用实例化工厂方法就解决了这个问题。看这段代码(我表述的可能有些模糊 ,但是我觉得看了代码应该就秒懂了):

<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/> <bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() {
return clientService;
} public AccountService createAccountServiceInstance() {
return accountService;
}
}

实例化bean的更多相关文章

  1. Spring实例化Bean的三种方式及Bean的类型

    1.使用类构造器实例化  [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...

  2. 三种实例化bean的方式

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

  3. (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器

    上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...

  4. (spring-第9回【IoC基础篇】)BeanFactoryPostProcessor,实例化Bean之前的第二大利器

    继承结构图如上.在加载XML,注册bean definition之后,在实例化bean definition之前,必要的时候要用到BeanFactoryPostProcessor.它负责把XML中有些 ...

  5. (spring-第6回【IoC基础篇】)BeanDefinition——实例化Bean之前的第一大利器。

    上节讲了Bean实例化的内部机制,这里再复述一遍: ResourceLoader从系统中加载XML配置信息,并由Resource来表示. BeanDefinitionReader从Resource中读 ...

  6. (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制

    前面讲过,spring的生命周期为:实例化前奏-->实例化-->实例化后期-->初始化前期-->初始化-->初始化后期-->bean的具体调用-->销毁前-- ...

  7. spring实例化bean的方式

    1.使用类构造器实现实例化(bean的自身构造器) <bean id = "orderService" class="cn.itcast.OrderServiceB ...

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

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

  9. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  10. Spring实例化bean的三种方法

    1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...

随机推荐

  1. Bash函数

    一.什么是Bash函数 Bash不支持goto语句,可以用function实现程序流程跳转.当前shell中一组组织在一起并被命名的命令.比脚本的效率高,一旦定义,就成为shell内存的一部分,可以随 ...

  2. css的各种选择器

    一.基本选择器 1. * 通用元素选择器,匹配任何元素 2. E 标签选择器,匹配所有使用E标签的元素 3. .info class选择器,匹配所有class属性中包含info的元素 4. #foot ...

  3. 神奇的 conic-gradient 圆锥渐变

    感谢 LeaVerou 大神,让我们可以提前使用上这么美妙的属性. conic-gradient 是个什么?说到 conic-gradient ,就不得不提的它的另外两个兄弟: linear-grad ...

  4. VS2012打开项目 提示Asp.net4.5未在web服务器上注册

    在用vs2012代开项目时,没回都显示Asp.net4.5未在web服务器上注册,最后是由于没有下载一个补丁的原因,只需要下载安装补丁 VS11-KB3002339.exe ,下载地址:https:/ ...

  5. GridView七十二绝技-大全(收藏版)(转至别人博客)

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  6. 一张图告诉你angular2所有知识点

    忙活了半年,从angular2.0到现在angular4.2.从没AOT到有AOT.我想说,angular2的学习曲线真的有点陡峭.只能说,angular2是一个比较完整的框架,框架就是这样,一大堆条 ...

  7. vue.js+UEditor集成 [前后端分离项目]

    首先,谈下这篇文章中的前后端所涉及到的技术框架内容. 虽然是后端的管理项目,但整体项目,是采用前后端分离的方式完成,这样做的目的也是产品化的需求: 前端,vue+vuex+vue router+web ...

  8. C# Using的用法

    C#里面Using有两种用法: 1.作为指令. using+命名空间,导入其他命名空间中定义的类型,这样可以在程序中直接用命名空间中的类型,不必指定命名空间: 命名空间是.NET程序在逻辑上的组织结构 ...

  9. Java+Tomcat + Idea + Jrebel 实现热部署

    1. 首先安装idea的jrebel插件, jrebel是收费的,所以要在网上下载验证码. 2. 安装好以后再setting 菜单能看到一个jrebel的菜单. 3.  4.其中需要选中frame失去 ...

  10. 8位基本定时器(TIM4)

    简介:该定时器由一个带可编程预分频器的8位自动重载的向上计数器所组成,它可以用来作为时基发生器,具有溢出中断功能. 主要功能: (1)8位向上计数的自动重载计数器: (2)3位可编程的预分配器(可在运 ...