随着对Java EE的不断接触和理解,你会发现Spring 在各个企业和项目中发挥着愈来愈重要的做用。掌握Spring 已成为咱们IT行业生存必学的本领之一。html
- 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,当你打开官网,你应该可以看到官网的宣传图片java
这里有个相关的新闻有兴趣能够看下,英文原版地址点击查看 中文版点击查看react
根据官网动态和我所了解的信息来看,Spring 官网还会继续支持Spring MVC,由于它还有不少改进的地方。git
可是将来的趋势我认为必将是 Spring Boot+ SpringWeb Flux + Spring Cloud.github
那么Spring MVC 和 Spring Web Flux 二者有何区别呢?web
官网对此给出了这样一张对比图:spring
翻译下就是:express
- Spring MVC基于servlet API构建,并使用一个同步阻塞I / O体系结构和一个单线程请求线程模型的Web 框架
- Spring WebFlux是一个非阻塞的Web框架,从该组创建起,利用多核,下一代处理器和大量并发链接。
总结:apache
看到这里,相信此时聪明的你应该晓得为何我以前会那么说了吧。编程
上面这些连接相信不少人都知道,可是其实每每咱们不是很清楚何时用哪一个连接。
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/ 上面的内容
Spring框架为现代基于Java的企业应用程序提供了一个全面的编程和配置模型 - 在任何类型的部署平台上。
Spring的一个关键元素是应用程序级别的基础架构支持:Spring着重于企业应用程序的“管道”,以便团队能够专一于应用程序级业务逻辑,而没必要与特定部署环境造成没必要要的联系。
- 核心技术:依赖注入,事件,资源,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+
在项目中开始使用spring-framework的推荐方法是使用依赖管理系统 - 下面的代码片断能够复制并粘贴到您的构建中。
其实不止Spring 官网咱们现在的各大公司应该大多数也推荐咱们是用Maven和Gradle 来管理项目Jar包依赖。
若是你使用的Maven:
<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
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 上下文的项目
准备工做:
Tips: 下载的时候有个坑注意下,若是你的JDK是64位,默认直接下载的STS是32位,会出现这个错误。
因此下载的时候必定要下载JDK匹配的版本才行,移步:https://spring.io/tools/sts/all
这里下载完成后咱们在咱们的IDE空白处,右键——> New——> Other...
输入maven 搜索,选择Maven Project,建立一个Maven项目
选择默认的工做空间
选择默认的类型
输入项目基本信息后点击Finish 完成
而后咱们应该能够看到这样的项目结构
首先修改pom.xml
<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
package com.xingyun.spring_context_sample.hello; public interface MessageService { String getMessage(); }
hello/MessagePrinter.java
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
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 |
org.springframework |
spring-aop |
Proxy-based AOP support |
org.springframework |
spring-aspects |
AspectJ based aspects |
org.springframework |
spring-beans |
Beans support, including Groovy |
org.springframework |
spring-context |
Application context runtime, including scheduling and remoting abstractions |
org.springframework |
spring-context-support |
Support classes for integrating common third-party libraries into a Spring application context |
org.springframework |
spring-core |
Core utilities, used by many other Spring modules |
org.springframework |
spring-expression |
Spring Expression Language (SpEL) |
org.springframework |
spring-instrument |
Instrumentation agent for JVM bootstrapping |
org.springframework |
spring-instrument-tomcat |
Instrumentation agent for Tomcat |
org.springframework |
spring-jdbc |
JDBC support package, including DataSource setup and JDBC access support |
org.springframework |
spring-jms |
JMS support package, including helper classes to send/receive JMS messages |
org.springframework |
spring-messaging |
Support for messaging architectures and protocols |
org.springframework |
spring-orm |
Object/Relational Mapping, including JPA and Hibernate support |
org.springframework |
spring-oxm |
Object/XML Mapping |
org.springframework |
spring-test |
Support for unit testing and integration testing Spring components |
org.springframework |
spring-tx |
Transaction infrastructure, including DAO support and JCA integration |
org.springframework |
spring-web |
Foundational web support, including web client and web-based remoting |
org.springframework |
spring-webmvc |
HTTP-based Model-View-Controller and REST endpoints for Servlet stacks |
org.springframework |
spring-webmvc-portlet |
MVC implementation to be used in a Portlet environment |
org.springframework |
spring-websocket |
WebSocket and SockJS infrastructure, including STOMP messaging support |