Spring 学习笔记(一):Spring 入门

Stella981
• 阅读 535

1 Spring简介

Spring是一个轻量级Java开发框架,最早由Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,是一个分层的Java SE/EE full-stack轻量级开源框架,为开发Java应用程序提供全面的基础架构支持。

2 Spring体系结构

目前Spring已经集成了20多个模块,分布在以下模块中:

  • Core Container:核心容器
  • Data Access/Integration:数据访问/集成
  • WebWeb模块
  • AOPAspect Oriented Programming,面向切面编程
  • Instrumentation:植入模块
  • Messaging:消息传输
  • Test:测试模块

如图所示:

Spring 学习笔记(一):Spring 入门

2.1 核心容器

Spring-coreSpring-beansSpring-contextSpring-context-supportSpring-expression等模块组成:

  • Spring-core:提供了框架的基本组成部分,包括IocInversion of Control,控制反转)以及DIDependency Injection,依赖注入)功能
  • Spring-beans:提供了BeanFactory,是工厂模式的一个典型实现,**Spring将管理的对象称为Bean**
  • Spring-context:建立在CoreBeans模块的基础上,提供了一个框架式的对象访问方式,是访问定义和配置的任何对象的媒介,ApplicationContext接口是Context模块的焦点
  • Spring-context-support:支持整合第三方库到Spring应用程序上下文,特别是用于高速缓存(EhCacheJCache)和任务调度(CommonJQuartz)的支持
  • Spring-expression:提供了强大的表达式语言去支持运行时查询和操作对象图,是JSP 2.1规定的统一表达式语言的扩展

2.2 AOPInstrumentation

  • Spring-aop:提供了一个符合AOP要求的面向切面的编程实现,允许定义方法拦截器和切入点
  • Spring-aspects:提供了与AspectJ的集成功能,AspectJ是一个功能强大且成熟的AOP框架
  • Spring-instrumentation:提供了类植入支持和类加载器的实现

2.3 消息

Spring 4.0后增加了消息模块,提供了对消息传递体系结构和协议的支持。

2.4 数据访问/集成

数据访问/集成层由JDBCORMOXMJMS和事务模块组成。

  • Spring-JDBC:提供了一个JDBC抽象层,消除了繁琐的JDBC编码和数据库厂商特有的错误代码解析
  • Spring-ORM:为流行的ORMObject-Relational Mapping,对象关系映射)框架提供了支持,包括JPAHibernate,使用Spring-ORM框架可以将这些O/R映射框架与Spring提供的所有其他功能结合使用
  • Spring-OXM:提供了一个支持对象/XML映射的抽象层实现,例如JAXBCastorJiBXXStream
  • Spring-JMSJMSJava Messaging ServiceJava消息传递服务),包含用于生产和使用消息的功能
  • Spring-TX:事务管理模块,支持用于实现特殊接口和所有POJO类的编程和声明式事务管理

2.5 Web

WebSpring-WebSpring-WebMVCSpring-WebSocketPortlet模块组成。

  • Spring-Web:提供了基本的Web开发集成功能,例如多文件上传等
  • Spring-WebMVC:也叫Web-Servlet模块,包含用于Web应用程序的Spring MVCREST Web Services的实现。
  • Spring-WebSocket:提供了WebSocketSockJS的实现
  • Porlet:类似于Servlet模块的功能,提供了Porlet环境下MVC的实现

2.6 测试

Spring-test模块支持使用JUnitTestNGSpring组件进行单元测试和集成测试。

3 环境

  • OpenJDK 11.0.8
  • IDEA 2020.2
  • Maven/Gradle

4 入门DemoJava版)

4.1 新建Maven工程

Spring 学习笔记(一):Spring 入门

Spring 学习笔记(一):Spring 入门

4.2 引入依赖

pom.xml文件加入:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.8.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

可以戳这里查看最新的版本。

4.3 新建文件

新建如下5个空文件:

Spring 学习笔记(一):Spring 入门

4.4 applicationContext.xml

该文件是Spring的配置文件,习惯命名为applicationContext.xml,内容如下:

<?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="test" class="TestImpl"/>
</beans>

这里声明了一个idtest,类为TestImplBean

4.5 TestInterface

public interface TestInterface {
    void hello();
}

4.6 TestImpl

public class TestImpl implements TestInterface {
    @Override
    public void hello() {
        System.out.println("Hello Spring.");
    }
}

4.7 Main

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello");
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ((TestInterface)context.getBean("test")).hello();
    }
}

4.8 Tests

public class Tests {
    @Test
    public void test()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestInterface testInterface = (TestInterface)context.getBean("test");
        testInterface.hello();
    }
}

4.9 运行

4.9.1 测试

直接点击测试类旁边的按钮即可:

Spring 学习笔记(一):Spring 入门

若出现如下错误:

Spring 学习笔记(一):Spring 入门

JDK版本设置错误的问题,先打开Project Structure,修改Modules下的JDK版本:

Spring 学习笔记(一):Spring 入门

下一步是打开设置,修改Comiler下的JDK版本:

Spring 学习笔记(一):Spring 入门

输出:

Spring 学习笔记(一):Spring 入门

4.9.2 Main

默认不能直接运行Main函数,需要添加运行配置:

Spring 学习笔记(一):Spring 入门

选择Application添加配置,并且指定Name以及Main class

Spring 学习笔记(一):Spring 入门

这样就可以运行了:

Spring 学习笔记(一):Spring 入门

5 KotlinDemo

使用Gradle+Kotlin的入门Demo

5.1 新建Gradle工程

Spring 学习笔记(一):Spring 入门

Spring 学习笔记(一):Spring 入门

5.2 build.gradle

完整文件如下:

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.4.0'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework', name: 'spring-context', version: '5.2.8.RELEASE'
    compile group: 'org.springframework', name: 'spring-core', version: '5.2.8.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '5.2.8.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}

除了添加依赖以外还添加了一些其他参数。

5.3 新建文件夹以及文件

Spring 学习笔记(一):Spring 入门

5.4 TestInterface

interface TestInterface {
    fun hello()
}

5.5 TestImpl

class TestImpl:TestInterface
{
    override fun hello() {
        println("Hello Spring")
    }
}

5.6 Main

fun main() {
    println("Hello")
    val context: ApplicationContext = ClassPathXmlApplicationContext("applicationContext.xml")
    val test: TestInterface = context.getBean("test") as TestInterface
    test.hello()
}

5.7 applicationContext.xml

同上。

5.8 Tests

class Tests {
    @Test
    fun test()
    {
        val context:ApplicationContext = ClassPathXmlApplicationContext("applicationContext.xml")
        val test:TestInterface = context.getBean("test") as TestInterface
        test.hello()
    }
}

5.9 运行

5.9.1 测试

Spring 学习笔记(一):Spring 入门

同样直接点击旁边的按钮即可运行:

Spring 学习笔记(一):Spring 入门

5.9.2 Main

同样点击按钮即可:

Spring 学习笔记(一):Spring 入门

Spring 学习笔记(一):Spring 入门

6 源码

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这