Spring5 new feature-integrated logging framework @Nullable annotated functional

1. The code of the entire Spring5 framework is based on Java8 and is compatible with JDK9 at runtime. Many classes and methods that are not recommended are deleted from the code base.
2. The Spring 5.0 framework comes with a common log package
(1) Spring5 has removed Log4jConfigListener, the official It is recommended to use Log4j2
(2) Spring5 framework to integrate Log4j2. The first step is to introduce the jar package

1. Log package

The first step is to introduce the jar package

Insert picture description here

The second step is to create the log4j2.xml configuration file

Insert picture description here

<?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>

Test Results

Insert picture description here

2. @Nullable annotation

(1) The @Nullable annotation can be used on methods, attributes, and parameters, indicating that the method return can be empty, the attribute value can be empty, and the parameter value can be empty
(2) The annotation is used on the method, and the method return value can be air
Insert picture description here

(3) Annotations are used in method parameters, which can be empty
Insert picture description here

(4) Annotation is used on the attribute, the attribute value can be empty

//函数式风格创建对象,交给spring进行管理 @Test public void testGenericApplicationContext() { //1 创建GenericApplicationContext对象 GenericApplicationContext context = new GenericApplicationContext(); //2 调用context的方法对象注册 context.refresh(); context.registerBean("user1",User.class,() -> new User()); //3 获取在spring注册的对象
// User user = (User)context.getBean("com.atguigu.spring5.test.User"); User user = (User)context.getBean("user1"); System.out.println(user);
}

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/107442118