Java基础之spring5新功能学习

网友投稿 254 2023-01-17

Java基础之spring5新功能学习

一、前言

1.整个 Spring5 框架的代码基于 java8 ,运行时兼容 JDK9,许多不建议使用的类和方 法在代码库中删除

2.Spring 5框架自带了通用的日志封装

Spring5 已经移除 Log4jConfigListener,官方建议使用 Log4j2

二、日志配置

jar包

<!-- 日志 -->

org.apache.logging.log4j

log4j-core

2.14.1

org.apache.logging.log4j

log4j-api

2.14.1

org.apache.logging.log4j

log4j-slf4j-impl

2.14.1

org.slf4j

slf4j-api

1.7.30

log4j2.xml配置文件

VBdQEM

pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />

手动日志输出

public class UserLog {

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

public static void main(String[] args) {

log.info("手动控制日志输出1");

log.warn("手动控制日志输出2");

System.out.println("测试日志");

}

}

如果是maven开发,test,这个需要注释掉

三、核心容器 支持@Nullable

@Nullable 注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空

1.注解用在方法上面,方法返回值可以为空

2.注解使用在方法参数里面,方法参数可以为空

3.注解使用在属性上面,属性值可以为

四、核心容器支持函数式风格

函数式风格 GenericApplicationContext

//函数式风格创建对象,交给 spring 进行管理

@Test

public void test4() {

//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);

}

五、支持整合 JUnit5

1.整合JUnit4

jar包

org.springframework

spring-test

5.3.6

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;

import cn.zj.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:bean1.xml") // 加载配置文件

public class JTest4 {

@Autowired

private UserService userService;

@Test

public void test1() {

userService.accountMoney();

}

}

2.整合JUnit5

jar包引入

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import cn.zj.service.UserService;

//@ExtendWith(SpringExtension.class)

//@ContextConfiguration("classpath:bean1.xml")

@SpringJUnitConfig(locations="classpath:bean1.xml")

//复合注解替代上面两个注解完成整合

public class JTest5 {

@Autowired

private UserService userService;

@Test

public void test1() {

userService.accountMoney();

}

}

pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />

手动日志输出

public class UserLog {

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

public static void main(String[] args) {

log.info("手动控制日志输出1");

log.warn("手动控制日志输出2");

System.out.println("测试日志");

}

}

如果是maven开发,test,这个需要注释掉

三、核心容器 支持@Nullable

@Nullable 注解可以使用在方法上面,属性上面,参数上面,表示方法返回可以为空,属性值可以为空,参数值可以为空

1.注解用在方法上面,方法返回值可以为空

2.注解使用在方法参数里面,方法参数可以为空

3.注解使用在属性上面,属性值可以为

四、核心容器支持函数式风格

函数式风格 GenericApplicationContext

//函数式风格创建对象,交给 spring 进行管理

@Test

public void test4() {

//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);

}

五、支持整合 JUnit5

1.整合JUnit4

jar包

org.springframework

spring-test

5.3.6

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;

import cn.zj.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:bean1.xml") // 加载配置文件

public class JTest4 {

@Autowired

private UserService userService;

@Test

public void test1() {

userService.accountMoney();

}

}

2.整合JUnit5

jar包引入

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import cn.zj.service.UserService;

//@ExtendWith(SpringExtension.class)

//@ContextConfiguration("classpath:bean1.xml")

@SpringJUnitConfig(locations="classpath:bean1.xml")

//复合注解替代上面两个注解完成整合

public class JTest5 {

@Autowired

private UserService userService;

@Test

public void test1() {

userService.accountMoney();

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基础之JDBC的数据库连接与基本操作
下一篇:JDBC编程的详细步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~