Java基礎之spring5新功能學習
一、前言
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 用于設置 log4j2 自身內部的信息輸出,可以不設置, 當設置成 trace 時,可以看到 log4j2 內部各種詳細輸出 --> <configuration status="INFO"> <!--先定義所有的 appender --> <appenders> <!--輸出日志信息到控制臺 --> <console name="Console" target="SYSTEM_OUT"> <!--控制日志輸出的格式 --> <PatternLayout pattern="%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 調用 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包引入
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(); } }
到此這篇關于Java基礎之spring5新功能學習的文章就介紹到這了,更多相關spring5新功能內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java構造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯誤,但不是所有錯誤都是異常,且錯誤有時候是可以避免的,super可以理解為是指向自己超(父)類對象的一個指針,而這個超類指的是離自己最近的一個父類,構造器也叫構造方法、構造函數(shù),是一種特殊類型的方法,負責類中成員變量(域)的初始化2021-10-10Spring在多線程下@Resource注入為null的問題
這篇文章主要介紹了Spring在多線程下@Resource注入為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02