一、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. vue组件的hover事件模拟、给第三方组件绑定事件不生效问题

    1.vue里面实现hover效果基本需要用事件模拟 <div @mouseover="overShow" @mouseout="outHide"> ...

  2. iOS runtime探究(三): 从runtime開始理解OC的属性property

    你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639303 本文主要解说runtime相关知识, ...

  3. variable_scope和name_scope差别

    先看代码:   #命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用       import tensorflow as tf       with t ...

  4. zookeeper基础知识整理

    http://blog.csdn.net/pelick/article/details/7269670 http://zookeeper.apache.org/doc/trunk/javaExampl ...

  5. viso-实现文本编辑

    选定主控件中所有的元素,然后选择[形状]-[组合] 然后右键[格式]-[行为] 按照如下设置,确定,保存就可以了

  6. c/c++ sizeof运算符详解以及对象大小

    原文:http://krystism.is-programmer.com/posts/41468.html 学过c的都知道sizeof运算符.不过还是需要注意以下几点.先从c的sizeof说起: 1. ...

  7. Flask调试

    1.AttributeError: 'NoneType' object has no attribute 'app' 原因:直接在py中调用视图函数,但没有上下文,导致出错 2.不转换html代码 { ...

  8. JavaScript代码不执行

    一天先后有两个同事问我为啥他的js代码出现了莫名其妙的问题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

  9. oauth2-server-php-docs 集成3

    Yii集成 对于Yii集成,请通过Filsh查看Yii OAuth2服务器资源库 CakePHP的 有关CakePHP中集成的这个库的示例,请参阅Qsoomro CakePHP OAuth2 Demo ...

  10. 用 CSS 实现元素垂直居中,有哪些好的方案?

    1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...