Spring5學(xué)習(xí)之基礎(chǔ)知識(shí)總結(jié)
1.概述
1、Spring 是輕量級(jí)的開源的 JavaEE 框架
2、 Spring 可以解決企業(yè)應(yīng)用開發(fā)的復(fù)雜性
3、Spring 有兩個(gè)核心部分:IOC 和 Aop
IOC:控制反轉(zhuǎn),把創(chuàng)建對(duì)象過程交給 Spring 進(jìn)行管理Aop:面向切面,不修改源代碼進(jìn)行功能增強(qiáng)
4、Spring 特點(diǎn)
方便解耦,簡(jiǎn)化開發(fā)Aop 編程支持方便程序測(cè)試方便和其他框架進(jìn)行整合方便進(jìn)行事務(wù)操作降低 API 開發(fā)難度
2.入門Demo
1.jar包引入
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zj.springDemo</groupId>
<artifactId>springDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>spring的入門demo</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.6</version>
</dependency>
</dependencies>
</project>
2.bean
package cn.zj.demo.bean;
public class User {
public void add() {
System.out.println("add....");
}
}
3.spring的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.xsd">
<!--1 配置User對(duì)象創(chuàng)建-->
<bean id="user" class="cn.zj.demo.bean.User"></bean>
</beans>
4.測(cè)試代碼
package cn.zj.demo.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.zj.demo.bean.User;
public class SpringTest {
@Test
public void testAdd() {
// 1 加載 spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2 獲取配置創(chuàng)建的對(duì)象
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}
5.輸出
cn.zj.demo.bean.User@24111ef1 add....
3.源碼
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
//容器刷新前的準(zhǔn)備,設(shè)置上下文狀態(tài),獲取屬性,驗(yàn)證必要的屬性等
prepareRefresh();
//獲取新的beanFactory,銷毀原有beanFactory、為每個(gè)bean生成BeanDefinition等
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
//配置標(biāo)準(zhǔn)的beanFactory,設(shè)置ClassLoader,設(shè)置SpEL表達(dá)式解析器,添加忽略注入的接口,添加bean,添加bean后置處理器等
postProcessBeanFactory(beanFactory);
// 實(shí)例化并調(diào)用所有注冊(cè)的beanFactory后置處理器(實(shí)現(xiàn)接口BeanFactoryPostProcessor的bean,在beanFactory標(biāo)準(zhǔn)初始化之后執(zhí)行)。
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
//實(shí)例化和注冊(cè)beanFactory中擴(kuò)展了BeanPostProcessor的bean。
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
//初始化國(guó)際化工具類MessageSource
initMessageSource();
//初始化事件廣播器
initApplicationEventMulticaster();
//模板方法,在容器刷新的時(shí)候可以自定義邏輯,不同的Spring容器做不同的事情。
onRefresh();
//注冊(cè)監(jiān)聽器,廣播early application events
registerListeners();
//實(shí)例化所有剩余的(非懶加載)單例
//實(shí)例化的過程各種BeanPostProcessor開始起作用。
finishBeanFactoryInitialization(beanFactory);
//refresh做完之后需要做的其他事情。
//清除上下文資源緩存(如掃描中的ASM元數(shù)據(jù))
//初始化上下文的生命周期處理器,并刷新(找出Spring容器中實(shí)現(xiàn)了Lifecycle接口的bean并執(zhí)行start()方法)。
//發(fā)布ContextRefreshedEvent事件告知對(duì)應(yīng)的ApplicationListener進(jìn)行響應(yīng)的操作
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
}

三級(jí)緩存

到此這篇關(guān)于Spring5學(xué)習(xí)之基礎(chǔ)知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Spring5知識(shí)總結(jié)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java導(dǎo)出Excel通用工具類實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Excel通用工具類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot簡(jiǎn)單使用SpringData的jdbc和durid
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot簡(jiǎn)單使用SpringData的jdbc和durid,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Java并發(fā)編程數(shù)據(jù)庫與緩存數(shù)據(jù)一致性方案解析
這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
SpringBoot利用@Retryable注解實(shí)現(xiàn)接口重試
本文主要介紹了springboot如何利用@Retryable注解實(shí)現(xiàn)接口重試功能,文中示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Maven多模塊及version修改的實(shí)現(xiàn)方法
這篇文章主要介紹了Maven多模塊及version修改的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例
這篇文章主要介紹了java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例,簡(jiǎn)單介紹了隊(duì)列的相關(guān)基礎(chǔ)知識(shí),然后通過實(shí)例向大家展示其實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
myeclipse創(chuàng)建servlet_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了myeclipse創(chuàng)建servlet的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Java Swing中JDialog實(shí)現(xiàn)用戶登陸UI示例
這篇文章主要介紹了Java Swing中JDialog實(shí)現(xiàn)用戶登陸UI功能,結(jié)合完整實(shí)例形式分析了Swing使用JDialog實(shí)現(xiàn)用戶登陸UI界面窗口功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Java迭代器實(shí)現(xiàn)Python中的range代碼實(shí)例
這篇文章主要介紹了Java迭代器實(shí)現(xiàn)Python中的range代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

