spring框架溫習—IOC

IOC控制反轉,降低代碼之間的耦合度。把對象的創建交給spring來完成
實現方法 1.xml配置文件方式 2.註解方式
使用的技術:xml配置文件、dom4j解析xml文件、工廠設計模式、反射
實現原理
問題:Controller需要調用Service,不通過new(修改一個地方,需要找找找改改改)的方式怎麼解決

public class DemoService(){
	
}

public class DemoController(){
	//new DemoService()得到service麼?
}

①創建xml配置文件,配置要創建的類

<bean id="demoService" class="com.pro1.service.DemoService"/>

②創建工廠類,使用dom4j解析xml配置文件,使用反射創建對象

pubic class demoFactory(){
	public static DemoService getService(){
		//使用dom4j解析xml文件
		//根據id值得到對應的class屬性
		String classValue = "class屬性值";
		//使用反射創建類對象
		Class clazz = Class.forName(classValue);
		//創建類對象
		DemoService demoService = clazz.newInstance();
		return demoService;
	}
}

則Controller中不需new

public class DemoController(){
	demoFactory.getService();
}

在這裏插入圖片描述
bean的實例化三種方式(創建類)實際開發中只用第一種(默認的無參構造方法)
1.使用類的無參構造方法實例化
在這裏插入圖片描述
調用的就是類的無參構造方法
2.使用靜態工廠創建(DmeoFactory.getBean())

<!-- 靜態工廠創建類 -->
<bean id="getBeanStatic" class="com.idea.ioc.UserBeanFactory" factory-method="getBeanStatic"></bean>
//靜態創建類工廠
public class UserBeanFactory {
	public static User getBeanStatic() {
		return new User();
	}
}
public class TestIoc {

//測試類
@Test
public void testUserService() {
	//加載spring配置文件
	ApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		//得到配置文件中的對象
//		User user = (User) context.getBean("userService");
		User user = (User) context.getBean("getBeanStatic");
		System.out.println(user);
		user.add();
		
	}
}

3.使用實例工廠創建(創建的不是靜態方法,new DemoFactory().getBean()實例化)
bean標籤的常用屬性
①id屬性 名稱標識,不包含特殊符號,簡潔明瞭
②class屬性 需要創建對象類的全路徑
③name屬性 和id功能一樣,但是name屬性可以包含特殊符號(不怎麼使用,針對Struts1框架)
④scope屬性 (主要使用前兩個
singleton 默認值,單例 對象只創建一次
prototype 多例的 每一次創建都是創建新的對象
request web項目中,spring創建一個bean對象,將對象存入request域中
session web項目中,spring創建一個bean對象,將對象存入session域中
globalSession web項目中,應用在Prolet環境,如果沒有Prolet環境那麼相當於session,比如登錄一次百度賬號,貼吧、網盤等百度產品都不需要再次登錄

屬性注入之-注入對象類型屬性
①創建service 、dao類(需要在service中得到dao對象)
②在service中定義一個dao類型屬性(把dao對象做爲屬性類型)
③生成dao類型屬性的方法

DI:依賴注入 向類中屬性設置值(IOC爲創建對象)
依賴注入不能單獨存在,需要在IOC的基礎上

spring整合web項目的基本原理
spring創建對象時需要加載xml文件,每次都需要new對象實現,效率很低,那麼可以考慮將加載配置文件和創建對象過程在服務器啓動時完成

//加載spring配置文件
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");

(均已被spring封裝,只需配置即可)
服務啓動時,每個項目創建一個(servlet中)ServletContext對象,監聽器可以監聽到ServletContext對象什麼時候創建,在此時加載配置文件並創建其中的對象,把創建出來的對象放在ServletContext域對象中(setAttribute()),獲取對象時從ServletContext域對象中獲取(getAttribute())

開啓註解模式
引入jar包在這裏插入圖片描述

<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"> <!-- bean definitions here -->
	
	<!-- 開啓註解掃描  屬性爲需要掃描的包位置 掃描配置路徑下 類 方法 屬性上面是否有註解
		掃描多個包 1 分隔開com.idea.anno,com.idea.anno,....
			   2 寫包含所有需要掃描的包的父路徑 如:com.idea
	-->
	<context:component-scan base-package="com.idea.anno"></context:component-scan>
	
	<!-- 只掃描屬性上邊的註解 -->
	<!-- <context:annotation-config></context:annotation-config> -->
</beans>

在需要創建的類上添加

@Component(value="user") //同配置文件中<bean id="user" class=""/>

創建對象有四個註解
Spring中提供Component的三個衍生註解:(功能上目前是一樣的…都是創建對象,爲了讓標註類本身的用途清晰和後續提供擴展)

@Controller //web層
@Service  //業務層
@Repository  //持久層
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章