天天看點

1.Spring官網初探

源碼下載下傳位址( http://repo.spring.io/release/org/springframework/spring/

日常生活中,我們發現什麼東西都是原裝的好,是以無論學習程式設計語言還是架構,與其花大量的時間搜資料,不如靜心好好學習官網,官網是最好的學習資料(權威、準确的第一手材料),spring的官方網址:

https://spring.io/

官網的界面簡潔清新,導航很明确,進入projects

從配置到安全,Web應用到大資料 - 無論您的應用程式有什麼樣的需求,都有一個Spring Project來幫助您建構它,spring的涵蓋面是很寬廣的,你需要什麼可以在上圖所示的頁面中查找,本頁很清晰,很容易找到spring framework, 還有一段英文介紹provides core support for dependency injection, transaction management, web apps, data access, messaging and more.(提供了核心功能依賴注入、事務管理、web應用、資料通路、遠端通路等等)

選擇spring framework

本頁有介紹、特點說明、spring架構版本對jdk的要求、以及如果使用Maven或 Gradle建構項目的話,官網還提供了相應的範例向導。

最重要是在特征下面的這段話,需要注意:

All avaible features and modules are described in 

the Modules section of the reference documentation . Their  maven/gradle coordinates are also described there

.

這段話很清晰的告訴我們點選這段話上的連結,專門有關于所有特征和子產品以及各子產品之間關系的介紹。這是一頁有關于spring架構的很詳細的介紹,包括spring的Jar包連結以及說明,是以很有必要認真看一看。

Spring架構簡介

Spring架構為基于Java的企業應用程式提供了全面的程式設計和配置模型,Spring的一個關鍵要素是應用程式級别的基礎架構支援:Spring側重于企業應用程式的“管道”,以便團隊可以專注于應用程式級别的業務邏輯,而不必與特定的部署環境形成不必要的聯系。

Spring架構特點

  • 依賴注入
  • AOP面向切面的程式設計,包括Spring的聲明式事務管理
  • Spring MVC和Spring WebFlux Web架構
  • 對JDBC,JPA,JMS的基礎支援

開始使用

官方推薦在項目中使用spring-framework的方法是使用依賴管理系統 - 下面的代碼片段可以複制并粘貼到您的建構中。

maven:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>           

gradle:

dependencies {
    compile 'org.springframework:spring-context:5.0.2.RELEASE'
}           

Spring架構包含許多不同的子產品。這裡我們展示了提供核心功能的spring-context子產品

hello/MessageService.java

package hello;

public interface MessageService {
    String getMessage();
}           

hello/MessagePrinter.java

package 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());
    }
}           

hello/Application.java

package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan
public class Application {

    @Bean
    MessageService mockMessageService() {
        return new MessageService() {
            public String getMessage() {
              return "Hello World!";
            }
        };
    }

  public static void main(String[] args) {
      ApplicationContext context = 
          new AnnotationConfigApplicationContext(Application.class);
      MessagePrinter printer = context.getBean(MessagePrinter.class);
      printer.printMessage();
  }
}