1.Spring Framework 5.0 入门篇

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框架,从该组建立起,利用多核,下一代处理器和大量并发连接。

总结:

  • Spring MVC 是基于Servlet API 构建的同步阻塞式I/O 的Web 框架。
  • Spring WebFlux 是一种更优秀的非阻塞式Web框架,而且能更好处理大量并发连接。

看到这里,相信此时聪明的你应该晓得为什么我之前会那么说了吧。

2. Spring 官网介绍

上面这些链接相信很多人都知道,但是其实往往我们不是很清楚什么时候用哪个链接。

Spring 官网: 关注Spring 官网动态,最新的Spring 技术和版本发布公告

Spring 文档: what you want to do ? 你想开发一个什么样的项目?可以在这里快速找到相关介绍和文档。

Spring IDE:    如果你打算用Eclipse 版本,那么我推荐用 官网这个STS,因为它应该是最友好支持Spring的Eclipse 版本。当然,如果如果条件可以,我还是强烈推荐你使用Intellij Idea.

Spring Project: 这里是按照项目模块划分的,比如 从配置到安全,Web应用程序到大数据,想学习哪个就按照分类去学即可。

 

项目生成器:这里是Spring 官网提供的一个非常便利的工具,需要哪些依赖,哪个版本,在这里配置下然后下载即可。

3. Spring Framework

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 最低要求

  • JDK 8+ for Spring Framework 5.x
  • JDK 6+ for Spring Framework 4.x
  • JDK 5+ for Spring Framework 3.x

Tips: 所以你的电脑现在推荐使用 JDK1.8+ 

3.4 快速开始

在项目中开始使用spring-framework的推荐方法是使用依赖管理系统 - 下面的代码片段可以复制并粘贴到您的构建中。

需要帮忙? 请参阅我们有关使用 Maven和 Gradle 构建的入门指南。

其实不止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>
View Code

创建文件

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

原文地址:https://www.cnblogs.com/xingyunblog/p/6926927.html