Spring的核心容器及依賴注入

Spring的核心容器

Spring 框架的主要功能是通過其核心容器來實現的,Spring 框架提供了兩種核心容器,分別爲 BeanFactory,ApplicationContext

BeanFactory 就是一個管理 Bean 的工廠,它主要負責初始化各種 Bean ,並調用它們的生命週期方法。

ApplicationContext是BeanFactory的子接口,也被稱爲應用上下文,是另一種常用的Spring核心容器。它由org.springframework.context.ApplicationContext接口定義,不僅包含了BeanFactory的所有功能,還添加了對國際化、資源訪問、事件傳播等方面的支持。創建ApplicationContext接口實例,通常採用兩種方法,具體如下:

  1. 通過ClassPathXmlApplicationContext創建
    ApplicationContext applicationContext =new ClassPathXmlApplicationContext(String configLocation);
    ClassPathXmlApplicationContext會從類路徑classPath中尋找指定的XML配置文件,找到並裝載完成ApplicationContext的實例化工作。

  2. 通過FileSystemXmlApplicationContext創建
    ApplicationContext applicationContext =new FileSystemXmlApplicationContext(String configLocation);
    FileSystemXmlApplicationContext會從指定的文件系統路徑(絕對路徑)中尋找指定的XML配置文件,找到並裝載完成ApplicationContext的實例化工作。

在Java項目中,會通過ClassPathXmlApplicationContext類來實例化ApplicationContext容器。而在Web項目中,ApplicationContext容器的實例化工作會交由Web服務器來完成。
Web服務器實例化ApplicationContext容器時,通常會使用ContextLoaderListener來實現,此種方式只需要在web.xml中添加如下代碼:

<context-param>
               <param-name>contextConfigLocation</param-name> 
               <param-value>
                          classpath:spring/applicationContext.xml
               </param-value>
       </context-param> 
       <listener>
               <listener-class>
                         org.springframework.web.context.ContextLoaderListener
               </listener-class>
       </listener>

創建Spring容器後,就可以獲取Spring容器中的Bean。Spring獲取Bean的實例通常採用以下兩種方法:
Object getBean(String name);
根據容器中Bean的id或name來獲取指定的Bean,獲取之後需要進行強制類型轉換。
<T> T getBean(Class<T> requiredType);
根據類的類型來獲取Bean的實例。由於此方法爲泛型方法,因此在獲取Bean之後不需要進行強制類型轉換。

依賴注入

DI的全稱是Dependency Injection,中文稱之爲依賴注入。它與控制反轉(IoC)的含義相同,只不過這兩個稱呼是從兩個角度描述的同一個概念。

控制反轉IoC:在使用Spring框架之後,對象的實例不再由調用者來創建,而是由Spring容器來創建,Spring容器會負責控制程序之間的關係,而不是由調用者的程序代碼直接控制。這樣,控制權由應用代碼轉移到了Spring容器,控制權發生了反轉,這就是控制反轉。

依賴注入DI:從Spring容器的角度來看,Spring容器負責將被依賴對象賦值給調用者的成員變量,這相當於爲調用者注入了它依賴的實例,這就是Spring的依賴注入。

依賴注入的實現方式

Spring依賴注入是讓組件依賴於抽象,當組件要與其他實際對象發生依賴關係時,通過抽象來注入依賴的實際對象。 實現方式有屬性setter方法注入、構造方法注入和接口注入。

1.屬性setter方法注入指IoC容器使用setter方法來注入被依賴的實例。通過調用無參構造器或無參靜態工廠方法實例化Bean後,調用該Bean的setter方法,即可實現基於setter方法的依賴注入。

2.構造方法注入是指IoC容器使用構造方法來注入被依賴的實例。基於構造方法的依賴注入通過調用帶參數的構造方法來實現,每個參數代表着一個依賴。

3.接口注入指的就是在接口中定義要注入的信息,並通過接口完成注入。


(下方代碼使用setter方法實現依賴注入) 1.在com.itheima.ioc包中,創建接口UserService,在接口中編寫一個say()方法。
package com.itheima.ioc;
        public interface UserService {
                 public void say();
        }

2.在com.itheima.ioc包中,創建UserService接口的實現類UserServiceImpl,在類中聲明userDao屬性,並添加屬性的setter方法。

package com.itheima.ioc;
        public class UserServiceImpl implements UserService {
	private UserDao userDao; 
	public void setUserDao(UserDao userDao) {
	         this.userDao = userDao;
	} 
	public void say() { 
	         this.userDao.say();
	         System.out.println("userService say hello World !");
	}
         }

3.在配置文件applicationContext.xml中,創建一個id爲userService的Bean,該Bean用於實例化UserServiceImpl類的信息,並將userDao的實例注入到userService中。

<bean id="userService" class="com.itheima.ioc.UserServiceImpl"> 
          <property name="userDao" ref="userDao" />
</bean>

4.在com.itheima.ioc包中,創建測試類TestDI,來對程序進行測試。

package com.itheima.ioc;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        public class TestDI {
	public static void main(String[] args) { 
	        ApplicationContext applicationContext = 
                        new ClassPathXmlApplicationContext("applicationContext.xml"); 
	        UserService userService = (UserService) applicationContext.getBean("userService"); 
	        userService.say();
	}
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章