Spring日志打印配置

Spring日志打印配置
pop.xml文件
<!--log4j2 依赖-->
        <!--<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>-->
        <!--slf4-impl 包含了log4j2 依赖-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.14.0</version>
            <scope>test</scope>
        </dependency>
在resources目录下准备log4j2.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>
spring5关于测试工具的支持
整合junit4依赖的jar
<!--Junit4单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <!--spring test测试支持包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.5</version>
            <scope>test</scope>
        </dependency>
测试代码,方法1
package com.msb.test;
import com.msb.config.SpringConfig;
import com.msb.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
@RunWith(SpringJUnit4ClassRunner.class)// 指定测试支持类
@ContextConfiguration("classpath:applicationContext.xml")// 指定核心配置文件位置
public class Test2 {
    @Autowired // 注入要获取的bean
    private  AccountService accountService;
    @Test()
    public void testTransaction(){
        int rows = accountService.transMoney(1, 2, 100);
        System.out.println(rows);
    }
}
使用util5的包
<!--junit5单元测试-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
测试方法
import com.msb.service.AccountService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
// 使用复合注解
@SpringJUnitConfig(locations = "classpath:Application.xml")
public class test {
    @Autowired
    private AccountService accountService;
    @Test//这里用的junit5单元测试
    public void test1(){
        /*ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");
        AccountService accountService = context.getBean(AccountService.class);*/
        int rows = accountService.transMoney(1, 2, 100);
        System.out.println(rows);
    }
}
posted @ 2022-06-17 17:29  爱豆技术部  阅读(488)  评论(0)    收藏  举报
TOP