一、整合Log4j2
1)引入log4j2相关jar包
2)配置log4j2的相关信息
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: 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>
二、手动日志输出
package com.xhu.aop.test;
import com.xhu.aop.proxy.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP {
private final Logger LOGGER = LoggerFactory.getLogger(TestAOP.class);
@Test
public void testLog() {
LOGGER.info("hello log!");
LOGGER.warn("hello log4j2!");
}
}
三、给Spring注册对象
@Test
public void testGenericApplicationContext() {
//获取注册对象类
GenericApplicationContext gac = new GenericApplicationContext();
//注册对象
gac.refresh();
gac.registerBean(User.class, () -> new User());
//获取注册的对象
User bean = (User)gac.getBean("com.xhu.aop.User");
//方式2
gac.registerBean("user",User.class,()->new User());
User bean2 = (User)gac.getBean("user");
}
四、Spring整合JUnit5
1、整合JUnit4
package com.xhu.aop.test;
import com.xhu.aop.ServiceImpl;
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;
@RunWith(SpringJUnit4ClassRunner.class)//知道单元测试框架版本
@ContextConfiguration("classpath:bean.xml")//加载bean.xml
public class TestJunit4 {
@Autowired
private ServiceImpl serviceImpl;
@Test
public void test1(){
serviceImpl.toString();
}
}
2、整合JUnit5
package com.xhu.aop.test;
import com.xhu.aop.ServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/*@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean.xml")*/
@SpringJUnitConfig(locations = "classpath:bean.xml")
public class TestJUnit5 {
@Autowired
private ServiceImpl serviceImpl;
@Test
public void test() {
serviceImpl.toString();
}
}