从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. C# 模拟浏览器请求

    public string getHtml(string Url, string type = "UTF-8")        {            try           ...

  2. Jenkins插件开发

    一.环境配置 不赘述,直接看wiki:https://wiki.jenkins.io/display/JENKINS/Extend+Jenkins 二.内容说明 1.插件代码结构 src/main/j ...

  3. Generic(泛型)

    什么是泛型:"通过参数化类型来实现在同一份代码上操作多种数据类型.利用"参数化类型"将类型抽象化,从而实现灵活的复用". 简单来说泛型就是为了使一些代码能够重复 ...

  4. [leetcode-604-Design Compressed String Iterator]

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  5. USACO Dynamic Programming (1)

    首先看一下题目: Introduction Dynamic programming is a confusing name for a programming technique that drama ...

  6. chart.js使用常见问题

    Chart.js是一个简单.面向对象.为设计者和开发者准备的图表绘制工具库. 在使用过程中新手可能会遇到很多问题导致图标无法显示.下面我们来看一下在使用过程中可能会遇到的问题. 刚开始用chart.j ...

  7. HybridApp Exception

    HybridApp Exception [创建安卓虚拟机失败]CPU acceleration status:HAXM must be updated(version 1.1.1<6.0.1) ...

  8. 分享一款简单好用的HTML拼接工具

    今天分享一款很好用的字符串拼接工具,在前端开发中,经常需要我们去手动拼接HTML代码,如果你经常这么做,那么肯定会因为单双引号的问题弄得焦头烂额.有了这个拼接工具,妈妈再也不用担心我拼不好html代码 ...

  9. JSON详细总结

    /** * Created by fa on 2016/3/15. */ var data = { name:"hello", children:[{ name:"chi ...

  10. jqueryEasyUI列表

    背景 因为学习大数据开发这段时间,同时也学习java的一些知识.利用了近五个月的时间来投入学习,当然我选择了一个机构,因为已经做了四年多的开发,所以即使不是做的java但是java还是了解的,这段时间 ...