學(xué)習(xí)視頻:【孫哥說Spring5:從設(shè)計模式到基本應(yīng)用到應(yīng)用級底層分析,一次深入淺出的Spring全探索。學(xué)不會Spring?只因你未遇見孫哥】
第二章、第一個Spring程序
1.軟件版本
1.JDK1.8+
2.Maven3.5+
3.IDEA2018+
4.SpringFramework 5.1.4
官網(wǎng):www.spring.io
2.環(huán)境搭建
-
Spring的jar包
1.設(shè)置pom的依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.4.RELEASE</version> </dependency>
-
Spring的配置文件
1.配置文件的放置位置:任意位置 沒有硬性要求
2.配置文件的命名:沒有硬性要求 建議 applicationContext.xml
思考:日后應(yīng)用Spring框架時,需要進行配置文件路徑的位置。
3.Spring的核心API
-
ApplicationContext
作用:Spring提供的ApplicationContext這個工廠,用于對象的創(chuàng)建
好處:解耦合
-
ApplicationContext接口類型
接口:屏蔽實現(xiàn)的差異
非web環(huán)境:ClassPathXmlApplicationContext(例如:main junit)
web環(huán)境:XmlWebApplicationContext
可以看到ApplicationContext就是一個接口
-
重量級資源(對象占用內(nèi)存多就是重量級資源。)
1.ApplicationContext工廠的對象占用大量內(nèi)存(指的是下面的實現(xiàn)類)
2.不會頻繁的創(chuàng)建對象:一個應(yīng)用程序只會創(chuàng)建一個工廠對象
3.ApplicationContext工廠:一定是線程安全的(多線程并發(fā)訪問)
-
4.程序開發(fā)
spring開發(fā)的4個步驟
1.創(chuàng)建類型
2.配置文件的配置 applicationContext.xml
<bean id="person" class="com.baizhi.basic.Person"/>
3.通過工廠類 獲得對象
ApplicationContext | ClassPathXmlApplicationContext
//1.獲取Spring的工廠
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
//2 通過工廠類獲取對象
Person person = (Person) context.getBean("person");
5.細節(jié)分析
-
名詞解釋
Spring工廠創(chuàng)建的對象叫做bean
-
Spring工廠的相關(guān)方法
Person person = context.getBean("person", Person.class); System.out.println("person = " + person); 當(dāng)前Spring的配置文件中只能有一個<bean class是Person類型 Person person = context.getBean(Person.class); System.out.println("person = " + person); 獲取的是 Spring工廠配置文件中所有bean標(biāo)簽的id值 person person1... String[] beanDefinitionNames = context.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { System.out.println(beanDefinitionName); } //根據(jù)類型獲取Spring配置文件中對應(yīng)的id值 String[] beanNamesForType = context.getBeanNamesForType(Person.class); for (String s : beanNamesForType) { System.out.println(s); } //用于判斷是否存在指定id值的bean System.out.println(context.containsBeanDefinition("person")); //用于判斷是否存在指定id值的bean System.out.println(context.containsBean("person"));
-
配置文件
1.只配置class屬性
上述這種配置 有默認(rèn)id值:com.baizhi.basic.Person#0
應(yīng)用場景:如果這個bean只需要使用一次,那么就可以省略id
如果這個bean使用多次,則需要設(shè)置id值
2.name屬性
作用:用于在Spring的配置文件中,為bean對象定義別名
相同:
context.getBean("name|id");
區(qū)別:
1.別名可以定義多個,但是id屬性只能有一個值
2.XML的id屬性的值,命名要求:必須以字母開頭,字母 數(shù)字 下劃線 連字符 不能以特殊字符開頭
name屬性的值,命名沒有要求 命名靈活
XML發(fā)展到了今天:id屬性的限制不存在了
3.代碼
//用于判斷是否存在指定id值的bean,不能判斷name值 System.out.println(context.containsBeanDefinition("p")); //用于判斷是否存在指定id值以及name值的bean System.out.println(context.containsBean("p"));
6.Spring工廠的底層實現(xiàn)原理(簡易版)
“Spring工廠是可以調(diào)用對象私有的構(gòu)造方法創(chuàng)建對象” 這就是比 new 創(chuàng)建對象 強大的地方
Spring的運行原理/機制
7.思考
問題:未來在開發(fā)過程中,是不是所有的對象,都會交給Spring工廠來創(chuàng)建呢?
回答:理論上 是的,但是有特例:實體對象(entity)是不會交給Spring創(chuàng)建的,它是由持久層框架進行創(chuàng)建,因為它需要數(shù)據(jù),數(shù)據(jù)來源于數(shù)據(jù)庫,而Spring沒有數(shù)據(jù)。
第三章、5.x與日志框架的整合
Spring與日志框架進行整合,日志框架就可以在控制臺中,輸出Spring框架運行過程中的一些重要的信息
好處:便于了解Spring框架的運行過程,有利于程序的調(diào)試
-
Spring如何整合日志框架
默認(rèn)
Spring1,2,3早期都是于commons-logging.jar 整合的日志框架
Spring5.x默認(rèn)整合的日志框架 logback log4j2
Spring5.x整合log4j
1.引入log4j jar包
2.引入log4.properties配置文件
-
pom
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
-
log4j.properties文章來源:http://www.zghlxwxcb.cn/news/detail-749998.html
# resources文件夾根目錄下 ## 配置根 log4j.rootLogger = debug,console ### 日志輸出到控制臺顯示 log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss) %-5p %c{1}:%L - %m%n
-
下一章:Set注入詳解和構(gòu)造注入文章來源地址http://www.zghlxwxcb.cn/news/detail-749998.html
到了這里,關(guān)于Spring5學(xué)習(xí)隨筆-Spring5的第一個程序(環(huán)境搭建、日志框架整合)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!