一.  IOC容器配置

  1. 一些概念

  (1)IOC容器

  定义:具有管理对象和管理对象之间的依赖关系的容器。

  作用:应用程序无需自己创建对象,对象由IOC容器创建并组装。BeanFactory是IOC容器的核心。

  流程:IOC容器根据配置文件,读取配置元数据,通过元数据库对程序中的各个对象进行实例化和装配。Spring与配置文件完全解耦,可以使用其他任何方式进行配置元数据,比如注解、基于java文件、基于属性文件的配置都可以。

  

  (2)Bean:IOC容器管理的应用程序的对象我们称它为bean。

  (3)IOC容器核心接口:org.springframework.beans.BeanFactory、org.springframework.context.ApplicationContext

  BeanFactory提供了IOC容器的最基本功能,ApplicationContext是BeanFactory的扩展,添加了更多的企业级功能的支持。

  XmlBeanFactory:BeanFactory的实现,可以从classpath或者文件系统获取资源,在Spring 4.0中已经不建议使用,直接使用applicationContext的实现类。

  ClassPathXmlApplicationContext、FileSystemXmlApplicationContext是ApplicationContext的实现,分别为从classpath、文件系统获取配置文件。

  例子:

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");

  

  2. Bean的配置

  (1)Spring IOC容器的目的就是管理bean,这些bean将根据配置文件中bean的定义进行创建,bean定义在容器内部由BeanDefintion对象表示,主要包含以下信息:

  ●全限定类名(FQN):用于定义Bean的实现类;(通过构造器和静态工厂方法进行实例化,必需属性。)

  ●Bean行为定义:这些定义了Bean在容器中的行为;包括作用域(单例、原型创建)、是否惰性初始化及生命周期等;

  ●Bean创建方式定义:说明是通过构造器还是工厂方法创建Bean;

  ●Bean之间关系定义:即对其他bean的引用,也就是依赖关系定义,这些引用bean也可以称之为同事bean 或依赖bean,也就是依赖注入。

  (2)Bean的命名:

    <!-- 指定id,id在IOC容器中唯一-->
<bean id="helloServiceById" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdImpl"/> <!-- 不指定id,只配置必须的全限定类名:用于定义Bean的实现类,由IoC容器为其生成一个标识 -->
<bean class="self.springmvc.beanConfig.service.impl.HelloServiceByTypeImpl"/> <!-- 指定name,name在IOC容器中唯一-->
<bean name="helloServiceByName" class="self.springmvc.beanConfig.service.impl.HelloServiceByNameImpl"/> <!-- 指定id和name,那么id在IOC容器中唯一,name作为别名,IOC会消除name和id的冲突。-->
<bean name="helloServiceByIdName2" id="helloServiceByIdName2" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdNameImpl"/>

   <!-- 多个别名-->
<bean name="helloServiceByIdName,helloServiceByIdName1,helloServiceByIdName2,helloServiceByIdName3" id="helloServiceByIdName" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdNameImpl"/>

  <!-- 定义别名-->
<alias name="helloServiceByIdName" alias="helloServiceByIdName4"></alias>
<alias name="helloServiceByIdName" alias="helloServiceByIdName5"></alias>

  (3)Bean的实例化

  实例化:使用构造器(无参构造器和参数构造器)、静态工厂方法、实例工厂方法。

 <!-- 无参构造函数实例化,如果注释掉该实现类的无参构造函数,那么就会编译报错。 -->
<bean id="bIHelloService1" class="self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl"/>
<!-- 参数构造函数实例化 -->
<bean id="bIHelloService2" class="self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl">
<constructor-arg index="0" value="Welcome to XiaMen"/>
</bean>
<!-- 使用静态工厂方法实例化 -->
<bean id="bIHelloService3" class="self.springmvc.beanInstantiaton.service.HelloServiceStaticFactory" factory-method="getBean"/>
<bean id="bIHelloService4" class="self.springmvc.beanInstantiaton.service.HelloServiceStaticFactory" factory-method="getBean">
<constructor-arg index="0" value="Welcome to XiaMen4"/>
</bean>
<!-- 使用实例工厂方法实例化-->
<!-- 定义实例工厂Bean-->
<bean id="helloServiceFactoryBean" class="self.springmvc.beanInstantiaton.service.HelloServiceFactory"/>
<!-- 使用实例工厂Bean实例化Bean-->
<bean id="bIHelloService5" factory-bean="helloServiceFactoryBean" factory-method="getBean"/>
<bean id="bIHelloService6" factory-bean="helloServiceFactoryBean" factory-method="getBean">
<constructor-arg index="0" value="Welcome to XiaMen5"/>
</bean>

  静态工厂类与实例工厂类:

package self.springmvc.beanInstantiaton.service;

import self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl;
import self.springmvc.beanInstantiaton.service.inter.HelloService; /**
* Description
*/
public class HelloServiceStaticFactory {
public static HelloService getBean(){
return new HelloServiceImpl();
}
public static HelloService getBean(String msg){
return new HelloServiceImpl(msg);
}
} package self.springmvc.beanInstantiaton.service; import self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl;
import self.springmvc.beanInstantiaton.service.inter.HelloService; /**
* Description
*/
public class HelloServiceFactory {
public HelloService getBean(){
return new HelloServiceImpl();
}
public HelloService getBean(String msg){
return new HelloServiceImpl(msg);
}
}

  

    

Spring学习(4)IOC容器配置bean:定义与实例化的更多相关文章

  1. Spring技术内幕_IOC容器载入Bean定义资源文件

    转自:http://blog.csdn.net/chjttony/article/details/6259723 1.当spring的IoC容器将Bean定义的资源文件封装为Spring的Resour ...

  2. Spring基础——在 IOC 容器中 Bean 之间的关系

    一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...

  3. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  4. [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

  6. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  7. 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)

    很长一段时间关注在Java Web开发的方向上,提及到Jave Web开发就绕不开Spring全家桶系列,使用面向百度,谷歌的编程方法能够完成大部分的工作.但是这种不系统的了解总觉得自己的知识有所欠缺 ...

  8. IOC容器和Bean的配置

     IOC容器和Bean的配置   1        IOC和DI ①IOC(Inversion of Control):反转控制. 在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取 ...

  9. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用spring framework的IoC容器功能----->方法一:使用XML文件定义beans之间的依赖注入关系

    XML-based configuration metadata(使用XML文件定义beans之间的依赖注入关系) 第一部分 编程思路概述 step1,在XML文件中定义各个bean之间的依赖关系. ...

随机推荐

  1. RAC with asm on AIX, ORA-01114 error,with "gipcretAuthFail (22) " in ocssd.log

    I/O Errors in Alert log with ORA-29701, with "gipcWait failed with 16" in trace (文档 ID 149 ...

  2. ES6新特性6:模块Module

    本文摘自ECMAScript6入门,转载请注明出处. 一.Module简介 ES6的Class只是面向对象编程的语法糖,升级了ES5的构造函数的原型链继承的写法,并没有解决模块化问题.Module功能 ...

  3. 【转】如何在VMware上安装macOS Sierra 10.12

    本文主要介绍目前网络上比较流行的使用预安装镜像安装macOS 10.12的方法,并以9月20号发布的最新GM版本16A323为例. 安装方案 破解VMware 创建虚拟机,加载预安装镜像 初始化mac ...

  4. B. Fafa and the Gates

    http://codeforces.com/problemset/problem/935/B Two neighboring kingdoms decided to build a wall betw ...

  5. Oracle 索引 详解

    转载:http://www.2cto.com/database/201110/107271.html 一.索引介绍 1.1 索引的创建语法: CREATE UNIUQE | BITMAP INDEX ...

  6. 【js】按下enter键禁止表单自动提交

    //enter键盘事件 document.onkeypress=function(){ if(event.keyCode==13){ return false; } }

  7. C 和 Object-C中的 #ifdef #ifndef

    很多宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”.有时,希望当满足某条件时 ...

  8. mac 设置mysql开机自启动

    1.编辑一个mysql启动文件. 在终端里面输入: sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist 2.输入启动文件内容: <?xml ...

  9. MongoDB的聚合操作以及与Python的交互

    上一篇主要介绍了MongoDB的基本操作,包括创建.插入.保存.更新和查询等,链接为MongoDB基本操作. 在本文中主要介绍MongoDB的聚合以及与Python的交互. MongoDB聚合 什么是 ...

  10. 用NI的数据采集卡实现简单电子测试之5——压控振荡器的测试

    本文从本人的163博客搬迁至此. 为了展示连续信号采集的方法,以其外部触发采集功能.我用运算放大器实现了一个最简单的低频压控振荡器(VCO),作为USB-6009采集的信号源.在LabVIEW下编写的 ...