標籤:XML 管理 span 應用 檔案引入 eclipse xmla string 思想
spring是什嗎?
Spring是一個輕量級的控制反轉(IoC)和面向切面(AOP)的容器架構
spring是一站式架構
web層:springMVC
service層:spring的ioc
dao層:spring的jdbcTemplate
spring的優點
1.低侵入式設計,代碼汙染極低
2.獨立於各種應用伺服器,基於Spring架構的應用
3.Spring的DI機制降低了業務對象替換的複雜性,提高了組件之間的解耦
4.Spring的AOP支援允許將一些通用任務如安全、事務、日誌等進行集中式管理,從而提供了更好的複用
5.Spring的ORM思想和DAO提供了與第三方持久層架構的良好整合,並簡化了底層的資料庫訪問
6.Spring並不強制應用完全依賴於Spring,開發人員可自由選用Spring架構的部分或全部
spring版本
spring 4.x(目前更新spring至 5.0)
spring的ioc操作
1、ioc的設定檔方式
2、ioc的註解方式
spring的ioc底層原理(降低類與類之間的耦合度)
1、xml設定檔
2、dom4j解析xml
3、工廠設計模式
4、反射
具體實現:
① 建立xml設定檔,配置要建立對象類
② 建立工廠類,使用dom4j解析設定檔+反射
返回UserService對象的方法
public static UserServicer getService(){}
使用dom4j解析xml檔案 根據id值userService,得到id值對應的class屬性值
String classValue = "class屬性值"
使用反射建立類對象
Class clazz = Class.forName(classValue);
建立類對象
UserService service = clazz.newInstance();
return service;
設定檔提示問題
spring引入schema約束,把約束檔案引入到eclipse中
①下載設定檔
②複製約束路徑
③在屬性中的XML CataLog中設定
Bean執行個體化的方式
第一種 使用類的無參構造方法建立(重點)
第二種 使用靜態工廠建立
第三種 使用執行個體工廠建立
Bean標籤常用屬性
id: 名稱,任意命名,不包含特殊符號
class:建立對象所在類的全路徑
name: 功能和id屬性一樣,可以包含特殊符號
scope:
singleton:預設值,單例的
prototype:多例的
request:建立對象把對象放到request域中
session:建立對象把對象放到session域裡中
globalSession:
屬性的注入
第一種 使用set方法注入(重點)
public class User{ private String name; public void setName(String name){ this.name = name; }}
第二種 有參數構造注入
public class User{ private String name; pulbic User(String name){ this.name = name; }}
第三種 使用介面注入
public interface Dao{ public void delete(String name);}public class DaoImpl implements Dao{ private String name; public void delete(String name){ this.name = name; }}
注入物件類型屬性(重點)
Service和Dao
P名稱控制項注
xmlns:p="http://www.springframework.org/schema/p"
注入複雜類型屬性
數組:
list集合:
map集合:
properties類型:
IOC和DI的區別
IOC(inversion of control):控制反轉,把對象建立交給spring進行配置
DI(Dependency Injection):依賴注入,向類裡面的屬性中設定值
關係:依賴注入不能單獨存在,需要在ioc的基礎之上來完成操作
spring整合web項目原理
1.載入spring核心設定檔
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
每次載入,new對象,功能可以實現,效率很低
2.實現思想:把載入設定檔和建立對象過程,在伺服器啟動時候完成
3.實現原理:
(1)SevletContext對象
(2)監聽器
具體使用:
在伺服器啟動時候,為每個項目建立一個Servlet對象
在ServletContext對象建立時候,使用監聽器可以具體到ServletContext對象在什麼時候可以建立
使用監聽器聽到ServletContext對象建立時候,載入spring設定檔,把設定檔設定物件建立
把建立出來的對象放到ServletContext域對象裡面(setAttribute方法)
擷取對象時,到ServletContext域得到(getAttribute方法)
spring架構(一)