Spring Framework 简介

Spring Framework

依赖注入、事务管理、Web应用程序、数据访问、消息传递、测试和更多的核心支持。

Tips:

Spring 官网:https://spring.io/

spring framework 官网:https://projects.spring.io/spring-framework/

0x01介绍

Spring框架提供了现代的基于java的企业应用程序在任何部署平台的综合规划和配置模型。Spring的一个关键元素是应用程序级的基础设施支持:Spring关注企业应用程序的“管道”,以便团队能够专注于应用级业务逻辑,而不必与特定部署环境不必要的联系。

0x02 功能

  • 核心技术:依赖注入、事件、资源、国际化、验证、数据绑定、类型转换,拼写,AOP。
  • 测试:模拟对象,TestContext框架,Spring MVC测试,WebTestClient。
  • 数据访问:事务、DAO支持、JDBC、ORM、编组XML。
  • Spring MVC Web框架和 Spring WebFlux web 框架
  • 整合:Remoting、JMS、JCA、JMX、电子邮件、任务调度、缓存。
  • 语言:Kotlin,Groovy动态语言。

0x03 Minimum requirements

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

0x04 Building Java Projects with Maven

 和大多数 Spring入门指南 一样,您可以从头开始完成每一步,也可以绕过您已经熟悉的基本设置步骤。无论哪种方式,最终都有工作代码。

  •  1. 手动使用Maven创建Java 项目

 2. 勾选如图所示两个地方

 

  3. 填写Project Name,点击 ’Finish‘ 按钮

4. 创建hello 文件夹,目录结构如图所示:

5. 修改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>

    <!-- 添加Spring Context maven依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
    </dependencies>

</project>

6.创建接口文件

hello/MessageService.java

package hello;

public interface MessageService {
    String getMessage();
}

7.创建实体类

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

Tips:这里一定不要漏了写这两个注解

8.创建方法调用主类

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) {
        // TODO Auto-generated method stub
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.printMessage();
    }

}

Tips:这里特别注意,任何一个注解都不能缺少,否则会报错。

9. Run 主方法

上面的例子显示了依赖注入的基本概念,messageprinter 实现 MessageService接口是解耦的,写什么都可以使用Spring Framework。 

本文项目源码下载地址:https://github.com/geekxingyun/JavaEE-Framework-Sample/tree/master/SpringBoot-Sample/spring-context-sample

 0x05 其他更多用法参考文档

https://spring.io/guides

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