javascript
Spring5学习随笔-Spring5的第一个程序(环境搭建、日志框架整合)
學習視頻:【孫哥說Spring5:從設計模式到基本應用到應用級底層分析,一次深入淺出的Spring全探索。學不會Spring?只因你未遇見孫哥】
第二章、第一個Spring程序
1.軟件版本
1.JDK1.8+
2.Maven3.5+
3.IDEA2018+
4.SpringFramework 5.1.4
官網:www.spring.io
2.環境搭建
-
Spring的jar包
1.設置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
思考:日后應用Spring框架時,需要進行配置文件路徑的位置。
3.Spring的核心API
-
ApplicationContext
作用:Spring提供的ApplicationContext這個工廠,用于對象的創建
好處:解耦合
-
ApplicationContext接口類型
接口:屏蔽實現的差異
非web環境:ClassPathXmlApplicationContext(例如:main junit)
web環境:XmlWebApplicationContext
可以看到ApplicationContext就是一個接口
-
重量級資源(對象占用內存多就是重量級資源。)
1.ApplicationContext工廠的對象占用大量內存(指的是下面的實現類)
2.不會頻繁的創建對象:一個應用程序只會創建一個工廠對象
3.ApplicationContext工廠:一定是線程安全的(多線程并發訪問)
-
4.程序開發
spring開發的4個步驟
1.創建類型
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.細節分析
-
名此解釋
Spring工廠創建的對象叫做bean
-
Spring工廠的相關方法
Person person = context.getBean("person", Person.class); System.out.println("person = " + person); 當前Spring的配置文件中只能有一個<bean class是Person類型 Person person = context.getBean(Person.class); System.out.println("person = " + person); 獲取的是 Spring工廠配置文件中所有bean標簽的id值 person person1... String[] beanDefinitionNames = context.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { System.out.println(beanDefinitionName); } //根據類型獲取Spring配置文件中對應的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屬性
上述這種配置 有默認id值:com.baizhi.basic.Person#0
應用場景:如果這個bean只需要使用一次,那么就可以省略id
如果這個bean使用多次,則需要設置id值2.name屬性
作用:用于在Spring的配置文件中,為bean對象定義別名
相同:
context.getBean("name|id");區別:
1.別名可以定義多個,但是id屬性只能有一個值
2.XML的id屬性的值,命名要求:必須以字母開頭,字母 數字 下劃線 連字符 不能以特殊字符開頭
name屬性的值,命名沒有要求 命名靈活
XML發展到了今天:id屬性的限制不存在了
3.代碼
//用于判斷是否存在指定id值的bean,不能判斷name值 System.out.println(context.containsBeanDefinition("p")); //用于判斷是否存在指定id值以及name值的bean System.out.println(context.containsBean("p"));
6.Spring工廠的底層實現原理(簡易版)
“Spring工廠是可以調用對象私有的構造方法創建對象” 這就是比 new 創建對象 強大的地方
Spring的運行原理/機制
7.思考
問題:未來在開發過程中,是不是所有的對象,都會交給Spring工廠來創建呢?
回答:理論上 是的,但是有特例:實體對象(entity)是不會交給Spring創建的,它是由持久層框架進行創建,因為它需要數據,數據來源于數據庫,而Spring沒有數據。
第三章、5.x與日志框架的整合
Spring與日志框架進行整合,日志框架就可以在控制臺中,輸出Spring框架運行過程中的一些重要的信息
好處:便于了解Spring框架的運行過程,有利于程序的調試
-
Spring如何整合日志框架
默認
Spring1,2,3早期都是于commons-logging.jar 整合的日志框架
Spring5.x默認整合的日志框架 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
# 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
-
總結
以上是生活随笔為你收集整理的Spring5学习随笔-Spring5的第一个程序(环境搭建、日志框架整合)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS 也能实现 if 判断?实现动态高
- 下一篇: 浏览器事件循环Event Loop