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

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

Java基礎(chǔ)之spring5新功能學習

瀏覽:121日期: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è)置, 當設(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ù)式風格

函數(shù)式風格 GenericApplicationContext

//函數(shù)式風格創(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新功能學習

Java基礎(chǔ)之spring5新功能學習

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')//復合注解替代上面兩個注解完成整合public class JTest5 {@Autowired private UserService userService; @Test public void test1() {userService.accountMoney(); }}

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

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产乱码 | 国产精品亚洲综合天堂夜夜 | 国产精品福利网站 | 国产免费女同性视频网站 | 中文字幕成人在线 | 欧美国产精品一区二区免费 | 久久精彩 | 被窝福利无限 | 免费视频观看在线www日本 | 黄色草逼网站 | 538prom精品视频在放免费 | 中国明星一级毛片免费 | 女人一级片 | 午夜激情在线观看 | 日韩欧美中字 | 日韩一区二区国色天香 | 欧美精品一区二区在线观看 | 一级a级国产不卡毛片 | 亚洲综合精品成人 | 欧美爱爱小视频 | 精品在线91 | 久草在线视频网站 | 日本精品久久久久中文字幕2 | 久草经典视频 | 日本人爱爱视频 | 国产区综合另类亚洲欧美 | 色婷婷激情 | 91亚洲国产 | 日本一区二区三区在线观看视频 | 北岛玲日韩精品一区二区三区 | 在线观看二区三区午夜 | 亚洲欧美精品综合中文字幕 | 亚洲国产天堂在线网址 | 毛片一区二区三区 | 亚洲高清一区二区三区久久 | 深夜爽爽福利gif在线观看 | 欧美一区二区三区在线观看不卡 | 国产日韩片 | 久久er热这里只有精品23 | 韩国19禁主播裸免费福利 | 国产成人综合95精品视频免费 |