1?官方資料?
1.1 官網
1.2?進入 Spring5
下拉 projects, 進入 Spring Framework
進入 Spring5 的 github
1.3 在maven項目中導入依賴
<dependencies><!--加入spring開發的基本包--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.8</version></dependency><!--加入spring開發切面編程需要的包--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.8</version></dependency>
</dependencies>
?1.4 官方文檔
2?Spring核心內容
上圖說明:
3 Spring的幾個重要概念
(1)Spring 可以整合其他的框架(Spring 是管理框架的框架)
(2)Spring 有兩個核心的概念: IOC 和 AOP
(3)IOC(Inversion Of Control 反轉控制)
- 傳統的開發模式[JdbcUtils / 反射]
程序------>環境(程序讀取環境配置,然后自己創建對象.)
解讀:程序員編寫程序, 在程序中讀取配置信息;創建對象, new 或者?反射方式;使用對象完成任務
- IOC 的開發模式 [EmpAction EmpService EmpDao Emp]?
程序<-----容器(容器創建好對象,程序直接使用.)
解讀:?

4 Spring快速入門
4.1 需求說明

4.2 實現步驟
(1)創建一個maven項目,在pom.xml文件中導入以下依賴
<dependencies><!--加入spring開發的基本包--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.8</version></dependency><!--加入spring開發切面編程需要的包--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.8</version></dependency>
</dependencies>
(2) 創建 Moster 的javabean/entity,無參構造器一定要寫
public class Monster {private Integer monsterId;private String name;private String skill;//全參構造器public Monster(Integer monsterId, String name, String skill) {this.monsterId = monsterId;this.name = name;this.skill = skill;}//無參構造器一定要寫,Spring反射創建對象時,需要使用public Monster() {}public Integer getMonsterId() {return monsterId;}public void setMonsterId(Integer monsterId) {this.monsterId = monsterId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSkill() {return skill;}public void setSkill(String skill) {this.skill = skill;}@Overridepublic String toString() {return "Monster{" +"monsterId=" + monsterId +", name='" + name + '\'' +", skill='" + skill + '\'' +'}';}
}
(3) 新建一個容器配置文件:鼠標右鍵目錄名 --> new --> XML Configuration --> Spring Config,然后給配置文件取個名字,這里取beans(對于Maven項目XML文件通常在?src/main/resources
?目錄)
點擊 創建 Spring facet ,然后點擊確定
?
?(4)在xml文件中配置Moster對象,在beans標簽加入以下配置
?注意:將class屬性值修改為指定類的全路徑
<!--1. 配置monster對象/javabean2. 在beans中可以配置多個bean3. 一個bean表示就是一個java對象4. class屬性是用于指定類的全路徑->spring底層使用反射創建5. id屬性表示該java對象在spring容器中的id, 通過id可以獲取到對象6. <property name="monsterId" value="100"> 用于給該對象的屬性賦值
-->
<bean class="com.spring.bean.Monster" id="monster01"><property name="monsterId" value="100"/><property name="name" value="牛魔王"/><property name="skill" value="芭蕉扇"/>
</bean><bean class="com.spring.bean.Monster" id="monster02"><property name="monsterId" value="1001"/><property name="name" value="牛魔王~"/><property name="skill" value="芭蕉扇~"/>
</bean>
(5)新建一個測試類來獲取容器中的對象
public class SpringBeanTest {@Testpublic void getMonster(){//1. 創建容器 ApplicationContext//2. 該容器和容器配置文件關聯ApplicationContext ioc =new ClassPathXmlApplicationContext("beans.xml");//3. 通過getBean獲取對應的對象// 默認返回的是Object , 但是運行類型Monster//Object monster01 = ioc.getBean("monster01");Monster monster01 = (Monster) ioc.getBean("monster01");//4. 輸出System.out.println("monster01=" + monster01 + " 運行類型=" + monster01.getClass());System.out.println("monster01=" + monster01 + "屬性name=" + monster01.getName() +" monserid=" + monster01.getMonsterId());//5. 也可以在獲取的時候,直接指定Class類型, 可以再次獲取Monster monster011 = ioc.getBean("monster01", Monster.class);System.out.println("monster011=" + monster011);System.out.println("monster011.name=" + monster011.getName());//6. 查看容器注入了哪些bean對象,會輸出bean的idSystem.out.println("================================");String[] beanDefinitionNames = ioc.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {System.out.println("beanDefinitionName=" + beanDefinitionName);}System.out.println("================================");System.out.println("ok~~~");}
}
運行結果:
4.3 注意事項
(1)該代碼為什么讀取到beans.xml文件
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
該代碼通過類路徑加載配置文件,可通過以下方式輸出類加載路徑
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
maven項目的類加載路徑是:target\classes
(2)debug 看看 spring 容器結構/機制
?
(3) 當調用getBean(),方法獲取對象時,會去beanDefinitionMap中去查找,如果發生該bean是單例的,就會返回 singletonobject 中初始化好了的對象,如果不是,就會動態的通過反射機制返回一個臨時創建的對象
?5 實現簡單的基于XML配置的Spring
5.1?需求說明
5.2 思路分析
(1)使用xml的解析技術-Dom4j
(2)讀取beans.xml
(3)解析得到monster對象的id,classpath,屬性
(4)使用反射,創建對象
(5)放入到ioc容器(hashmap)
(6)提供一個getBean方法
5.3 實現步驟
(1)在maven項目中添加dom4j依賴
第一種方式:如果導入低版本(1.6.1),需要如下兩個依賴
<dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version>
</dependency>
<dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.1.1</version>
</dependency>
第二種方式:如果導入高版本(2.0.0),需要如下一個依賴
<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.0.0</version>
</dependency>
(2)實現容器類 WwjApplicationContext,java
注意:項目系統目錄不能有中文,或者會找不到XML文件
package com.spring.wwjApplicationContext;import com.spring.bean.Monster;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.io.File;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;/*** 1. 這個程序用于實現Spring的一個簡單容器機制* 2. 這里實現如何將beans.xml文件進行解析,并生成對象,放入容器中* 3. 提供一個方法 getBean(id) 返回對應的對象*/
public class WwjApplicationContext {private ConcurrentHashMap<String, Object> singletonObjects =new ConcurrentHashMap<>();//構造器//接收一個容器的配置文件 比如 beans.xml, 該文件默認在resourcespublic WwjApplicationContext(String iocBeanXmlFile) throws Exception {//1. 得到類加載路徑
// String path = this.getClass().getClassLoader().getPath();String path = this.getClass().getResource("/").getPath();//2. 創建 SaxreaderSAXReader saxReader = new SAXReader();//3. 得到Document對象Document document =saxReader.read(new File(path + iocBeanXmlFile));//4. 得到rootDocumentElement rootElement = document.getRootElement();//5. 得到第一個bean-monster01Element bean = (Element) rootElement.elements("bean").get(0);//6. 獲取到第一個bean-monster01的相關屬性String id = bean.attributeValue("id");String classFullPath = bean.attributeValue("class");List<Element> property = bean.elements("property");//遍歷Integer monsterId =Integer.parseInt(property.get(0).attributeValue("value"));String name = property.get(1).attributeValue("value");String skill = property.get(2).attributeValue("value");//7. 使用反射創建對象.=> 回顧反射機制Class<?> aClass = Class.forName(classFullPath);//這里o對象就是Monster對象Monster o = (Monster) aClass.newInstance();//給o對象賦值o.setMonsterId(monsterId);o.setName(name);o.setSkill(skill);//8. 將創建好的對象放入到singletonObjectssingletonObjects.put(id, o);}public Object getBean(String id) {return singletonObjects.get(id);}
}
(3)創建main類測試
package com.spring;import com.spring.bean.Monster;
import com.spring.wwjApplicationContext.WwjApplicationContext;public class AppMain {public static void main(String[] args) throws Exception {WwjApplicationContext ioc = new WwjApplicationContext("beans.xml");Monster monster01 = (Monster)ioc.getBean("monster01");System.out.println("monster01 = " + monster01);}
}
運行結果: