亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Java基礎(chǔ)之spring5新功能學(xué)習(xí)

瀏覽:92日期:2022-08-13 10:36:52
一、前言

1.整個 Spring5 框架的代碼基于 Java8 ,運行時兼容 JDK9,許多不建議使用的類和方 法在代碼庫中刪除

2.Spring 5框架自帶了通用的日志封裝

Spring5 已經(jīng)移除 Log4jConfigListener,官方建議使用 Log4j2

二、日志配置

jar包

<!-- 日志 --><!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --><dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --><dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl --><dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.14.1</version> <!--<scope>test</scope>--></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version></dependency>

log4j2.xml配置文件

<?xml version= '1.0' encoding= 'UTF-8' ?><!--日志級別以及優(yōu)先級排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --><!--Configuration 后面的 status 用于設(shè)置 log4j2 自身內(nèi)部的信息輸出,可以不設(shè)置, 當(dāng)設(shè)置成 trace 時,可以看到 log4j2 內(nèi)部各種詳細輸出 --><configuration status='INFO'> <!--先定義所有的 appender --> <appenders><!--輸出日志信息到控制臺 --><console name='Console' target='SYSTEM_OUT'> <!--控制日志輸出的格式 --> <PatternLayoutpattern='%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n' /></console> </appenders> <!--然后定義 logger,只有定義 logger 并引入的 appender,appender 才會生效 --> <!--root:用于指定項目的根日志,如果沒有單獨指定 Logger,則會使用 root 作為 默認的日志輸出 --> <loggers><root level='info'> <appender-ref ref='Console' /></root> </loggers></configuration>

手動日志輸出

public class UserLog { private static final Logger log=LoggerFactory.getLogger(UserLog.class);public static void main(String[] args) {log.info('手動控制日志輸出1');log.warn('手動控制日志輸出2');System.out.println('測試日志'); }}

如果是maven開發(fā),test,這個需要注釋掉

三、核心容器 支持@Nullable

@Nullable 注解可以使用在方法上面,屬性上面,參數(shù)上面,表示方法返回可以為空,屬性值可以為空,參數(shù)值可以為空

1.注解用在方法上面,方法返回值可以為空

2.注解使用在方法參數(shù)里面,方法參數(shù)可以為空

3.注解使用在屬性上面,屬性值可以為

四、核心容器支持函數(shù)式風(fēng)格

函數(shù)式風(fēng)格 GenericApplicationContext

//函數(shù)式風(fēng)格創(chuàng)建對象,交給 spring 進行管理 @Test public void test4() {//1 創(chuàng)建 GenericApplicationContext 對象GenericApplicationContext context = new GenericApplicationContext();//2 調(diào)用 context 的方法對象注冊context.refresh();context.registerBean( 'user1',User. class,() -> new User());//3 獲取在 spring 注冊的對象// User user = (User)context.getBean('com.atguigu.spring5.test.User');User user = (User)context.getBean( 'user1');System. out .println(user); }五、支持整合 JUnit5

1.整合JUnit4

jar包

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.6</version><!-- <scope>test</scope> --></dependency>import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.zj.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration('classpath:bean1.xml') // 加載配置文件public class JTest4 { @Autowired private UserService userService; @Test public void test1() {userService.accountMoney(); }}

2.整合JUnit5

jar包引入

Java基礎(chǔ)之spring5新功能學(xué)習(xí)

Java基礎(chǔ)之spring5新功能學(xué)習(xí)

import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;import cn.zj.service.UserService;//@ExtendWith(SpringExtension.class)//@ContextConfiguration('classpath:bean1.xml')@SpringJUnitConfig(locations='classpath:bean1.xml')//復(fù)合注解替代上面兩個注解完成整合public class JTest5 {@Autowired private UserService userService; @Test public void test1() {userService.accountMoney(); }}

到此這篇關(guān)于Java基礎(chǔ)之spring5新功能學(xué)習(xí)的文章就介紹到這了,更多相關(guān)spring5新功能內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 性爽交免费视频 | 亚洲高清国产品国语在线观看 | 可以免费看黄的网站 | 日韩第二页 | 日韩欧美亚州 | 亚洲一区中文字幕 | 国产精品亚洲一区在线播放 | 性色生活免费看性大片 | 寡妇一级a毛片免费播放 | 91中文在线 | 黄色网在线播放 | 国产精品免费一级在线观看 | 综合久久网 | 欧州一级毛片 | 国产一区二区三区毛片 | 国产精品入口麻豆高清 | 久久婷婷色综合老司机 | 成人三级图片 | 国产美腿丝袜福利视频在线观看 | 天天摸天天碰色综合网 | 欧美成人片在线 | 人人九九精品 | 欧美一级高清片在线 | 精品亚洲一区二区在线播放 | 免费在线看黄的网站 | 国产成人精品一区二区不卡 | 看美国毛片| 亚洲美女亚洲精品久久久久 | 亚洲丁香 | a级毛片免费全部播放 | 制服丝袜在线播放 | 午夜性片 | 亚洲国产另类久久久精品小说 | 美国毛片一级视频在线aa | 一区二区三区视频观看 | 国产视频黄 | 色综合久久九月婷婷色综合 | 黄色片网站免费 | 免费看一毛一级毛片视频 | 99在线国内精品自产拍 | 免费在线观看中文字幕 |