欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          Spring IOC和DI詳解

          Spring IOC和DI詳解

          什么是Spring

          Spring是一個(gè)以IoC和AOP為內(nèi)核的框架。

          IoC(Inversion of Control ,控制反轉(zhuǎn))是Spring的基礎(chǔ)。

          IoC簡(jiǎn)單說(shuō)就是創(chuàng)建對(duì)象由以前的程序員調(diào)用new 構(gòu)造方法,變成了交由Spring創(chuàng)建對(duì)象。

          DI(Dependency Inject,依賴(lài)注入)與IoC的含義相同,只不過(guò)這兩個(gè)稱(chēng)呼是從兩個(gè)角度描述的同一個(gè)概念。

          簡(jiǎn)單地說(shuō), DI就是對(duì)象的屬性,已經(jīng)被注入好相關(guān)值了,直接使用即可。

          Spring IOC和DI詳解

          IoC-控制反轉(zhuǎn)

          ??把各個(gè)對(duì)象類(lèi)封裝之后,通過(guò)IoC容器來(lái)關(guān)聯(lián)這些對(duì)象類(lèi)。這樣對(duì)象與對(duì)象之間就通過(guò)IoC容器進(jìn)行聯(lián)系,而對(duì)象與對(duì)象之間沒(méi)有什么直接聯(lián)系。
          Spring IOC和DI詳解

          ??應(yīng)用程序在沒(méi)有引入IoC容器之前,對(duì)象A依賴(lài)對(duì)象B,那么A對(duì)象在實(shí)例化或者運(yùn)行到某一點(diǎn)的時(shí)候,自己必須主動(dòng)創(chuàng)建對(duì)象B或者使用已經(jīng)創(chuàng)建好的對(duì)象B,其中不管是創(chuàng)建還是使用已創(chuàng)建的對(duì)象B,控制權(quán)都在應(yīng)用程序自身。

          ??如果應(yīng)用程序引入了Ioc容器之后,對(duì)象A和對(duì)象B之間失去了直接聯(lián)系,所以,當(dāng)對(duì)象A實(shí)例化和運(yùn)行時(shí),如果需要對(duì)象B的話(huà),IoC容器會(huì)主動(dòng)創(chuàng)建一個(gè)對(duì)象B注入(即依賴(lài)注入)到對(duì)象A所需要的地方。由此,對(duì)象A獲得依賴(lài)對(duì)象B的過(guò)程,由主動(dòng)行為變成了被動(dòng)行為,即把創(chuàng)建對(duì)象交給了IoC容器處理,控制權(quán)顛倒過(guò)來(lái)了,這就是所謂的控制反轉(zhuǎn)。

          DI-依賴(lài)注入

          由IoC容器在運(yùn)行期間,動(dòng)態(tài)地將某種依賴(lài)關(guān)系注入到對(duì)象之中。例如,將對(duì)象B注入(賦值)給對(duì)象A的成員變量。

          ??事實(shí)上,依賴(lài)注入(DI)和控制反轉(zhuǎn)(IoC)是對(duì)同一件事情的不同描述,從某個(gè)方面講,就是它們描述的角度不同。依賴(lài)注入是從應(yīng)用程序的角度在描述,即應(yīng)用程序依賴(lài)容器創(chuàng)建并注入它所需要的外部資源;而控制反轉(zhuǎn)是從容器的角度在描述,即容器控制應(yīng)用程序,由容器反向的向應(yīng)用程序注入應(yīng)用程序所需要的外部資源。這里所說(shuō)的外部資源可以是外部實(shí)例對(duì)象,也可能是外部文件對(duì)象等。

          IoC與DI的實(shí)現(xiàn)

          ??(1)屬性setter方法注入:指IoC容器使用setter方法注入被依賴(lài)的實(shí)例。通過(guò)調(diào)用無(wú)參構(gòu)造器或無(wú)參靜態(tài)工廠方法實(shí)例化Bean后,調(diào)用該Bean的setter方法,即可實(shí)現(xiàn)基于setter方法的依賴(lài)注入。該方式簡(jiǎn)單、直觀,而且容易理解,所以Spring的設(shè)置注入被大量使用。

          package com.ssm.entry;  public class UserServiceImpl implements UserService {  	private UserDao userDao; 	 	// setter()實(shí)現(xiàn)依賴(lài)注入 	public void setUserDao(UserDao userDao){ 		this.userDao = userDao; 	} 	 	public void login() { 		this.userDao.login(); 		System.out.println("UserService login"); 	}  }

          ??(2)構(gòu)造方法注入:指IoC容器使用構(gòu)造方法注入被依賴(lài)的實(shí)例?;跇?gòu)造方法的依賴(lài)注入通過(guò)調(diào)用帶參數(shù)的構(gòu)造方法來(lái)實(shí)現(xiàn),每個(gè)參數(shù)代表著一個(gè)依賴(lài)。

          applicationContext.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-4.3.xsd">     		     <!-- 將指定類(lèi)配置給Spring,讓Spring創(chuàng)建其對(duì)象的實(shí)例 控制反轉(zhuǎn) --> 		<bean id="UserDao" class="com.ssm.entry.UserDaoImpl"></bean> 		 		<!-- 添加一個(gè)id為userService的Bean 依賴(lài)注入--> 		<bean id="UserService" class="com.ssm.entry.UserServiceImpl"> 			<!-- 將name為UserDao的Bean注入U(xiǎn)serService實(shí)例中 --> 			<property name="UserDao" ref="UserDao"/> 		</bean> </beans>

          IoC測(cè)試

          package com.ssm.entry;  import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class IoC { 	public static void main(String[] args) { 		 		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 		UserDao userDao = (UserDao)applicationContext.getBean("UserDao"); 		userDao.login(); 		 	} }

          DI測(cè)試

          package com.ssm.entry;  import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class DI { 	 	public static void main(String[] args) { 		ApplicationContext applicationContext = new  				ClassPathXmlApplicationContext("applicationContext.xml"); 		UserService userService = (UserService)applicationContext.getBean("UserService"); 		userService.login(); 	} 	 }

          推薦教程:《Java教程》

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)