最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年05月08日 08:36:09   作者:不善言談?wù)? 
這篇文章主要介紹了Java基礎(chǔ)之spring5新功能學(xué)習(xí),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下

一、前言

1.整個(gè) Spring5 框架的代碼基于 Java8 ,運(yùn)行時(shí)兼容 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" ?>
<!--日志級(jí)別以及優(yōu)先級(jí)排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration 后面的 status 用于設(shè)置 log4j2 自身內(nèi)部的信息輸出,可以不設(shè)置, 當(dāng)設(shè)置成 trace 時(shí),可以看到 
    log4j2 內(nèi)部各種詳細(xì)輸出 -->
<configuration status="INFO">
    <!--先定義所有的 appender -->
    <appenders>
        <!--輸出日志信息到控制臺(tái) -->
        <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:用于指定項(xiàng)目的根日志,如果沒有單獨(dú)指定 Logger,則會(huì)使用 root 作為 默認(rèn)的日志輸出 -->
    <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("測(cè)試日志");
    }
}

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

三、核心容器 支持@Nullable

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

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

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

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

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

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

//函數(shù)式風(fēng)格創(chuàng)建對(duì)象,交給 spring 進(jìn)行管理
    @Test
    public void test4() {
        //1 創(chuàng)建 GenericApplicationContext 對(duì)象
        GenericApplicationContext context =  new GenericApplicationContext();
        //2 調(diào)用 context 的方法對(duì)象注冊(cè)
        context.refresh();
        context.registerBean( "user1",User. class,() ->  new User());
        //3 獲取在 spring 注冊(cè)的對(duì)象
        // 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包引入

image.png

image.png

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ù)合注解替代上面兩個(gè)注解完成整合
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

赫章县| 黔西县| 资讯 | 广河县| 芜湖县| 玛曲县| 威信县| 怀柔区| 文成县| 闵行区| 兰溪市| 五莲县| 安乡县| 河源市| 攀枝花市| 和平区| 林周县| 县级市| 蒲城县| 西昌市| 罗田县| 沂南县| 墨玉县| 聂荣县| 左云县| 贡觉县| 汉中市| 天津市| 普兰店市| 三门县| 社会| 邢台县| 丹寨县| 内丘县| 泰来县| 铜川市| 隆回县| 临江市| 方城县| 古浪县| 鲜城|