一、什么是Spring IOC:

Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。

在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。

二、Spring中如何实现DI(依赖注入)

  1.构造器注入

<bean id="beanName" class="BeanClassName">
    <constructor-arg index="构造器中的位置" name="参数名" value="简单值" type="Java类型"/>
    <constructor-arg index="构造器中的位置" name="参数名" ref="OtherBeanName" type="Java类型"/>
</bean>

  2.Setter注入

<bean id="beanName" class="BeanClassName">
    <property name="字段名" value="简单值">
    <property name="字段名" ref="OtherBeanName">
</bean>

  3.接口注入:不常用,例如JNDI注入tomcat自带数据库连接池

三、XML配置Bean

  0.命名空间   

<?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.xsd">

  1.装配简易值和对象

<bean id="beanName" class="类的全限定名">
  <property name="字段名" value="简易值"/>
  <property name="字段名" ref="otherBeanName"/>
</bean>

  2.装配集合

<bean id="beanName" class="类的全限定名">
    <property name="字段名set">
      <set>
        <value>value</value>
        //<ref bean="otherBeanName">
      </set>
    </property>
    <property name="字段名list">
      <list>
        <value>value</value>        //<ref bean="otherBeanName">
      </list>
    </property>

    <property name="字段名props">
      <props>
        <prop key="name">value</prop>
      </props>
    </property>

    <property name="字段名map">             <map>                 <entry key="key" value="value"/>        //<entry key="key" value-ref="otherBeanName"/>        //<entry key-ref="otherBeanName" value-ref="otherBeanName"/>
      </map>
    </property>

    <property name="数组">      <array>        <value>value1</value>      </array>    </property>
</bean>

四、注解配置Bean

  1.@Component 配置Bean

    配置在类上,代表这个类会被SpringIOC扫描成一个Bean,注解中属性value表示为Bean的名字,如同XML中配置的id,不配置

    value的值时,默认是将类名的首字母小写。

    @Component   

    @Component("beanName")

  2.@ComponentScan  配置注解扫描器

    配置在类上,因为SpringIOC容器并不知道要扫描哪个类和包,需要定义一个SpringConfig类来告诉它,注解默认扫描该类所在包

    如果想扫描指定包,注解属性basePackages中,多个包,使用逗号隔开

    @ComponentScan

    @ComponentScan(basePackages={"包1","包2"})

  3.@Value   简单值注入

    配置在类的字段(属性)上,其中放入的是字符串,注入时会自动转换类型,同样可以使用EL表达式来注入资源属性文件中的值

    @Value("1")

    @Value("xxxx")

    @Value("${jdbc.database.driver}")

  4.@Autowired  自动装配属性

    配置在类的字段(属性)上、方法及构造函数的参数中完成自动装配的工作,可以通过 @Autowired的使用来消除 setter ,getter方法

    此注解是按照类型来注入的,当有多个bean同时实现一个接口时,可以使用@Primary和@Qualifier注解来消除歧义。

    @Autowired

  5.@Primary   优先注入

    配置在某个接口有多个实现类的某个实现类上,在其他Bean中使用@Autowired注入该接口类型的bean时,优先注入这个实现类的bean。

    和@Component一起使用,否则无效。

  6.@Qualifier  指定注入

    配置在类的字段(属性)上,和@Autowired一起使用,当@Autowired注入时,指定注入的bean,该注解的属性(value)指定Bean的name

    @Qualifier("beanName") 

  7.@Configration  配置菜单

    配置在类上,此注解通常为了通过 @Bean 注解生成 SpringIOC容器管理的类,本质和@Component一样。

    @Configration

  8.@Bean   配置第三方类的Bean

    配置在方法上,将方法返回的对象生成Bean,交给SpringIOC容器管理,要求此方法所在类也被配置成Bean,通常使用@Configration,

    注解的属性name,指定生成的Bean的name

    @Bean(name="beanName")

  9.@Resource(name="beanName")  jdk的自动装配

    相当于@Autowired,区别在于Resource先按beanName来找注入的Bean,如果没有指定名字的Bean,再按照类型来注入。

五、启动SpringIOC容器的方式    

  1.Spring采用xml配置时,根据xml文件来启动

ApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");ClassName cn = (ClassName)ac.getBean("beanName");

  2.Spring采用注解配置时,根据Springconfig配置类来启动

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
ClassName cn = (ClassName)ac.getBean("beanName");

六、注解和XML的混合使用

   1.注解引用XML

    @ImportResource({"classpath:spring-config.xml","classpath:spring-config1.xml"}),配置在SpringConfig类上

  2.多个注解配置类

    @Import({SpringConfig1.class,SpringConfig2.class})

  3.XML扫描注解 

<context:component-scan base-packages="包名1,包名2"/>

   4.多个XML文件

<import resource="spring-config2.xml"/><import resource="spring-config3.xml"/>

七、加载属性文件

·  1.XML加载资源属性文件

<context:property-placeholder ignore-resource-not-found="true" location="classpath:database-config.properties"/>

  2.注解加载资源属性文件

    @PropertySource(value={"classpath:database-config.properties"},ignoreResourceNotFound="true")

    value={"classpath:database-config.properties"}  : 资源属性文件位置

       ignoreResourceNotFound="true"  :  当扫描不到此资源配置文件时,不报错

八、Bean的作用域

  Spring提供了4种作用域:singleton(单例)  prototype(原型)  Session(会话)  request(请求)

  XML中的bean中有属性:scope ,默认是单例,可以设置为原型。会话和请求的作用域需要在web.xml中配置

  注解中有注解@Scope("singleton"),@Scope("prototype"),默认是单例,可以设置为原型。

 

    

  

  

    

  

  

 
 

Spring - SpringIOC容器详解的更多相关文章

  1. 三、spring成长之路——springIOC容器详解(上)

    目录 一.springIOC 一.springIOC 控制反转和依赖注入: ​ 简单的说就是将对象的创建,属性的的设置交给spring容器进行管理,而不再由用户自己创建,当用户需要使用该接口或者类的时 ...

  2. Spring ——Spring IoC容器详解(图示)

    1.1 Spring IoC容器 从昨天的例子当中我们已经知道spring IoC容器的作用,它可以容纳我们所开发的各种Bean.并且我们可以从中获取各种发布在Spring IoC容器里的Bean,并 ...

  3. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  4. Spring Boot异常处理详解

    在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...

  5. spring注入参数详解

    spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...

  6. Spring的lazy-init详解

    1.Spring中lazy-init详解ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化(也就是依赖注入).提前实例化意味着作为初始 ...

  7. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  8. (转)Spring事务管理详解

    背景:之前一直在学习数据库中的相关事务,而忽略了spring中的事务配置,在阿里面试时候基本是惨败,这里做一个总结. 可能是最漂亮的Spring事务管理详解 https://github.com/Sn ...

  9. Spring IOC使用详解

    SpringIOC使用详解 一.IOC简介 IOC(Inversion of Control):控制反转,即对象创建的问题.通俗地讲就是把创建对象的代码交给了Spring的配置文件来进行的.这样做的优 ...

随机推荐

  1. perl之创建临时文件夹遇到同名文件该咋办

    当你在目录下进行一系列操作时,若要创建许多文件或者修改文件,可能会遇到许多麻烦的事.所以呢,新建一个文件夹,然后在这个文件夹下新建文件或者修改文件.假设,你的代码要在一个目录下新建一个文件夹,名为Tm ...

  2. iOS 9 Safari广告拦截插件

    相对于谷歌对广告拦截的禁止,苹果与之态度截然相反,继Mac版Safari加入广告拦截工具之后,即将到来的iOS9对Safari也引入了内容拦截插件-Content Blocker,并且开发者可以使用最 ...

  3. 《CSS权威指南(第三版)》---第五章 字体

    这章主要的内容有: 1.字体:一般使用一种通用的字体. 2.字体加粗:一般从数字100 -900 . 3.字体大小:font-size 4.拉伸和调整字体:font-stretch 5.调整字体大小: ...

  4. Linux学习之路(三)搜索命令

    1.文件搜索命令locate 2.命令搜索命令whereis与which 3.字符串搜索命令grep 4.find命令与grep命令的区别 locate命令相对于find命令搜索非常快,find命令在 ...

  5. 1034 Head of a Gang (30)(30 分)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  6. AtCoder Grand Contest #026 C - String Coloring

    Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...

  7. 1026 Table Tennis (30)(30 分)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  8. Codeforces 758A. Holiday Of Equality 贪心

    题目大意: 给定一个长为\(n\)序列,每次操作在一个数上+1,求最小的操作次数使所有的数大小相同. 题解: 对这种题无话可说 #include <cstdio> #include < ...

  9. 11g dataguard 类型、保护模式、服务

    一. Dataguard中的备库分为物理备库和逻辑备库及快照备库 备库是主库的一致性拷贝,使用一个主库的备份可以创建多到30个备库,将其加入到dataguard环境中,创建成功后,dataguard通 ...

  10. poj2356Find a multiple——鸽巢定理运用

    题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案 ...