国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java基礎之spring5新功能學(xué)習

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:不善言談?wù)?/span> 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

一、前言

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

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 時(shí),可以看到 
    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 才會(huì )生效 -->
    <!--root:用于指定項目的根日志,如果沒(méi)有單獨指定 Logger,則會(huì )使用 root 作為 默認的日志輸出 -->
    <loggers>
        <root level="info">
            <appender-ref ref="Console" />
        </root>
    </loggers>
</configuration>

手動(dòng)日志輸出

public class UserLog {

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

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

三、核心容器 支持@Nullable

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

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

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

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

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

函數式風(fēng)格 GenericApplicationContext

//函數式風(fēng)格創(chuàng  )建對象,交給 spring 進(jìn)行管理
    @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")
//復合注解替代上面兩個(gè)注解完成整合
public class JTest5 {
    
    @Autowired
    private UserService userService;

    @Test
    public void test1() {
        userService.accountMoney();
    }
}

到此這篇關(guān)于Java基礎之spring5新功能學(xué)習的文章就介紹到這了,更多相關(guān)spring5新功能內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

精品多人P群无码| 亚洲AV日韩AV不卡在线观看| 亚洲午夜精品无码专区在线观看| 国产乱人伦偷精品视频AAA| 色吊丝AV中文字幕| 男男暴菊GAY无套网站|