Article Directory
1. Integrated log framework: spring5 integrates Log4j2
Spring5 comes with a common logging framework, but it can also integrate other logging frameworks. Taking Log4j2 as an example, the integration process is as follows:
- Introduce the relevant jar package:
log4j-api-2.11.2.jar、log4j-core-2.11.2.jar、log4j-slf4j-impl-2.11.2.jar、slf4j-api-1.7.30.jar。
- Create a log4j.xml configuration file in the src directory:
<?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>
After this configuration, the corresponding information will be output on the console when running the program.
- You can also create a special log class to perform some operations:
package com.wang;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserLog {
private static final Logger log= LoggerFactory.getLogger(UserLog.class);
public static void main(String[] arg){
log.info("hello log4j2");
log.warn("hello log4j2");
}
}
2.@Nullable annotation
@Nullable annotation can be used on methods (allowing to return a null value), attributes (attribute values can be empty), and parameters (parameter values can be empty).
3. Functional Registration Object
I wrote a method in the test class to demonstrate this process:
//函数式风格创建对象,交给spring管理
@Test
public void testFunction(){
//1创建GenericApplicationContext对象
GenericApplicationContext context=new GenericApplicationContext();
//2注册对象
context.refresh();
context.registerBean(User.class,()->new User());
//3获取对象
User user=(User)context.getBean("com.wang.entity.User");
System.out.println(user);
}
4. Integrate the JUnit unit testing framework
(1)Integrate JUnit4
- Introduce the jar package: spring-test-5.2.6.RELEASE.jar
- Introduce JUnit4 dependency: when proceeding to the next step, add according to IDEA prompts when using annotations
- Create a test class and complete it through annotations:
package com.wang.test;
import com.wang.entity.Person;
import com.wang.service.PersonService;
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")//加载配置文件
public class JTest4 {
@Autowired
private PersonService personService;
@Test
public void test1(){
Person p1=new Person();Person p2=new Person();
p1.setPerson_id("1");p2.setPerson_id("2");
personService.transfer(p1,p2,100);
}
}
(2)Integrate JUnit5
- Introduce the jar package: spring-test-5.2.6.RELEASE.jar
- Introduce JUnit5 dependency: when proceeding to the next step, add it according to IDEA prompts when using annotations.
- Create a test class and complete it through annotations:
package com.wang.test;
import com.wang.entity.Person;
import com.wang.service.PersonService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:bean.xml")
//上面两个注解可用这一个注解代替@SpringJUnitConfig(locations="classpath:bean.xml")
public class JTest5 {
@Autowired
private PersonService personService;
@Test
public void test1(){
Person p1=new Person();Person p2=new Person();
p1.setPerson_id("1");p2.setPerson_id("2");
personService.transfer(p1,p2,100);
}
}