Spring 7: New features of the Spring 5 framework

Posted by palito on Tue, 28 Dec 2021 06:51:11 +0100

1. New Spring5 Framework Functions

1. Version Compatibility

The code for the entire Spring5 framework is Java8-based, runtime-compatible with JDK9, and many unsupported classes and methods are removed from the code base.

2. Log Encapsulation

The Spring5 framework comes with a generic log encapsulation

(1) Spring 5 has removed Log4jConfigListener, which is officially recommended
(2) Spring5 Framework Integration Log4j2

1. Introducing log4j2 and slf4j

    <!-- log4j2 integration slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.14.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.14.1</version>
    </dependency>

(2) Create log4j2.xml configuration file

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Log level and priority ordering:OFF < FATAL < ERROR < WARN < INFO < DEBUG < TRACE < ALL Higher priority results in more output-->
<!-- Configurantion Hinder status For setting up log4j2 Internal information output can be set when set to trace When you see the log4j2 Various internal verbose outputs -->
<configuration status="DEBUG ">
    <!-- Define all first appender -->
    <appenders>
        <!-- Output log information to console -->
        <console name="Console" target="SYSTEM_OUT">
            <!-- Controlling the format of log output -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </console>
    </appenders>
    <!-- Then define logger,Only defined logger And introduced appender,appender Will take effect -->
    <!-- root:Root log for the specified project, if not specified separately Logger,Will use root As default log output -->
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

(3) Test Classes

public class UserLog {

    private static final Logger log = LoggerFactory.getLogger(UserLog.class);

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

(4) Results

19:25:32.010 [main] INFO  UserLog - hello log4j2
19:25:32.012 [main] INFO  UserLog - hello log4j2

3. @Nullable comment

Spring5 Framework Core Container Support @Nullable Annotation

Scope of useEffect
Method aboveRepresentation method return value can be null
Above AttributesIndicates that an attribute value can be null
Above ParametersIndicates that the parameter value can be null

4. Functional Style Creating Objects

Spring5 Core Container supports Functional Style Creation Object GenericApplicationContext

     //Functional Style
    @org.junit.Test
    public void testGenericApplicationContext(){
        //1. Create GenericApplicationContext object
        GenericApplicationContext context = new GenericApplicationContext();
        //2. Call context s'methods for object registration
        context.refresh();
        context.registerBean("user1",User.class,() -> new User());
        //3. Get objects registered with Spring
        User user = (User) context.getBean("user1");
        System.out.println(user);
    }

5. Improvements in testing

Sping5 supports integration of JUnit5
(1) Integrate JUnit4
1. Introduce Spring correlation for test dependency.
(2) Create test classes and complete them with annotations.

@RunWith(SpringJUnit4ClassRunner.class) //Unit Test Framework
@ContextConfiguration("classpath:bean1.xml") //Load Profile
public class Test {

    @Autowired
    private UserService userService;

    @org.junit.Test
    public void JUnit4test(){
        userService.accountMoney(); //The following testAccount() method can be replaced
    }

    @org.junit.Test
    public void testAccount(){ //testAccount() method
        ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }
}

(2) Integrating JUnit5
1. Introduce Spring correlation for test dependency.
(2) Create test classes and complete them with annotations.

@ExtendWith(SpringExtension.class)
@Configuration("classpath:bean1.xml")
public class Test {

    @Autowired
    private UserService userService;

    @org.junit.Test
    public void JUnit5test(){
        userService.accountMoney(); //The following testAccount() method can be replaced
    }

    @org.junit.Test
    public void testAccount(){
        ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }
}

③ @SprinJunitConfig(location="classpath:bean1.cml")
You can replace the above:
@ExtendWith(SpringExtension.class)
@Configuration("classpath:bean1.xml")

6,SpringWebflux

6.1 Introduction to Spring Webflux

(1) Spring5 adds new modules for web development, similar to SpringMVC, which is currently a popular responsive programming framework.
(2) Using traditional web frameworks, such as SpringMVC, which are based on Servlet containers, Webflux is an asynchronous, non-blocking framework that is only supported after Servlet 3.1. The core is based on Reactor's associated API s.

  • Asynchronization and synchronization: For the caller, the caller sends a request, and if he waits for the other party to respond before doing anything else is synchronization; If you do something else after sending it without waiting for the other person to respond, it's asynchronous.
  • Blocking and non-blocking: Blocking occurs when the callee receives a request and gives feedback after completing the requested task. It is not blocking to give feedback immediately after receiving a request and then do something again.

The above are all for different objects

(4) Webflux features:
Asynchronous non-blocking: Improving throughput and scalability of the system with limited resources and implementing responsive programming based on Reactor.
(2) Functional programming: The Spring5 framework is based on Java8, and Webflux implements routing requests programmatically when using Java8 functions.

(5) Compare Spring MVC

Both frameworks can be annotated and run in containers such as Tomcat.
(2) SpringMVC uses command programming and Webflux uses asynchronous response programming.

6.2 Responsive programming

Topics: Java Spring Back-end