当前位置: 移动技术网 > IT编程>开发语言>Java > Spring - SpringIOC容器详解

Spring - SpringIOC容器详解

2019年06月10日  | 移动技术网IT编程  | 我要评论
一、什么是Spring IOC: Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。 二、Spring中如何实现DI(依赖注入) 1.构造器注入 2.Se ...

一、什么是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"),默认是单例,可以设置为原型。

 

 

    

 

 

  

  

 

    


  

 

 

  

 
 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网