美文网首页技术随笔IT翻译我爱编程
译:Spring快速上手firstPage

译:Spring快速上手firstPage

作者: 笑楔信 | 来源:发表于2017-10-26 14:20 被阅读16次

http://projects.spring.io/spring-framework/#quick-start

简介

Spring 框架提供了综合性的编程和配置模型,对象是当前基于JAVA的企业应用(应用于各种发布平台)。Spring一个重要优势体现于在应用层上提供了架构式的支持:Spring专注于企业级应用的探究,让项目团队能够专注于应用层次上的业务逻辑,而无需关注具体的部署环境。

特色--(很多人翻译为特性,如:新特性)

依赖注入

面向切线编程-包含了Spring声明式事务管理

Spring MVC和Spring WebFlux的web架构

        * Spring webflux 是一个新的非堵塞函数式 Reactive Web 框架,可以用来建立异步的,非阻塞,事件驱动的服务,并且扩展性非常好

对JDBC、JPA、JMS的基础性支持

更多……

所有这些可利用的特性和模型,更多详情尽在:the Modules section of the reference documentation。关于maven/gradle坐标的描述也可以在上面链接中找到。

最低需求

JDK 8+ for Spring Framework 5.x

JDK 6+ for Spring Framework 4.x

JDK 5+ for Spring Framework 3.x

快速引入Spring

下载地址 版本 5.0.1 maven/gradle

推荐您通过依赖管理系统开始使用Spring,复制下面的代码片段到你pom中。有不明白的,看一下Maven和Gradle的开始引导。

<dependencies>

    <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-context</artifactId>

        <version>5.0.1.RELEASE</version>

    </dependency>

</dependencies>

Spring Framework包含了一些不同的模块。这里显示的Spring-context提供了最核心的功能。参考右侧菜单上入门引导可以查看其它选项。

一旦你已经构建好了你的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();

}

}

上述的例子展示了依赖注入最基本的概念,MessagePrinter与MessageService实现类实现了解耦,利用了Spring可以把所有事物自动装配到一起的能力。

执行main方法:

首先,new AnnotationConfigApplicationContext(Application.class),初始化Application方法,然后通过@ComponentScan注解找到所有@Component的类,扫描这些类时发现了@Autowired--MessagePrinter(MessageService service),Spring要帮其找到匹配的bean,这时要试着执行MessagePrinter(MessageService service)方法,发现没有MessageService的实例,所以找匹配的bean。

然后,找到@Bean注解中的mockMessageService,注入到MessagePrinter(MessageService service)中,注入时需要执行一遍mockMessageService方法,然后再次执行MessagePrinter的构造方法MessagePrinter(MessageService service),执行成功

之后把其他的@Bean执行一遍取得每个@Bean的实例(不过这里暂时没有其他@bean)。可以看出,@Autowired优先级高于@Bean。

最后执行printer.printMessage()。

参考

“这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。”--百度百科@Autowired

译:@Autowired

相关文章

网友评论

    本文标题:译:Spring快速上手firstPage

    本文链接:https://www.haomeiwen.com/subject/djbhpxtx.html