从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. 学习maven的各种问题

    1. The container 'Maven Dependencies' references non existing library 解决方法,将eclipse中maven插件中“resolve ...

  2. 超好用的Redis管理及监控工具,使用后可大大提高你的工作效率!

    Redis做为现在web应用开发的黄金搭担组合,大量的被应用,广泛用于存储session信息,权限信息,交易作业等热数据.做为一名有10年以上JAVA开发经验的程序员,工作中项目也是广泛使用了Redi ...

  3. python+NLTK 自然语言学习处理:环境搭建

    首先在http://nltk.org/install.html去下载相关的程序.需要用到的有python,numpy,pandas, matplotlib. 当安装好所有的程序之后运行nltk.dow ...

  4. 简谈java 中的 继承和多态

    继承(extends) : 1:object 是所有类的父(基)类. 2:子类继承父类所有的内容除了(private修饰的和构造方法). 3:子类在手动创建构造方法时,必须调用父类构造方法. 4:在J ...

  5. HTML行内元素、块状元素、行内块状元素的区别

    HTML可以将元素分类方式分为行内元素.块状元素和行内块状元素三种.首先需要说明的是,这三者是可以互相转换的,使用display属性能够将三者任意转换: (1)display:inline;转换为行内 ...

  6. 【原】无脑操作:eclipse + maven搭建SSM框架

    网上看到一些Spring + Spring MVC + MyBatis框架的搭建教程,不是很详细或是时间久远了,自己动手整一个简单无脑的! 0.系统环境 1)Windows 10 企业版 2)JDK ...

  7. template might not exist or might not be accessible by any of the configured Template Resolvers

    距离上一篇文章已经很长时间了,最近太忙碌了,今天发布spring boot遇到一个问题,找了好久才找到解决办法,今天贴出来和大家一起分享下,首先看错误信息 HTTP Status 500 - Requ ...

  8. Chapter 3. Video Coding Concepts

    本章主要介绍一些有关视频编码的概念 时域模型(Temporal Model) 时域模型的作用是去除帧间冗余.如:将第二帧减去第一帧,得到的剩余信息,其能量会远小于第二帧本身. 基于块的运动估计和补偿 ...

  9. iOS- 解决iOS10 App启动时放大铺满App Icon的问题

    0.前言 iOS10 App启动时放大铺满App图标 iPad Application shows app icon as launch screen in iOS 10 如图,点击APP后APP图标 ...

  10. ionic搭建与基础

    ionic搭建与基础 一.环境搭建 安装 npm install -g cordova npm install -g ionic 创建 项目 ionic start MyProject blank i ...