1. 程式人生 > >Spring5學習(二)-spring projects之Spring Framework

Spring5學習(二)-spring projects之Spring Framework

Spring Framework

Core support for dependency injection(依賴注入), transaction management, web applications, data access, messaging, testing and more.

Introduction

The Spring Framework provides a comprehensive(綜合的;廣泛的;有理解力的) programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element(關鍵要素) of Spring is infrastructural(基礎設施) support at the application level: Spring focuses on the "plumbing"(管道工程) of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Features

Dependency Injection
Aspect-Oriented(面向切面) Programming including Spring's declarative(宣告的) transaction management
Spring MVC andSpring WebFlux web frameworks
Foundational support for JDBC, JPA, JMS
Much more…

All available features and modules are described in the Modules section of the reference documentation. Their maven/gradle coordinates are also described there.

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

Quick Start

The recommended way to get started using spring-framework in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.

current version:5.0.2

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 Framework includes a number of different modules. Here we are showing spring-context which provides core functionality(功能). Refer to the getting started guides on the right for other options.
Once you've set up your build with the spring-context dependency, you'll be able to do the following:

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();
  }
}
The example above shows the basic concept of dependency injection, the MessagePrinter is decoupled(減弱) from the MessageService implementation, with Spring Framework wiring(裝配) everything together.



說明:

1. Spring WebFlux:Spring webflux 是一個新的非堵塞函式式 Reactive Web 框架,可以用來建立非同步的,非阻塞,事件驅動的服務,並且擴充套件性非常好。(來自:LUPA

2. JPA:JPA是Java Persistence API的簡稱,中文名Java持久層API,是JDK 5.0註解或XML描述物件-關係表的對映關係,並將執行期的實體物件持久化到資料庫中。(來自:百度百科

3. JMS:Java平臺上的專業技術規範。JMS即Java訊息服務(Java Message Service)應用程式介面,是一個Java平臺中關於面向訊息中介軟體(MOM)的API,用於在兩個應用程式之間,或分散式系統中傳送訊息,進行非同步通訊。Java訊息服務是一個與具體平臺無關的API,絕大多數MOM提供商都對JMS提供支援。(來自:百度百科