一、Spring Ioc容器详解(1)    20131105
1.一切都是Bean
Bean可是一个字符串或者是数字,一般是一些业务组件.
粒度一般比较粗.
2.Bean的名称
xml配置文件中,id属性可以检测是否唯一。name是可以重复的。
一个bean的实现可以有多个名字,别名
<alias name="p3" alias="p4"/> 3.Spring 容器的初始化过程(不同的实现有不同的过程)
A、根据加载的配置文件信息注解Bean的信息(BeanDefinition)到Bean工厂
B、根据得到的Beandefintion对象来确定要不要初始化一些Bean
C、处理依赖注入(根据BeanDefintion 中有关依赖注入的信息)
D、客户端通过容器来查询业务组件
E、当容器关闭的时候,销毁 4.Bean工厂及应用上下文
A、BeanFactory--Spring最基本的容器接口
getBean(String)
getBean(String, Class<T>) ==获得指定类型的bean,如果符合条件的
所谓单例,是说容器只需要创建一次
containsBean(String) isSingleton(String) boolean isPrototype(String name) B、ApplicationContext--应用层面容器(提供访问环境资源信息的相关方法) ApplicationContext getParent();表现了一种树状结构。 applicationContext会在初始化时自动加载单例Bean
而beanFactory只有在用的时候才会初始化 5.三种实例化bean的方式
A.通过构造函数
默认的无参的构造函数 通过带一个参数的进行初始化
public Person(String name) {
super();
System.out.println("初始化Person!传进来姓名参数!");
this.name = name;
} B 使用静态工厂 <!-- 使用静态工厂方法 进行初始化 -->
<bean id="p3" class="com.lspring.springIoc.PersonFactory"
factory-method="createPerson"></bean>
<!-- 使用带参数的静态工厂方法创建bean -->
<bean id="p4" class="com.lspring.springIoc.PersonFactory"
factory-method="createPerson">
<constructor-arg value="五笔输入法"></constructor-arg>
</bean> public static Person createPerson(){
System.out.println("通过静态工厂实例化bean!");
return new Person();
} public static Person createPerson(String name){
System.out.println("通过静态工厂实例化bean!"+name);
return new Person(name);
} C 使用动态工厂 <!-- 使用动态工厂方法 进行初始化 -->
<!-- 先定义一个工厂 -->
<bean id="factoryBean" class="com.lspring.springIoc.PersonFactoryFamily">
<property name="family" value="王"></property>
</bean>
<bean id="p5" factory-bean="factoryBean" factory-method="createPerson">
<constructor-arg value="小二"></constructor-arg>
</bean>
<bean id="p6" factory-bean="factoryBean" factory-method="createPerson">
</bean> private String family; public String getFamily() {
return family;
} public void setFamily(String family) {
this.family = family;
} public Person createPerson(){
System.out.println("通过静态工厂实例化bean!");
return new Person(family+"");
} public Person createPerson(String name){
System.out.println("通过静态工厂实例化bean!"+name);
return new Person(family+":"+name);
} 6.Bean的作用域
spring3以后默认提供了5种
(1).singleton(默认值)
在每个spring ioc容器中一个bean定义只有一个对象实例(共享)
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-int='true'来延迟初始化bean,这时候,只有第一次获取bean才会初始化bean.如:
<bean id="xxxx" class="com.lspring..." lazy-init="true"/>
如果相对所有的bean都应用延迟初始化,可以在根节点beans设置default-lazy-init="true",如下:《beans default-lazy-init="true"..》 (2).prototype
允许bean可以被多次实例化(使用一次就创建一个实例 )
(3).reuqest
(4).session
(5).global session(Portlet规范将portlet定义为一种基于java技术的webx组件,由处理请求和生成动动态内容的portlet容器管理") A.singleton--单例 一个类只有一次初始化,因为第一次加载时就放入内存中去了。。 B.prototype--多例(原型),每次从要从容器取该Bean时,均重新创建一次。
<constructor-arg value="fff"></constructor-arg>
C.request--生存期在一次请求内有效,必须在web应用中才能用. D.session---生存期在一次会话内有效,必须在web应用中才能用.购物车bean. E.global session,portlet环境应用中才有效 7.指定bean的初始化方法和销毁方法
<bean id="personDao" class="com.lspring..." init-method="init" destroy-method="close" >
<property name="timeout" value="10000"></property></bean>
8.加载多个配置文件
<bean>
<import reource="bean-scope.xml"/>
<import resource="beans-init.xml"/>
</beans>
五。Spring ioc容器详解(2)
1.spring容器中的依赖注入概述
类型:使用构造器注入/使用属性setter方法注入/使用field注入(用于注解方式)
<!-- 使用setter注入 -->
public Integer getAge() {
return age;
}
<bean id="p1" class="com.lspring.springIoc.Person">
<property name="name" value="TOM"></property>
<property name="age" value ="18"></property>
</bean> <!-- 构造子注入 -->
public Person(String name, Integer age) {
super();
System.out.println("构造子注入!");
this.name = name;
this.age = age;
}
<bean id="p2" class="com.lspring.springIoc.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="20"> </constructor-arg>
</bean>
<!-- 对于 一些有歧义的可能有几个构造函数与下面的参数匹配时, 可以通过指定type进行区分 -->
<bean id="p3" class="com.lspring.springIoc.Person">
<constructor-arg index="1" type="java.lang.Integer"
value="20">
</constructor-arg>
<constructor-arg index="0" value="33"></constructor-arg>
</bean>
C、字段注入
需要在配置文件在配置context命名空间,指定容器去扫描Bean中的注解
private String name;
private Integer age;
@Autowired
private String Mytel; <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>
<!-- 启动注解扫描功能,有的配置是在注解-->
<context:annotation-config></context:annotation-config>
<!-- 配置一个字段注入 -->
<bean id="Mytel" class="java.lang.String">
<constructor-arg value="11113333"></constructor-arg> 2.装配的类型:手动装配还是自动装配

[Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。的更多相关文章

  1. Spring学习笔记--环境搭建和初步理解IOC

    Spring框架是一个轻量级的框架,不依赖容器就能够运行,像重量级的框架EJB框架就必须运行在JBoss等支持EJB的容器中,核心思想是IOC,AOP,Spring能够协同Struts,hiberna ...

  2. qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

    原博主博客地址:http://blog.csdn.net/qq21497936本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516 ...

  3. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  4. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

  5. Spring学习笔记2:Spring HelloWorld

    1:IntelliJ新建Maven工程 2:pom文件加入Spring依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  6. [Spring学习笔记 5 ] Spring AOP 详解1

    知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...

  7. Spring学习笔记:Spring概述,第一个IoC依赖注入案例

    一.Spring的优点 企业及系统: 1.大规模:用户数量多.数据规模大.功能众多 2.性能和安全要求高 3.业务复杂 4.灵活应变 Java技术:高入侵式依赖EJB技术框架-->Spring框 ...

  8. 1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)

    1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/    选择需要下载的版本    ...

  9. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

随机推荐

  1. 利用blob对象实现大文件分片上传

    首先说分片上传,我们在进行文件上传的时候,因为服务器的限制,会限制每一次上传到服务器的文件大小不会很大,这个时候我们就需要把一个需要上传的文件进行切割,然后分别进行上传到服务器. 假如需要做到这一步, ...

  2. 命令行修改mysql密码和远程访问

    http://jingyan.baidu.com/article/a3a3f8118cea488da2eb8a0a.html

  3. 大数据开发实战:MapReduce内部原理实践

    下面结合具体的例子详述MapReduce的工作原理和过程. 以统计一个大文件中各个单词的出现次数为例来讲述,假设本文用到输入文件有以下两个: 文件1: big data offline data on ...

  4. 个基于TensorFlow的简单故事生成案例:带你了解LSTM

    https://medium.com/towards-data-science/lstm-by-example-using-tensorflow-feb0c1968537 在深度学习中,循环神经网络( ...

  5. HTML/CSS-返回到上一页

    <a class="back_btn" href="javascript:window.history.go(-1)">< 返回</a& ...

  6. IIS 7.5: HOW TO ENABLE TLS 1.1 AND TLS 1.2

    In IIS 7.5, which is installed on Windows 2008 R2 servers, only SSL 3.0 and TLS 1.0 are enabled for ...

  7. CHtmlEditCtrl (3): More HTML Editor Options

    In this version of our HTML Editor, we'll create a floating source view/edit window and we'll implem ...

  8. String escape/unescape into XML

    Is there any C# function which could be used to escape and un-escape a string, which could be used t ...

  9. C118+OSMCOMBB嗅探短信

    ubuntu系统:12.04.4, 下载地址:http://cdimage.ubuntu.com/releases/12.04.4/release/ 编译环境下载 : http://pan.baidu ...

  10. ArcGIS鼠标滚轮方向之注册表篇

    ArcMap鼠标滚轮方向的设置是记录在注册表信息中,那么我们做一个简单的测试.先打开注册表,将ReverseMouseWheel删除,再打开ArcMap进行鼠标滚轮方向设置. 设置完成后,刷新注册表, ...