網(wǎng)站做301跳轉(zhuǎn)的作用百度關(guān)鍵詞收錄
Spring 的基本概述
Spring學(xué)習(xí)的核心內(nèi)容—一圖勝千言
- IOC:控制反轉(zhuǎn),可以管理 Java 對象
- AOP:切面編程
- JDBCTemplate:是Spring提供一套訪問數(shù)據(jù)庫的技術(shù),應(yīng)用性強(qiáng),相對好理解
- 聲明式事務(wù):基于IOC / AOP實現(xiàn)事務(wù)管理
Spring的幾個重要概念
1、Spring可以整合其他的框架(Spring是管理框架的框架)
2、Spring有兩個核心的概念:IOC和AOP
3、IOC(Inversion Of Control 控制反轉(zhuǎn))
- 傳統(tǒng)的開發(fā)模式(JdbcUtils / 反射)
程序 —> 環(huán)境(程序讀取環(huán)境配置,然后自己創(chuàng)建對象)
1、程序員編寫程序,在程序中讀取到配置信息
2、創(chuàng)建對象,new Object() // 反射方式
3、使用對象完成任務(wù)
- IOC的開發(fā)模式
程序 <— 容器(容器創(chuàng)建好對象,程序直接使用)
1、Spring 根據(jù)配置文件XML / 注解來創(chuàng)建對象,并放入到容器(ConcurrentHashMap)中,并且可以完成對象之間的依賴
2、當(dāng)需要使用某個對象實例的時候,就直接從容器中獲取即可
3、程序員可以更加關(guān)心如果使用對象完成相應(yīng)的業(yè)務(wù)(以前是new => 注解 / 配置方式)
4、DI—Dependency Injection 依賴注入(是實現(xiàn)IOC的一種方法)
5、Spring最大的價值,通過配置給程序提供需要使用的web層[ Servlet(Action / Controller) ]/Service/Dao/[ JavaBean/Entity ]對象
- 這個是核心價值所在,也是IOC的具體體現(xiàn),實現(xiàn)解耦
Spring的快速入門
需求說明
- 通過 Spring 的方式(配置文件),獲取 JavaBean:Monster的對象,并給該對象屬性賦值,輸出該對象信息
完成步驟
- 下載Spring的開發(fā)包:https://spring.io/projects/spring-framework#learn
代碼實現(xiàn)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttps://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--1. 配置monster對象2. 在beans中可以配置多個bean3. bean表示就是一個java對象4. class屬性是用于指定類的全路徑 -> Spring底層使用反射創(chuàng)建5. id屬性表示該Java對象在Spring容器中的id,通過id可以獲取到該對象6. <property name="monsterId" value="100"/> 用于給該對象的屬性賦值--><bean class="com.zan.spring.bean.Monster" id="monster01"><!-- <bean class="com.zan.spring.bean.Monster">--><property name="monsterId" value="100"/><property name="name" value="牛魔王"/><property name="skill" value="芭蕉扇"/></bean></beans>
public class SpringBeanTest {@Testpublic void getMonster() {// 1. 創(chuàng)建容器 ApplicationContext// 2. 該容器和容器配置文件是相關(guān)聯(lián)的 - 讀取到的是out文件夾下的beans.xml文件ApplicationContext iocContext = new ClassPathXmlApplicationContext("beans.xml");// 3. 通過getBean獲取對應(yīng)的對象(傳入ID)// 默認(rèn)返回的是Object,但是運(yùn)行類型是Monster
// Object monster01 = iocContext.getBean("monster01");Monster monster01 = (Monster) iocContext.getBean("monster01");// 4. 輸出System.out.println("monster01=" + monster01 + " 運(yùn)行類型=" + monster01.getClass());System.out.println("monster01=" + monster01 + " 獲取對應(yīng)的屬性name=" + monster01.getName());// 5. 也可以在獲取的時候,直接指定Class類型,可以再次獲取Monster monster011 = iocContext.getBean("monster01", Monster.class);System.out.println("monster011=" + monster011);System.out.println("monster011.name=" + monster011.getName());// 6. 查看容器注入了哪些bean對象,獲取所有的beanNameString[] beanDefinitionNames = iocContext.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {System.out.println(beanDefinitionName);}System.out.println("ok~~~");}// 驗證類加載路徑@Testpublic void classPath() {File file = new File(this.getClass().getResource("/").getPath());// 看到類的加載路徑System.out.println("file = " + file);// file = D:\Code\Java\Learn\Spring\spring-review\out\production\spring-review}
}
Debug查看Spring容器結(jié)構(gòu)/機(jī)制
手動開發(fā) - 簡單的Spring基于XML配置的程序
需求說明
- 自己寫一個簡單的Spring容器, 通過讀取beans.xml,獲取第1個JavaBean:Monster的 對象,并給該的對象屬性賦值,放入到容器中, 輸出該對象信息
- 也就是不使用 Spring 的原生框架,我們自己簡單模擬實現(xiàn)
需求分析
代碼實現(xiàn)
1、導(dǎo)入 Dom4j.jar 包
2、編寫ZanApplicationContext
public class ZanApplicationContext {private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();// 構(gòu)造器// 接受一個容器的配置文件,比如 beans.xmlpublic ZanApplicationContext(String iocBeanXmlPath) throws Exception {// 1. 得到類加載路徑String path = this.getClass().getResource("/").getPath();// 2. 創(chuàng)建SAXReaderSAXReader saxReader = new SAXReader();// 3. 獲取文檔Document對象Document document = saxReader.read(new File(path + iocBeanXmlPath));// 4. 得到rootDocumentElement rootElement = document.getRootElement();// 5. 得到第一個bean對象 monster01Element bean = (Element) rootElement.elements("bean").get(0);// 6. 獲取到第一個 bean-monster01 的相關(guān)屬性String id = bean.attributeValue("id");String classFullPath = bean.attributeValue("class");List<Element> property = bean.elements("property");// 遍歷
// for (Element element : property) {
// String value = element.attributeValue("value");
// System.out.println(value);
// }// 這里簡便獲取Integer monsterId = Integer.parseInt(property.get(0).attributeValue("value"));String name = property.get(1).attributeValue("value");String skill = property.get(2).attributeValue("value");// 7. 使用反射創(chuàng)建對象 ==> 回顧反射機(jī)制Class<?> aClass = Class.forName(classFullPath); // 獲取對應(yīng)的class對象// 這是的o對象就是一個Monster對象Monster o = (Monster) aClass.newInstance();// 給o對象賦值// 使用反射賦值
// Method[] declaredMethods = aClass.getDeclaredMethods();
// for (Method declaredMethod : declaredMethods) {
// Object invoke = declaredMethod.invoke(o);
//
// }o.setMonsterId(monsterId);o.setName(name);o.setSkill(skill);// 8. 將創(chuàng)建好的對象放入到singleObjects中singletonObjects.put(id, o);}public Object getBean(String id) {// 還可以嚴(yán)謹(jǐn)一下return singletonObjects.get(id);}
}
- Test類
public class ZanApplicationContextTest {public static void main(String[] args) throws Exception {ZanApplicationContext zanApplicationContext = new ZanApplicationContext("beans.xml");Monster monster01 = (Monster) zanApplicationContext.getBean("monster01");System.out.println(monster01);}
}
Spring原生容器底層結(jié)構(gòu)
補(bǔ)充:可以不進(jìn)行分配ID,系統(tǒng)會默認(rèn)分配ID,分配ID的規(guī)則為全類名#1,全類名#1 …