`

Spring IOC(Inversion of control)/DI(Dependency Injection)

阅读更多

简单的理解,根据开发者配置属性,由Spring管理所有的Bean,容器向应用提供对应依赖的对象。


1、构造器注入

(1)默认构造方法或带参数构造方法(包含对象引用)

如果没有public公共构造方法的类使用factory-method="getInstance"

2、Setter注入

3、装配bean,与作用域
      可以装配list set map,
      Spring Bean默认都是单例,当然用户可以对该Bean的配置文件进行配置,作用域规则如下.

作用域                            规则

singleton                       Spring容器中,一个Bean定义只有一个对象实例

prototype                        每次调用都创建一个实例

request                            每次Http请求,每个Bean定义对应一个实例,仅在Web的Spring容器中有效

session                             在一个HttpSession中,每个Bean对应一个实例,仅在Web的Spring容器中有效

global-session              一个全局HttpSession中,每个Bean对应一个实例,仅在Web的Spring容器中有效

 

 

4、Bean的生命周期

 Spring里的源码分析

1)实例化

2)填充属性

3)调用BeanNameAware的SetBeanName方法

4)BeanFactory的SetBeanFactory方法

5)调用ApplicationContextAware的setApplicationContext()

6)BeanPostProcessore的预初始化方法

7)InitializingBean的afterPropertiesSet()方法

8)调用定制初始化方法

9)BeanPostProessors的后初始化方法

10)Bean成功初始化-容器关闭-调用DisposableBean 的destory()方法-调用定制的销毁方法。

 

(1)初始化和销毁

通过配置文件

 

通过接口实现

 

 

分享到:
评论

相关推荐

    spring框架约束步骤及教程

    spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入...

    spring jar包

    3.spring-beans:基础jar包,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring...

    springioc和spring aop

    控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找...

    登陆案例jar包

    //包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。 spring-core-4.2.4.RELEASE.jar //这个jar 文件包含Spring 框架基本的核心工具类。Spring ...

    spring-beans-4.0.0.RELEASE.jar

    spring-beans-4.0.0.RELEASE.jar这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI...

    Spring Security、Spring Social 、Spring Security OAuth

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    spring_note.rar_inversion_spring concept

    inversion of control dependency injection AOP的概念与好处 Spring简介 Spring应用IOC/DI(重要) xml annotation Spring应用AOP(重要) xml annotation Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要)...

    SpringSecutiry实现认证授权功能,整合SpringBoot

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    Spring——IOC(控制反转)与DI(依赖注入).docx

    控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。在没有IOC的程序中,我们使用面向对象编程,对象的创建于对象间的依赖完全硬编码在程序中,对象的创建有程序自己控制;...

    SpringSecurity.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    spring-struts1-strust2-hibernate 核心包介绍

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行 Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC /DI支持,引入spring-core.jar及...

    spring jar 包详解

    (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,...

    apache-tomcat-9.0.17.rar

    This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their ...

    Pro Spring 3

    What Inversion of Control (IoC) and dependency injection (DI) are Aspect-oriented programming techniques with Spring, and why they’re important Data access and persistence using Spring and Hibernate,...

    SpringSecurity实战教程.txt

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    Spring Security

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    spring4.3.9相关jar包

    spring-beans.jar(必须):这 个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI ...

    spring-security-4.2.5.RELEASE-source.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    最新最全的spring开发包

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及...

    SpringSecyrity入门demo

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

Global site tag (gtag.js) - Google Analytics