Spring5的基本使用

Spring5的一些变化

  • Spring5.x整个框架已经全面基于Java8及以上版本,所以Spring5最低JDK版本要求是8

  • 由于Java8的反射增强,因此Spring5.x可以对方法的参数进行更高效的访问

  • Spring5.x核心接口已经加入了Java8接口支持的默认方法

  • Spring5.x已经自带了通用的日志封装,不需要再额外使用commons-loging.jar包

  • Spring5.x引入了@Nullable和@NotNull注解来修饰可空的参数及返回值,避免运行时的空指针异常

  • Spring5.x支持JetBrains Kotlin语言,而且新增了对函数式Bean定义的支持,包括函数式的Bean检索样式

  • 取消了相关类、包和方法:

    beans.factory.access

    dbc.support.nativejdbc

    从spring-aspects模块中移除了mock.staicmock包

    不再提供AnnotationDrivenStaticEntityMockingControl支持

官网在解释Spring框架的常用场景的图:

在这里插入图片描述

Spring框架帮我们简化了什么开发工作:

  1. 查询数据:DAO+POJO的初始化和装载的过程被简化

  2. 给所有Service的查询方法记录日志

进入官网:https://spring.io/

  1. 点击Spring Framework
    在这里插入图片描述

  2. 选择Learn

    在这里插入图片描述

    查看当前最新的GA版本(稳定版本)

    GA:正式发布的稳定版本
    PRE:预览版本,可能存在很多漏洞
    SNAPSHOT:快照版本

  3. 点击GitHub头像
    在这里插入图片描述

  4. 找到Access to Binaries选择Spring Framework Artifacts
    在这里插入图片描述

  5. 进入Spring Framework Artifacts页面后,找到Downloading a Distribution,进入链接

    https://repo.spring.io
    在这里插入图片描述

  6. 进入后点击Artifacts,选择release

    在这里插入图片描述

  7. 依次找如下路径release/org/springframework/spring/

    在这里插入图片描述

  8. 点击URL to file的链接

    在这里插入图片描述

  9. 选取最新版本

    在这里插入图片描述

  10. 下载dist.zip

    在这里插入图片描述

    dist:源码包(包含了docs和scheme)
    docs:官方文档
    scheme:配置文件
    

    在这里插入图片描述

Spring5需要的主要的Jar包

spring-beans-5.3.9.jar
spring-core-5.3.9.jar
spring-expression-5.3.9.jar
spring-context-5.3.9.jar

注意事项:如果只是引入这几个包,还需要额外引入一个外部包commons-logging-1.2.jar,Spring5.x版本需要导入spring-jcl-5.3.9.jar
测试代码

import com.carl.entity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @PackageName PACKAGE_NAME
 * @Auther carl
 * @Description: 测试
 * @Version V1.0
 * @Date 2022-09-24 11:08
 * Modified By:chihuaxin
 **/
public class Main {
    @Test
    public void testUser(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
//        ApplicationContext context = new FileSystemXmlApplicationContext("E:\\project\\studyProject\\JavaEE\\Spring\\introduction\\Demo1\\resource\\bean1.xml");
//        //FileSystemXmlApplicationContext()默认是在当前项目下(E:\project\studyProject\JavaEE\Spring\introduction\Demo1)
//        ApplicationContext context = new FileSystemXmlApplicationContext("resource\\bean1.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.carl.entity.User"/>
</beans>

SpringDemo

  • 20
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Carl·杰尼龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值