1. 为什么学习spring?
随着对java ee的不断接触和理解,你会发现spring 在各个企业和项目中发挥着越来越重要的作用。掌握spring 已成为我们it行业生存必学的本领之一。
- spring framework 是一个开源的java/java ee全功能栈(full-stack)的应用程序框架,以apache许可证形式发布,也有.net平台上的移植版本。
- 该框架基于 expert one-on-one java ee design and development(isbn 0-7645-4385-7)一书中的代码,最初由rod johnson和juergen hoeller等开发。
- spring framework提供了一个简易的开发方式,这种开发方式,将避免那些可能致使底层代码变得繁杂混乱的大量的属性文件和帮助类。
spring framework 当前最新版本是spring framework 5,当你打开官网,你应该能够看到官网的宣传图片
这里有个相关的新闻有兴趣可以看下,英文原版地址点击查看中文版点击查看
根据官网动态和我所了解的信息来看,spring 官网还会继续支持spring mvc,因为它还有很多改进的地方。
但是未来的趋势我认为必将是 spring boot+ springweb flux + spring cloud .
那么spring mvc 和 spring web flux 两者有何区别呢?
官网对此给出了这样一张对比图:
翻译下就是:
- spring mvc基于servlet api构建,并使用一个同步阻塞i / o体系结构和一个单线程请求线程模型的web 框架
- spring webflux是一个非阻塞的web框架,从该组建立起,利用多核,下一代处理器和大量并发连接。
总结:
看到这里,相信此时聪明的你应该晓得为什么我之前会那么说了吧。
2. spring 官网介绍
Spring 官网:https://spring.io/ Spring
文档:https://spring.io/guides
Spring IDE:https://spring.io/tools/sts
Spring Project:https://spring.io/projects
项目快速生成器:https://start.spring.io/
上面这些链接相信很多人都知道,但是其实往往我们不是很清楚什么时候用哪个链接。
spring 官网: 关注spring 官网动态,最新的spring 技术和版本发布公告
spring 文档: what you want to do ? 你想开发一个什么样的项目?可以在这里快速找到相关介绍和文档。
spring ide: 如果你打算用eclipse 版本,那么我推荐用 官网这个sts,因为它应该是最友好支持spring的eclipse 版本。当然,如果如果条件可以,我还是强烈推荐你使用intellij idea.
spring project: 这里是按照项目模块划分的,比如 从配置到安全,web应用程序到大数据,想学习哪个就按照分类去学即可。
项目生成器:这里是spring 官网提供的一个非常便利的工具,需要哪些依赖,哪个版本,在这里配置下然后下载即可。
spring framework核心支持依赖注入,事务管理,web应用程序,数据访问,消息传递,测试和更多
tips:这里讲述的是翻译 https://projects.spring.io/spring-framework/ 上面的内容
3.1 介绍
spring框架为现代基于java的企业应用程序提供了一个全面的编程和配置模型 – 在任何类型的部署平台上。
spring的一个关键元素是应用程序级别的基础架构支持:spring着重于企业应用程序的“管道”,以便团队可以专注于应用程序级业务逻辑,而不必与特定部署环境形成不必要的联系。
3.2 功能特点
- 核心技术:依赖注入,事件,资源,i18n,验证,数据绑定,类型转换,spel,aop。
- 测试:模拟对象,testcontext框架,spring mvc测试,webtestclient。
- 数据访问:事务,dao支持,jdbc,orm,编组xml。
- spring mvc和spring webflux web框架
- 整合:远程处理,jms,jca,jmx,电子邮件,任务,调度,缓存。
- 语言:kotlin,groovy,动态语言。
tips:这里加一张官网文档中的一个截图吧,相信有助于你更好地理解。
3.3 最低要求
tips: 所以你的电脑现在推荐使用 jdk1.8+
3.4 快速开始
在项目中开始使用spring-framework的推荐方法是使用依赖管理系统 – 下面的代码片段可以复制并粘贴到您的构建中。
需要帮忙? 请参阅我们有关使用 maven 和 gradle 构建的入门指南。
其实不止spring 官网我们如今的各大公司应该大多数也推荐我们是用maven和gradle 来管理项目jar包依赖。
如果你使用的maven:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid> spring-context</artifactid>
<version>5.1.0.build-snapshot</version>
</dependency>
</dependencies><repositories>
<repository>
<id>spring-snapshots</id>
<name>spring snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
|
如果你是用的是gradle
|
1
2
3
4
5
6
7
|
dependencies {
compile 'org.springframework: spring-context:5.1.0.build-snapshot'
}repositories {
maven {
url 'https://repo.spring.io/libs-snapshot'
}
}
|
tips: 其实我觉得gradle应该是一种比maven更先进的版本依赖管理工具,不过如今各大公司使用gradle 似乎还不是很多,也许是与因为eclipse 对gradle 的支持还不够像intellij idea 那么完美吧。
spring框架包含许多不同的模块。 这里我们展示了提供核心功能的spring-context。 有关其他选项,请参阅右侧的入门指南。
一旦你使用spring-context依赖关系设置你的构建,你就可以做到以下几点:
到这里后官网有些不详细,补充下。
方法一: 使用sts 工具构建这个带有spring-context 上下文的项目
准备工作:
- jdk 1.8
- sts ide
tips: 下载的时候有个坑注意下,如果你的jdk是64位,默认直接下载的sts是32位,会出现这个错误。
所以下载的时候一定要下载jdk匹配的版本才行,移步: https://spring.io/tools/sts/all
这里下载完成后我们在我们的ide空白处,右键——> new——> other…
输入maven 搜索,选择maven project,创建一个maven项目
选择默认的工作空间
选择默认的类型
输入项目基本信息后点击finish 完成
然后我们应该可以看到这样的项目结构
首先修改pom.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<project xmlns="http://maven.apache.org/pom/4.0.0"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>com.xingyun</groupid>
<artifactid>spring-context-sample</artifactid>
<version>0.0.1-snapshot</version>
<packaging>jar</packaging>
<name>spring-context-sample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid> spring-context</artifactid>
<version>5.1.0.build-snapshot</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>spring snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
|
创建文件
hello/messageservice.java
|
1
2
3
4
5
|
package com.xingyun.spring_context_sample.hello;
public interface messageservice {
string getmessage();
}
|
hello/messageprinter.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.xingyun.spring_context_sample.hello;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
@component
public class messageprinter {
final private messageservice service;
@autowired
public messageprinter(messageservice service) {
this.service = service;
}
public void printmessage() {
system.out.println(this.service.getmessage());
}
}
|
tips: 注意下这里有个注解不要忘了
app.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.xingyun.spring_context_sample;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import com.xingyun.spring_context_sample.hello.messageprinter;
import com.xingyun.spring_context_sample.hello.messageservice;
@configuration
@componentscan
public class app
{
@bean
messageservice mockmessageservice() {
return new messageservice() {
public string getmessage() {
return "hello world!";
}
};
}
public static void main( string[] args )
{
applicationcontext context =
new annotationconfigapplicationcontext(app.class);
messageprinter printer = context.getbean(messageprinter.class);
printer.printmessage();
}
}
|
tips: 注意类上有两个注解和方法上有一个注解不要忘了,类的名字你可以改成官网上的application 也可以保留默认的app名字也行。
创建成功后项目结构应该是这样
当然可能你会看着这种项目结构不舒服,那么你也可以选择改变下。
项目结构就变成了这种:
运行app.main() 主方法
项目源码下载: https://github.com/geekxingyun/javaee-framework-sample/tree/master/spring-context-sample
附录:核心jar包依赖关系
tips: 如果你不是写一个java web application,那么将不需要spring-web 模块。
groupid |
artifactid |
description |
|
spring-aop |
proxy-based aop support |
|
|
spring-aspects |
aspectj based aspects |
|
|
spring-beans |
beans support, including groovy |
|
|
spring-context |
application context runtime, including scheduling and remoting abstractions |
|
|
spring-context-support |
support classes for integrating common third-party libraries into a spring application context |
|
|
spring-core |
core utilities, used by many other spring modules |
|
|
spring-expression |
spring expression language (spel) |
|
|
spring-instrument |
instrumentation agent for jvm bootstrapping |
|
|
spring-instrument-tomcat |
instrumentation agent for tomcat |
|
|
spring-jdbc |
jdbc support package, including datasource setup and jdbc access support |
|
|
spring-jms |
jms support package, including helper classes to send/receive jms messages |
|
|
spring-messaging |
support for messaging architectures and protocols |
|
|
spring-orm |
object/relational mapping, including jpa and hibernate support |
|
|
spring-oxm |
object/xml mapping |
|
|
spring-test |
support for unit testing and integration testing spring components |
|
|
spring-tx |
transaction infrastructure, including dao support and jca integration |
|
|
spring-web |
foundational web support, including web client and web-based remoting |
|
|
spring-webmvc |
http-based model-view-controller and rest endpoints for servlet stacks |
|
|
spring-webmvc-portlet |
mvc implementation to be used in a portlet environment |
|
|
spring-websocket |
websocket and sockjs infrastructure, including stomp messaging support |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/xingyunblog/p/6926927.html
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
Laravel执行migrate命令提示:No such file or directory的解决方法
2025-05-29 43 -
2025-05-27 88
-
2025-05-26 65
-
2025-05-26 77
-
2025-05-27 84


















