核心技術(shù)-IoC容器

1、IoC容器簡介

控制反轉(zhuǎn)IoC(Inversion of Control)又稱依賴注入DI(dependency injection),由org.springframework.beans和org.springframework.context基礎包構(gòu)成了IoC容器,其中BeanFactory接口提供了一種先進的配置機制能夠管理任何類型的對象,ApplicationContext是BeanFactory的一個子類,它更加簡便的集成了Spring的AOP特性,通常情況下使用ApplicationContext,因為它提供了BeanFactory的完整超集,如果想用BeanFactory替代ApplicationContext可以參考 Section 6.16, “The BeanFactory”
Spring IoC容器工作圖描述

Paste_Image.png

2、源數(shù)據(jù)(Metadata)配置

可以基于以下三種方式配置:

  • 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> 
    </bean> 
    <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> 
    </bean> <!-- more bean definitions go here -->
</beans>

允許組合多個bean配置xml

<beans> 
    <import resource="services.xml"/> 
    <import resource="resources/messageSource.xml"/> 
    <import resource="/resources/themeSource.xml"/> 

    <bean id="bean1" class="..."/> 
    <bean id="bean2" class="..."/>
</beans>

使用容器加載bean

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
  <context:annotation-config/>
</beans>
@Configuration
public class AppConfig { 
    @Bean
    public MyService myService() { 
      return new MyServiceImpl(); 
    }
}

加載bean

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
MyService myService = ctx.getBean(MyService.class); 
myService.doStuff();
3、實例化bean
  • 通過構(gòu)造函數(shù)實例化
<bean id="exampleBean" class="examples.ExampleBean"/>
  • 通過靜態(tài)方法實例化
    java code
public class ClientService { 
    private static ClientService clientService = new ClientService(); 
    private ClientService() {} 
    public static ClientService createInstance() { 
      return clientService; 
    }
}

xml配置

<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>

靜態(tài)方法帶參數(shù)(通過constructor-arg傳入?yún)?shù))
java code

public class ExampleBean { 
    // a private constructor 
    private ExampleBean(...) { ... } 
    // a static factory method; the arguments to this method can be 
    // considered the dependencies of the bean that is returned, 
    // regardless of how those arguments are actually used. 
    public static ExampleBean createInstance ( 
      AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { 
        ExampleBean eb = new ExampleBean (...); 
        // some other operations... return eb; 
    }
}

xml 配置

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
    <constructor-arg ref="anotherExampleBean"/> 
    <constructor-arg ref="yetAnotherBean"/> 
    <constructor-arg value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  • 通過普通方法實例化
    java code
public class DefaultServiceLocator { 
    private static ClientService clientService = new ClientServiceImpl(); 
    private DefaultServiceLocator() {} 
    public ClientService createClientServiceInstance() { 
        return clientService; 
    }
}

xml配置

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator"> 
    <!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
4、依賴注入
  • 基于構(gòu)造器的注入
    普通類型的參數(shù)
    java code
package x.y;
public class Foo { 
    public Foo(Bar bar, Baz baz) { 
    // ... 
    }
}

xml配置

<beans> 
    <bean id="foo" class="x.y.Foo"> 
       <constructor-arg ref="bar"/> 
       <constructor-arg ref="baz"/> 
    </bean> 
    <bean id="bar" class="x.y.Bar"/> 
    <bean id="baz" class="x.y.Baz"/>
</beans>

基本數(shù)據(jù)類型的參數(shù)
java code

package examples;
public class ExampleBean { 
    // Number of years to calculate the Ultimate Answer 
    private int years; 
    // The Answer to Life, the Universe, and Everything 
    private String ultimateAnswer; 
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; this.ultimateAnswer = ultimateAnswer; 
    }
}

xml配置-type

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg type="int" value="7500000"/> 
    <constructor-arg type="java.lang.String" value="42"/>
</bean>

xml配置-index

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg index="0" value="7500000"/> 
    <constructor-arg index="1" value="42"/>
</bean>

xml配置-name

<bean id="exampleBean" class="examples.ExampleBean"> 
    <constructor-arg name="years" value="7500000"/> 
    <constructor-arg name="ultimateAnswer " value="42"/>
</bean>

注意,基于那么的注入,如果你的編譯不是以debug方式編譯,那么參數(shù)名是會變掉,這時候需要通過jdk注解指定編譯后的參數(shù)名

package examples;
public class ExampleBean { 
    // Fields omitted 
    @ConstructorProperties({"years", "ultimateAnswer"})
    public ExampleBean(int years, String ultimateAnswer) { 
      this.years = years; 
      this.ultimateAnswer = ultimateAnswer; 
    }
}
  • 基于setter的注入
    java code
public class SimpleMovieLister { 
    // the SimpleMovieLister has a dependency on the MovieFinder 
    private MovieFinder movieFinder; 
    // a setter method so that the Spring container can inject a MovieFinder 
    public void setMovieFinder(MovieFinder movieFinder) { 
      this.movieFinder = movieFinder; 
    } 
    // business logic that actually uses the injected MovieFinder is omitted...
}

xml配置

<bean id="simpleMovieLister" class="examples.SimpleMovieLister"> 
    <!-- setter injection using the nested ref element --> 
    <property name="movieFinder"> 
      <ref bean="movieFinderBean"/> 
    </property>
</bean>
<bean id="movieFinderBean" class="examples.MovieFinder"/>

該方式依賴注入由容器調(diào)用setter方法將匹配參數(shù)類型的bean注入進去,所以該方式注入會在其它構(gòu)造器注入之后之后注入

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容