Spring 学习笔记(一):Spring 入门
1 Spring简介
Spring是一个轻量级Java开发框架,最早由Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,是一个分层的Java SE/EE full-stack轻量级开源框架,为开发Java应用程序提供全面的基础架构支持。
2 Spring体系结构
目前Spring已经集成了20多个模块,分布在以下模块中:
Core Container:核心容器Data Access/Integration:数据访问/集成Web:Web模块AOP:Aspect Oriented Programming,面向切面编程Instrumentation:植入模块Messaging:消息传输Test:测试模块
如图所示:

2.1 核心容器
由Spring-core、Spring-beans、Spring-context、Spring-context-support、Spring-expression等模块组成:
Spring-core:提供了框架的基本组成部分,包括Ioc(Inversion of Control,控制反转)以及DI(Dependency Injection,依赖注入)功能Spring-beans:提供了BeanFactory,是工厂模式的一个典型实现,Spring将管理的对象称为BeanSpring-context:建立在Core和Beans模块的基础上,提供了一个框架式的对象访问方式,是访问定义和配置的任何对象的媒介,ApplicationContext接口是Context模块的焦点Spring-context-support:支持整合第三方库到Spring应用程序上下文,特别是用于高速缓存(EhCache、JCache)和任务调度(CommonJ、Quartz)的支持Spring-expression:提供了强大的表达式语言去支持运行时查询和操作对象图,是JSP 2.1规定的统一表达式语言的扩展
2.2 AOP和Instrumentation
Spring-aop:提供了一个符合AOP要求的面向切面的编程实现,允许定义方法拦截器和切入点Spring-aspects:提供了与AspectJ的集成功能,AspectJ是一个功能强大且成熟的AOP框架Spring-instrumentation:提供了类植入支持和类加载器的实现
2.3 消息
Spring 4.0后增加了消息模块,提供了对消息传递体系结构和协议的支持。
2.4 数据访问/集成
数据访问/集成层由JDBC、ORM、OXM、JMS和事务模块组成。
Spring-JDBC:提供了一个JDBC抽象层,消除了繁琐的JDBC编码和数据库厂商特有的错误代码解析Spring-ORM:为流行的ORM(Object-Relational Mapping,对象关系映射)框架提供了支持,包括JPA和Hibernate,使用Spring-ORM框架可以将这些O/R映射框架与Spring提供的所有其他功能结合使用Spring-OXM:提供了一个支持对象/XML映射的抽象层实现,例如JAXB、Castor、JiBX、XStreamSpring-JMS:JMS(Java Messaging Service,Java消息传递服务),包含用于生产和使用消息的功能Spring-TX:事务管理模块,支持用于实现特殊接口和所有POJO类的编程和声明式事务管理
2.5 Web
Web有Spring-Web、Spring-WebMVC、Spring-WebSocket和Portlet模块组成。
Spring-Web:提供了基本的Web开发集成功能,例如多文件上传等Spring-WebMVC:也叫Web-Servlet模块,包含用于Web应用程序的Spring MVC和REST Web Services的实现。Spring-WebSocket:提供了WebSocket和SockJS的实现Porlet:类似于Servlet模块的功能,提供了Porlet环境下MVC的实现
2.6 测试
Spring-test模块支持使用JUnit或TestNG对Spring组件进行单元测试和集成测试。
3 环境
OpenJDK 11.0.8IDEA 2020.2Maven/Gradle
4 入门Demo(Java版)
4.1 新建Maven工程


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个空文件:

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>
这里声明了一个id为test,类为TestImpl的Bean。
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 测试
直接点击测试类旁边的按钮即可:

若出现如下错误:

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

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

输出:

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

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

这样就可以运行了:

5 Kotlin版Demo
使用Gradle+Kotlin的入门Demo。
5.1 新建Gradle工程


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 新建文件夹以及文件

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 测试

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

5.9.2 Main
同样点击按钮即可:


6 源码
Spring 学习笔记(一):Spring 入门的更多相关文章
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java架构师之路 Spring学习笔记(一) Spring介绍
前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- Spring学习笔记之Spring概述
概述 Spring是一个java应用最广的开源框架,它是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Deve ...
- Spring学习笔记之五----Spring MVC
Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入
本节主要内容: 1.实例化Spring容器示例 2.利用Spring容器创建JavaBean对象 3.如何控制Bean实例化 4.利用Spring实现bean属性sett ...
- spring学习笔记之spring框架本质
Spring框架的本质是,开发者在Spring配置文件中使用XML元素进行配置,实际驱动Spring执行相应的代码: 使用<bean.../>元素,实际启动Spring执行无参或有参构造函 ...
- Spring学习笔记2:Spring HelloWorld
1:IntelliJ新建Maven工程 2:pom文件加入Spring依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...
随机推荐
- java线程池趣味事:这不是线程池
要想写出高性能高并发的应用,自然有许多关键,如io,算法,异步,语言特性,操作系统特性,队列,内存,cpu,分布式,网络,数据结构,高性能组件. 胡说一通先. 回到主题,线程池.如果说多线程是提高系统 ...
- vue_webpack
1.生成项目工程描述文件 npm init 2.安装webpack开发依赖 (本地安装):npm install -D 3.(webpack4.0版本以上安装webpack cli) npm inst ...
- 下载com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
看别人都说在repo.maven.com下载,没想到竟然要登录 索性我直接在国内阿里云的镜像仓库下载好了,速度又快又方便 搜索aspectj 下载地址:https://maven.aliyun.com ...
- C#从1970年开始到现在时间的总秒数
TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1)); string timeStamp = ((int)timeSpan. ...
- Django Admin 后台Admin继承UserAdmin增加用户密码不显示明文和用户登录不了的解决方法
Django后台Admin继承UserAdmin增加用户不显示明文方法 1.在 models.py 中用户表 # 导包规范-1.Python标准模块 from django.db import mod ...
- python进阶(9)多线程
什么是线程? 线程也叫轻量级进程,是操作系统能够进行运算调度的最小单位,它被包涵在进程之中,是进程中的实际运作单位.线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其 ...
- Win10下ctrl与alt键互换
我之前尝试过用第三方软件修改,但是总是不成功,后来发现直接去修改注册表也不麻烦,记录一下步骤. win + r 输入 regedit 进到这个路径 点击Keyboard Layout 右键,新建一个 ...
- 《C++ Primer》笔记 第11章 关联容器
关联容器类型 解释 按关键字有序保存元素 -- map 关联数组:保存关键字-值对 set 关键字即值,即只保存关键字的容器 multimap 关键字可重复出现的map multiset 关键字可重复 ...
- (三)String、StringBuilder、StringBuffer在字符串操作中的性能差异浅析
参考资料:https://www.iteye.com/blog/hank4ever-581463 <Core Java Volume I-Fundamentals>原书第十版 <Ja ...
- MarkFormat,一个在Word中使用Mark进行格式化的插件
MarkFormat(标记格式化),是一款在Word中基于标记进行格式化的工具. 让我们看下具体效果. 首先是有标记的文本 点击格式化之后 点击去除标记之后(去除标记也会进行格式化) 如果想要恢复标记 ...