织梦如何修改网站内容,网站做产品的审核工作,佛山百度提升优化,自己的电脑做网站文章目录 前言一、整合日志功能二、Nullable注解三、函数式风格编程四、JUnit5单元测试框架总结 前言
整合日志、Nullable注解、函数式风格编程、整合JUnit5、Webflux 一、整合日志功能
Spring5移除了Log4jConfigListener#xff0c;官方建议使用Log4j2. 依赖#xff1a; Nullable注解三、函数式风格编程四、JUnit5单元测试框架总结 前言
整合日志、Nullable注解、函数式风格编程、整合JUnit5、Webflux 一、整合日志功能
Spring5移除了Log4jConfigListener官方建议使用Log4j2. 依赖 dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-api/artifactIdversion2.17.1/version/dependencydependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-core/artifactIdversion2.17.1/version/dependencydependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-slf4j-impl/artifactIdversion2.17.1/version
!-- scopetest/scope--/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-api/artifactIdversion1.7.30/version/dependencylog4j2.xml文件
?xml version1.0 encodingUTF-8?
!--日志级别以及优先级排序: OFF FATAL ERROR WARN INFO DEBUG TRACE ALL --
!--Configuration后面的status这个用于设置log4j2自身内部的信息输出可以不设置当设置成trace时你会看到log4j2内部各种详细输出--
configuration statusDEBUG!--先定义所有的appender--appenders!--这个输出控制台的配置--console nameConsole targetSYSTEM_OUT!--输出日志的格式--PatternLayout pattern%d{yyyy-MM-dd HH:mm:ss.SSS} 【%t】 %-5level %logger{36} - %msg%n//console/appenders!--然后定义logger只有定义了logger并引入的appenderappender才会生效--!--root用于指定项目的根日志如果没有单独指定Logger则会使用root作为默认的日志输出--loggersroot levelinfoappender-ref refConsole//root/loggers
/configuration然后写一个主函数运行就发现日志输出变了就会按照上面输出日志格式的设定。 configuration 标签内的属性statusDEBUG,属性值可以改成OFF FATAL ERROR WARN INFO DEBUG TRACE ALL任意一个这里是优先级顺序。
二、Nullable注解
Nullable注解可以使用在方法上面属性上面参数上面表示方法可以返回为空属性值可以为空参数值可以为空。 1注解使用在方法上面方法返回值可以为空。
Nullable
String getId();2注解使用在方法参数里方法参数可以为空。
public T void select(Nullable String name,int id){........return T;
}3注解使用在属性上面属性值可以为空
Nullabele
private String bookName;三、函数式风格编程
函数式创建对象交给Spring管理 代码
package com.dragon.xintexing;import com.dragon.spring5.User;
import org.springframework.context.support.GenericApplicationContext;public class test1 {public static void main(String[] args) {//创建GenericApplicationContext对象GenericApplicationContext contextnew GenericApplicationContext();//调用context的方法对象注册context.refresh();context.registerBean(User.class,()-new User());//获取在Spring注册的对象User user(User) context.getBean(com.dragon.spring5.User);System.out.println(user);}
} 四、JUnit5单元测试框架
JUnit5的代码
package com.dragon.xintexing;import com.dragon.shiwu.service.UserService;
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.junit.jupiter.api.Test;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;ExtendWith(SpringExtension.class) //单元测试框架
ContextConfiguration(classpath:bean8.xml)//加载配置文件
//SpringJUnitConfig(locations classpath:bean8.xml)
public class J5test {Autowiredprivate UserService userService;Testpublic void test1(){userService.accountMoney();}
}
上面的注释掉的SpringJUnitConfig注解可以替代它上面的两个注解使用是复合注解。
JUnit4的代码
package com.dragon.xintexing;import com.dragon.shiwu.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;
RunWith(SpringJUnit4ClassRunner.class)//单元测试框架
ContextConfiguration(classpath:bean8.xml)//加载配置文件
public class Jtest {Autowiredprivate UserService userService;Testpublic void test1(){userService.accountMoney();}
}
大家可以自行对比一下 这里再补充一下bean8.xml代码 大家不需要太注意我的配置文件主要给你们模拟测试参考用的。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdcontext:property-placeholder locationclasspath:jdbc.properties/bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${pro.driverClass}/propertyproperty nameurl value${pro.url}/propertyproperty nameusername value${pro.username}/propertyproperty namepassword value${pro.password}/property/beanbean idjdbcTemplate classorg.springframework.jdbc.core.JdbcTemplateproperty namedataSource refdataSource/property/beancontext:component-scan base-packagecom.dragon.shiwu/context:component-scanbean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource/property/bean!-- 开启事务注解--tx:annotation-driven transaction-managertransactionManager/tx:annotation-driven
/beans总结
以上就是Spring5新功能的讲解,Webflu还未讲解后面我会出一篇文章专门讲诉。