Java基礎(chǔ)之spring5新功能學(xué)習(xí)
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)部各種詳細(xì)輸出 --><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 作為 默認(rèn)的日志輸出 --> <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 進(jìn)行管理 @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包引入
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)!
相關(guān)文章:
1. JSP中Servlet的Request與Response的用法與區(qū)別2. IntelliJ IDEA刪除類的方法步驟3. vue cli4下環(huán)境變量和模式示例詳解4. Django視圖類型總結(jié)5. Xml簡介_動力節(jié)點Java學(xué)院整理6. Intellij IDEA 關(guān)閉和開啟自動更新的提示?7. Python Selenium XPath根據(jù)文本內(nèi)容查找元素的方法8. python如何編寫類似nmap的掃描工具9. 解析原生JS getComputedStyle10. javascript使用alert實現(xiàn)一個精美的彈窗
