美文网首页
Spring学习--IoC容器

Spring学习--IoC容器

作者: mnikn | 来源:发表于2017-03-11 19:12 被阅读26次

依赖注入

依赖注入是把生成被调用类对象转接给第三方做。例如两个演员类要进行互动,其中一个演员需要得到另一个演员的行为结果,如没有依赖注入的话一般需要在演员类里面生成另一个演员类的对象,代码如下:

public Actor1{
  private Actor2 actor2;
  void ask(){
    actor2 = new Actor2();
    actor2.reponse();
  }
}

但是如果采用依赖注入的话代码如下:

public Actor1{
  private Actor2 actor2;
  public Actor1(Actor2 actor2){
    this.actor2 = actor2;
  }
  void ask(){
    actor2.reponse();
  }
}

我们通过在构造函数注入对象来避免类里生成对象的情况,这样的好处是如果类里面要调用的是接口的话可以避免直接生成对象从而实现多态。

依赖注入常用分为两种,构造函数注入和属性注入。构造函数注入已在上方演示过,适用于必须用被调用对象的情况下。但是在某些情况下可能只是其中一个方法会用到,为此而生成对象太浪费内存。此时可以采用属性注入,代码如下:

public Actor1{
  private Actor2 actor2;
  public setActor2(Actor2 actor2){
    this.actor2 = actor2;
  }
  void ask(){
    actor2.reponse();
  }
}

Spring IoC

对于Spring来说我们需要在xml上写明依赖关系,代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <bean id="actor2" class="LiuDeHua"/>
   <bean id="actor1" class="com.mnikn.ioc.Actor1" 
         p:actor2-ref="actor2"/>
</beans>

Spring的IoC(依赖注入)通过Java反射机制来实现。

Spring Resource

Spring框架实现了Resource接口来简化读取资源的工作,其支持的资源类型前缀为:

"classpath:"对应有"classpath:*","classpath:"只会在加载第一个符合规则的包下找,"classpath:*"会在所有符合规则的情况下找。
资源地址支持以下匹配符:

  • ?,匹配文中一个字符。
  • *,匹配文中任意个字符。
  • **,匹配多层路径。

Spring IoC容器

Spring框架内部有BeanFactory来根据配置文件建立Bean,一般称BeanFactory为IoC容器,称ApplicationContext为Spring容器,ApplicationContext面向使用Spring框架的应用者。


BeanFactory结构图

配置文件示例:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <bean id="actor1" class="com.mnikn.test.Car"
        p:brand="Bens"
        p:color="Red"
        p:maxSpeed=40
</beans>s

其中WebApplicationContext继承ApplicationContext,针对Web实现了一些操作,WebApplicationContext的初始化示例:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>sharingcode</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:sharingcode-servlet.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sharingcode</servlet-name>
        <url-pattern>*.html</url-pattern>

    </servlet-mapping>

ContextLoaderListener负责整合Web容器和Spring容器,只负责监听Web容器的启动关闭事件,而RequestContextListener负责监听Http请求事件。

Bean生命周期

Bean生命周期过程如下:


Bean生命周期图

Bean命名规定前两个字母全大写或全小写。

相关文章

网友评论

      本文标题:Spring学习--IoC容器

      本文链接:https://www.haomeiwen.com/subject/lusagttx.html