Spring 5 framework notes summary

1. New features of spring 5 framework

The code of the whole spring 5 framework is based on Java 8, and the runtime is compatible with JDK9. Many classes and methods that are not recommended are deleted from the code base. (new feature 1)

2. Spring 5 integrates Log4j2 Version (new feature 2)

The Spring 5 framework comes with a general logging package, and can also integrate other logging tools, such as log4j2 logging.

  • The Log4jConfigListener has been removed from Spring 5. It is officially recommended to use the Log4j2 version. Therefore, if you use version 1 of log4j in Spring 5, you need to reduce the Spring version to version 4!
  • Therefore, let's integrate the Log4j2 version of spring 5.

Note: spring 4 and earlier versions can directly use log4j, but spring 5 can only use log4j2. Remember!

Spring 5 framework integration Log4j2

Step 1: import the jar package.

Step 2: create log4j2 XML configuration file. Note that the name is fixed! (lowercase log4j.xml file).

<?xml version="1.0" encoding="UTF-8"?>

<!--The log level and priority size are: OFF < FATAL < ERROR < WARN < INFO < DEBUG < TRACE < ALL  , It means that the more displayed to the right, the higher the priority.-->
<!--For example: set to DEBUG,Then the display will show DEBUG And ratio DEBUG Low priority(ERROR < WARN < INFO < DEBUG)Information about.-->
<!--Configuration hinder status Used to set log4j2 The internal information output can not be set when it is set to trace When, you can see log4j2 Various internal detailed outputs-->
<configuration status="DEBUG">

    <!--Define all first appender-->
    <appenders>
        <!--Output log information to console-->
        <console name="Console" target="SYSTEM_OUT">
            <!--Controls the format of log output-->
            <PatternLayout charset="GBK" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </console>
    </appenders>

    <!--Then define loggers,Only defined logger,And introduce appender,appender Will take effect-->
    <loggers>
        <!--root: Used to specify the root log of the project, if not specified separately Logger,Will be used root As default log output-->
        <root level="debug">  <!--Case is OK -->
            <AppenderRef ref="Console"/>
        </root>
    </loggers>
</configuration>

Another way is through the logger class in the log4j-slf4j-impl package:

Logger log = LoggerFactory.getLogger(UserLog.class);

Let's create a UserLog class to try:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserLog {

    //Note that the logger here is org Slf4j's logger, don't confuse!
    private static final Logger log = LoggerFactory.getLogger(UserLog.class);

    public static void main(String[] args) {
        log.info("hello log4j2 ~ info");
        log.warn("hello log4j2 ~ warn");
    }

}

The effect is as follows: it will also be printed!

3. Spring 5 core container supports @ Nullable annotation (new feature 3)

The Spring 5 framework core container supports @ Nullable annotation:

  • @Nullable annotations can be used on methods, properties and parameters, indicating that the method return can be null, the property value can be null and the parameter value can be null.

  • @Nullable annotation: used on a method to indicate that the return value of the method can be null.

  • @Nullable annotation: used on method parameters to indicate that the method parameters can be null.

  • @Nullable annotation: used on the attribute variable table. The attribute variable can be empty.

In the source code, there are many examples of using @ Nullable, as shown in the following figure:

4. Spring 5 core container supports functional style (GenericApplicationContext, new feature 4)

In fact, the so-called functional style is to create objects through function code and submit them to spring for management.

Create a User class and test the class:

package com.itholmes.User;

public class User {
}

Create a test class to realize the effect of creating objects with functional code and submitting them to spring:

package Test;

import com.itholmes.User.User;
import org.junit.Test;
import org.springframework.context.support.GenericApplicationContext;

public class Test4 {
    //The steps of creating objects in functional style and handing them over to spring for management
    @Test
    public void testGenericApplicationContext(){

        //1. Create GenericApplicationContext object
        GenericApplicationContext context = new GenericApplicationContext();

        //2. Call the method object registration of context
        //context.refresh() is used to empty the content of the context.
        context.refresh();
        //() -> new User();  Is a Lambda expression
        context.registerBean(User.class,() -> new User());

        //3. Get the object registered in spring
        //Here, we can't directly get the user with lowercase initials like xml and annotation. We must add the package name, and the corresponding path is as follows:
        User user = (User)context.getBean("com.itholmes.User.User");

        System.out.println(user);

    }

    @Test
    public void testGenericApplicationContext2(){

        GenericApplicationContext context = new GenericApplicationContext();

        context.refresh();
        //We can name him by adding the parameter beanName here, because the source code here is the @ Nullable annotation type, so sometimes the parameter can be empty.
        context.registerBean("user123",User.class,() -> new User());

        User user = (User)context.getBean("user123");

        System.out.println(user);

    }
}

5. Spring 5 supports the integration of JUnit 5 unit test framework (new feature 5)

The Spring 5 framework also supports the integration of JUnit 4, but compared with other Spring version frameworks, there is a test of integrating JUnit 5.

5.1 spring 5 integration JUnit4

Spring 5 integrates JUnit 4:

Step 1: introduce Spring related dependencies for testing.

Step 2: create a test class and complete it with JUint4 annotation.

package Test;

import com.itholmes.service.UserService;
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;

//This step is equivalent to @ RunWith(xxx) or @ RunWith(value=xxx): to specify which version of junit we use. Note that this is in the form of class file.
@RunWith(SpringJUnit4ClassRunner.class)

//This step is equivalent to ApplicationContext = new classpathxmlapplicationcontext ("beans2. XML");
@ContextConfiguration("classpath:beans2.xml")
public class JTest4 {

    //The configuration file is loaded above. We can get it directly here.
    //This step is equivalent to userservice bean = context getBean("userService", UserService.class);
    @Autowired
    private UserService userService;

    @Test
    public void test1(){
        //Here we can call it directly.
        userService.accountMoney();
    }

}

5.2 spring 5 integration JUnit 5

Steps to integrate JUnit 5:

Step 1: introduce the jar package or dependency of JUnit 5.

Step 2: create a test class and complete it with the annotations in JUnit 5.

package Test;

import com.itholmes.service.UserService;

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): a reference to an annotation
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:beans2.xml")
public class JTest5 {

    @Autowired
    private UserService userService;

    @Test//Note that the test here is org junit. jupiter. api. Test of JUnit5 in test
    public void test1(){
        userService.accountMoney();
    }

}

You can also use the compound annotation @ SpringJUnitConfig(locations = "xxx.xml") to compound the following:

package Test;

import com.itholmes.service.UserService;

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;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:beans2.xml")

//@Spring junitconfig (locations = "classpath: beans2. XML") is the composite annotation of the above two annotations

@SpringJUnitConfig(locations = "classpath:beans2.xml")
public class JTest5 {

    @Autowired
    private UserService userService;

    @Test//Note that the test here is org junit. jupiter. api. Test of JUnit5 in test
    public void test1(){
        userService.accountMoney();
    }

}

6. New module spring Webflux of spring 5 framework (new feature 6)

To learn spring Webflux, you must master the new features of Spring MVC, Spring Boot and Java 8, and then add them!

Keywords: Java Spring Back-end log4j2

Added by werkkrew on Tue, 18 Jan 2022 03:45:15 +0200