Spring初学文档
- 格式:docx
- 大小:28.16 KB
- 文档页数:15
spring官⽹在线学习⽂档翻译55. Aspect Oriented Programming with Spring(使⽤Spring进⾏⾯向切⾯编程)5.1. Introduction(⼊门)Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)⾯向⽅⾯程序设计(AOP)通过提供另⼀种考虑程序结构的⽅法来补充⾯向对象程序设计(OOP)。
OOP中模块性的关键单元是类,⽽AOP中模块性的单元是⽅⾯。
⽅⾯⽀持关注点的模块化,例如跨多个类型和对象的事务管理。
(在AOP⽂献中,这样的关注点通常被称为横切关注点。
)One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP complements Spring IoC to provide a very capable middleware solution.Spring的关键组件之⼀是AOP框架。
spring是一个开源的控制反转(inversion of control)和面向切面(AOP)的容器框架,她主要设计目标是简化开发。
如:public class UserService {private UserDAO userDAO = new UserDAO();public void addUser(User user) {userDAO.add(user);}}userDAO是在应用内部创建和维护,控制反转是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护由外部容器负责,这种控制权由应用转移到外部容器就称为反转。
当把这种控制权力交给外部容器处理之后代码可以改为public class UserService {private UserDAO userDAO;public void addUser(User user) {userDAO.add(user);}}针对这个属性可以提供构造方法中传递参数或者提供setter方法进行注入。
依赖注入(Dependency Injection):由容器动态地将依赖对象注入到组件中。
Spring带来的好处1、降低组件之间的耦合度,实现软件各层之间的解耦。
Action →service →DAO2、可以使用容器提供相关服务,如:事务管理服务等,使用spring管理服务之后不再需要手工控制事务。
(hibernate或者是jdbc的事务相关操作代码都可以去掉)Spring的声明式事务管理可以实现复杂的事务操作。
3、容器提供单例模式,开发人员不再编写单例代码。
4、容器提供AOP技术,利用它很容易实现权限拦截、监控、日志等功能。
5、容器提供很多辅助类,利用这些类能够加快应用的开发,如:JdbcTemplate、HibernateTemplate等6、spring对于一些主流的框架提供了集成支持,如hibernate、struts、ibatis等搭建spring开发环境使用spring需要的jar在官网上下载spring的安装包dist\spring.jarlib\jakarta-commons\commons-logging.jar如果使用aop还需要Lib\aspectj\aspectjweaver.jar、aspectjrt.jarLib\cglib\cglib-nodep-2.1.3.jar如果使用JSR-250中的注解还需要使用lib\j2ee\common-annotations.jarspring配置文件,规范参考文档手册实例化spring容器方法一、在类路径下寻找配置文件来实例化容器ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{“beans.xml”,”user.xml”});方法二、在文件系统路径下需找配置文件来实例化容器ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“c:\\beans.xml”}); 注意:spring配置文件可以有多个,这时通过string数组传入。
第一部分 Spring框架的概述Spring框架是一个轻量级的解决方案和为构建企业级应用程序潜在的一站式服务。
然而,Spring是模块化的,允许你只使用那些你需要的部分,而忽略其他部分。
你可以在使用Struts时用Spring的IoC 容器,但你也可以只使用Hibernate 集成代码或JDBC抽象层。
Spring框架支持声明式事务管理、通过RMI或web服务远程访问你的逻辑,还有多种方式处理数据的持久化。
它还提供一个全能的MVC框架,并且能将AOP移植进你的软件。
Spring被设计为非侵入式的,意味着你的逻辑代码完全不必依赖于此框架。
虽然一些基于数据访问技术和Spring的库会存在于你的集成层(例如数据访问层),但是你的其他代码很容易隔离这些依赖。
1.开始使用Spring这篇手册提供了关于spring框架的详细信息,不仅有全面的特性,还有一些关于spring包含的潜在的概念(例如“依赖注入”)的背景知识。
如果你才刚刚开始,也许你应该从低级版的"Getting Started" 手册开始,从bbb://spring.io.访问。
为了更容易消化,这篇手册是专注于任务式。
2.Spring框架的介绍Spring框架是一个支持开发Java应用程序而提供全面的基础设施的Java平台,Spring处理基础部分从而你可以专注于你的应用。
spring 让你能够通过POJOs和向POJOs应用无侵入的企业服务就可以构建你的应用。
这些不仅能应用到Java SE而且还能应用到Java EE.一些作为一个开发者能够使用spring平台优势的例子●使Java方法可以执行数据库事务而不用去处理事务API●使本地Java方法可以执行远程过程而不用去处理远程API●使本地Java方法可以拥有管理操作而不用去处理JMXAPI●使本地Java方法可以执行消息处理而不用去处理JMSAPI2.1 依赖注入和控制反转Java应用程序——一个宽松的专业术语,小到一个Appletes大到运行在n层服务器上的企业级程序—通常由互相协作的对象而形成的适当的应用程序。
springboot教程文档Spring Boot 是一个用于简化 Spring 应用程序开发的框架,它可以帮助开发人员快速搭建独立的、可运行的、生产级的Spring 应用程序。
Spring Boot 提供了自动配置、启动器、命令行界面和一系列工具,以帮助开发人员快速进行应用开发,省去了繁琐的配置过程,提高了开发效率。
在使用 Spring Boot 之前,我们需要先了解一些基础概念和原理。
Spring Boot 使用了"约定优于配置"的原则,通过一些默认配置来快速搭建应用程序,开发人员只需要关注业务逻辑的实现即可。
Spring Boot 的核心特性包括自动配置、起步依赖和命令行界面。
自动配置是 Spring Boot 的一个重要特性,它根据应用程序的类路径和配置文件的内容,自动配置 Spring 应用程序的各种组件和功能。
例如,当我们引入了 Spring Boot Security Starter 依赖时,Spring Boot 会自动配置 Spring Security,根据配置文件中的内容来决定是否开启认证和授权功能。
起步依赖是 Spring Boot 的另一个重要特性,它通过引入一系列的依赖来简化应用程序的配置和部署。
这些依赖是预先配置好的,包含了大量常用的库和框架,开发人员只需要引入相应的依赖即可,不需要手动去找和配置依赖。
命令行界面是 Spring Boot 的一个可选特性,它提供了一个命令行界面,可以用于快速生成和运行 Spring Boot 项目。
我们可以使用命令行来创建项目、打包、运行和测试应用程序。
除了以上核心特性之外,Spring Boot 还提供了一系列扩展特性和工具,包括Actuator、Devtools、配置文件、错误处理等。
这些特性和工具可以帮助开发人员进一步提升开发效率和应用程序的可用性。
总的来说,Spring Boot 是一个简化 Spring 应用程序开发的框架,它提供了自动配置、启动器、命令行界面等特性和工具,可以帮助开发人员快速搭建独立的、可运行的、生产级的Spring 应用程序。
spring习题及答案Spring习题及答案Spring是一个非常流行的Java开发框架,它提供了一整套的工具和库,用于简化企业级应用程序的开发。
对于想要学习和掌握Spring框架的开发者来说,掌握一些基本的习题和答案是非常重要的。
下面我们来看一些常见的Spring习题及答案。
1. 什么是Spring框架?它有哪些核心模块?答:Spring框架是一个轻量级的Java开发框架,它提供了一整套的解决方案,用于简化企业级应用程序的开发。
Spring框架的核心模块包括IoC容器、AOP、数据访问和事务管理、Web开发、测试等。
2. 什么是Spring的IoC容器?它有哪些常见的实现方式?答:IoC(Inversion of Control)容器是Spring框架的核心,它负责管理应用程序中的对象。
常见的IoC容器实现方式包括BeanFactory和ApplicationContext。
3. Spring的AOP是什么?它有哪些常见的概念?答:AOP(Aspect-Oriented Programming)是一种编程范式,它允许开发者在不改变原有代码的情况下,增加横切关注点的功能。
常见的AOP概念包括切点(Pointcut)、通知(Advice)、切面(Aspect)等。
4. 如何在Spring中进行数据访问和事务管理?答:Spring提供了一整套的数据访问和事务管理的解决方案,包括JDBC、ORM、事务管理器等。
开发者可以通过配置数据源、定义数据访问对象(DAO)和事务管理器来实现数据访问和事务管理。
5. 如何在Spring中进行Web开发?答:Spring提供了一整套的Web开发解决方案,包括Spring MVC、RESTful Web服务、WebSocket等。
开发者可以通过配置控制器、视图解析器、拦截器等来实现Web开发。
6. 如何在Spring中进行单元测试和集成测试?答:Spring提供了一整套的测试支持,包括JUnit、TestNG、Spring Test、Mockito等。
【SpringFramework】Spring⼊门教程(三)使⽤注解配置本⽂主要介绍四个⽅⾯:(1) 注解版本IOC和DI(2) Spring纯注解(3) Spring测试(4) SpringJDBC - Spring对数据库的操作使⽤注解配置Spring⼊门说在前⾯学习基于注解的IoC配置,⼤家脑海⾥⾸先得有⼀个认知,即注解配置和xml配置要实现的功能都是⼀样的,都是要降低模块间的耦合度。
仅仅只是配置的形式不⼀样。
关于实际的开发中到底使⽤xml还是注解,每家公司有着不同的使⽤习惯。
所以这两种配置⽅式我们都需要掌握。
基于注解配置的⽅式也已经逐渐代替xml配置。
所以我们必须要掌握使⽤注解的⽅式配置Spring。
配置步骤注意:如果使⽤Eclipse需要先安装了STS插件,或者使⽤STS开发⼯具创建项⽬。
本⽂使⽤IDEA进⾏演⽰。
1.2.1. 第⼀步:拷贝必备jar包到⼯程的lib⽬录。
注意:在基于注解的配置中,我们还要多拷贝⼀个aop的jar包。
如下图:1.2.2. 第⼆步:在类的根路径下创建⼀个任意名称的xml⽂件(不能是中⽂)注意:基于注解整合时,Spring配置⽂件导⼊约束时需要多导⼊⼀个context命名空间下的约束。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/context/schema/context/spring-context.xsd"></beans>1.2.3. 第⼆步:创建⼀个服务类创建⼀个测试的服务类,并且加⼊使⽤@Component注解,声明该类允许注⼊到Spring容器package org.cjw.service;import ponent;/*使⽤注解配置时,需要将Spring框架启动就创建对象的类表⽰为组件类表⽰组件类使⽤@Component注解*/@Componentpublic class CustomerService {public void save() {System.out.println("-保存数据-");}}1.2.4. 第四步在spring的配置⽂件加⼊扫描注解<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/context/schema/context/spring-context.xsd"><!-- 声明扫描包及其⼦包的类,如果发现有组件注解的类,就创建对象并加⼊到容器中去 --><context:component-scan base-package="org.cjw" /></beans>1.2.5. 第五步:测试调⽤代码package org.cjw.test;import org.cjw.service.CustomerService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class CustomerServiceTest {@Testpublic void testSave() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService customerService = context.getBean(CustomerService.class);customerService.save();}}--测试结果,如果可以调⽤服务⽅法,测试成功。
Spring 实例入门本文结合实例理解解容器,DI,IOC,耦合,解耦等Spring所涉及的概念,同时了解Spring 的最基本也是最核心的使用方法。
1.Spring容器Spring容器负责对象的实例化,对象生命周期的管理,被Spring管理的对象称之为Bean。
Spring默认使用单例的方式创建对象。
可以通过修改<bean>的配置改变成其它创建方式。
这个属性为Scope,称之为作用域或生命周期,它的值为singleton(单例,默认值),prototype2.注入方式有setter注入,构造注入方式,接口注入(不需掌握)。
建议多使用Setter注入方式。
Setter注入:Soldier类中有一个属性name,如何在创建Soldier的时候使name的值变为”RANBO”?配置如下:这样创建的Soldier对象的name属性就有值了,测试代码:构造注入:配置如下:测试结果同上。
3.依赖当A对象使用了B对象的方法,A对B产生依赖,称之为A依赖B。
下面的例子中Soldier当HandGun发生变化时,必然导致Soldier必须做相应修改,同时,当Soldier需要使用OtherGun时也必须重新编写代码,导致代码重用度不高。
当对象之间的依赖关系很强时(耦合),会使程序代码死板,不利于后期的维护和扩展。
降低对象之间的依赖关系称之为解耦。
Spring能够很好的解决这一问题。
4.控制反转(Inversion of Control,简称IOC)和依赖注入(Dependence Inject简称DI)我们运用Spring的setter注入方式解决HandGun和Soldier的耦合问题。
修改Soldier的代码,将HandGun定义为Soldier的属性并提供setter方法:package com.hb;/***士兵类*/public class Soldier {private HandGun handGun;public void setHandGun(HandGun handGun) {this.handGun = handGun;}/***打仗*/public void fight(){handGun.killEnemy();}}配置如下己去实例化HandGun了。
本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!== 本文为word格式,下载后可方便编辑和修改! ==spring春天的英语作文模板导语:春天来了,虽然天气有点凉,但是万物复苏,一切充满生气,是个令人神往的季节。
下面是小编为大家整理的,英语范文。
想要知更多的资讯,请多多留意CNFLA学习网!春天的英语作文【篇1】Some say the spring sunshine, it is the most fair, he himself no error of sunlight on the north and south hemispheres, reflected akind of justice.The spring is in my mind should be selfless, she will be equal to her children, and when her children get her sunshine, will launch the warmth of their own.Have you noticed that fair is more than the spring? It is each person's love in the society."If everyone give a little love, the world will become a better human. The spring breeze of the human will not decay as time went on, will not fade as the change of the seasons. Because it is a link in the invisible line between each heart with heart. It may be the parents' strict discipline, may be the teacher to inculcate, may be the enthusiasm of the students help, these are the selfless love,just give. In the warmth, everyone can not only read a strange move, can read a hint of yearning for more.I wish spring in the world.I am not a painter, can not use clever pen to describe your very beautiful flowers.I am not a poet, can not use poetry to concentrate your abundant life.I am not a singer, can not use voice to make public your selfless.I'm just an ordinary person, can only use my hot heart to feel the charm of spring, feel the charm of spring.有人说,春分的阳光是最公正,他丝毫误差地把自己的阳光照射在南北半球上,体现了一种公正。
spring成神之路第三篇:Spring容器基本使⽤及原理(ApplicationCont。
1. jdk1.82. idea3. maven-3.6.14. spring-5.2.3.RELEASEIOC容器是具有依赖注⼊功能的容器,负责对象的实例化、对象的初始化,对象和对象之间依赖关系配置、对象的销毁、对外提供对象的查找等操作,对象的整个⽣命周期都是由容器来控制。
我们需要使⽤的对象都由ioc容器进⾏管理,不需要我们再去⼿动通过new的⽅式去创建对象,由ioc 容器直接帮我们组装好,当我们需要使⽤的时候直接从ioc容器中直接获取就可以了。
那么spring ioc容器是如何知道需要管理哪些对象呢?需要我们给ioc容器提供⼀个配置清单,这个配置⽀持xml格式和java注解的⽅式,在配置⽂件中列出需要让ioc容器管理的对象,以及可以指定让ioc容器如何构建这些对象,当spring容器启动的时候,就会去加载这个配置⽂件,然后将这些对象给组装好以供外部访问者使⽤。
这⾥所说的IOC容器也叫spring容器。
由spring容器管理的对象统称为Bean对象。
Bean就是普通的java对象,和我们⾃⼰new的对象其实是⼀样的,只是这些对象是由spring去创建和管理的,我们需要在配置⽂件中告诉spring容器需要创建哪些bean对象,所以需要先在配置⽂件中定义好需要创建的bean对象,这些配置统称为bean定义配置元数据信息,spring容器通过读取这些bean配置元数据信息来构建和组装我们需要的对象。
1. 引⼊spring相关的maven配置2. 创建bean配置⽂件,⽐如bean xml配置⽂件3. 在bean xml⽂件中定义好需要spring容器管理的bean对象4. 创建spring容器,并给容器指定需要装载的bean配置⽂件,当spring容器启动之后,会加载这些配置⽂件,然后创建好配置⽂件中定义好的bean对象,将这些对象放在容器中以供使⽤5. 通过容器提供的⽅法获取容器中的对象,然后使⽤spring内部提供了很多表⽰spring容器的接⼝和对象,我们来看看⽐较常见的⼏个容器接⼝和具体的实现类。
Spring Boot Reference Documentation Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, Christian Dupuis, Sébastien Deleuze, Michael Simons, Vedran Pavić, Jay Bryant, Madhura Bhave, Eddú Meléndez, ScottFrederick, Moritz HalbritterVersion 3.2.0-SNAPSHOTTable of Contents1. Legal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22. Getting Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33. Documentation Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.1. First Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.2. Upgrading From an Earlier Version. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.3. Developing With Spring Boot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.4. Learning About Spring Boot Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.5. Web. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.6. Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.7. Messaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.8. IO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.9. Container Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.10. Moving to Production. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.11. GraalVM Native Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.12. Advanced Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.1. Introducing Spring Boot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.2. System Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74.2.1. Servlet Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74.2.2. GraalVM Native Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.3. Installing Spring Boot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.3.1. Installation Instructions for the Java Developer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Maven Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Gradle Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.3.2. Installing the Spring Boot CLI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Manual Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installation with SDKMAN!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 OSX Homebrew Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 MacPorts Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Command-line Completion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Windows Scoop Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4.4. Developing Your First Spring Boot Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.4.1. Prerequisites. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.4.2. Setting up the project with Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.4.3. Setting up the project with Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164.4.5. Writing the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17The @RestController and @RequestMapping Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 The @SpringBootApplication Annotation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 The “main” Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184.4.6. Running the Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.4.7. Creating an Executable Jar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214.5. What to Read Next. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225. Upgrading Spring Boot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.1. Upgrading From 1.x. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.2. Upgrading to a New Feature Release. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.3. Upgrading the Spring Boot CLI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235.4. What to Read Next. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236. Developing with Spring Boot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 6.1. Build Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256.1.1. Dependency Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256.1.2. Maven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256.1.3. Gradle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256.1.4. Ant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266.1.5. Starters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 6.2. Structuring Your Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316.2.1. Using the “default” Package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316.2.2. Locating the Main Application Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 6.3. Configuration Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336.3.1. Importing Additional Configuration Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336.3.2. Importing XML Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 6.4. Auto-configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336.4.1. Gradually Replacing Auto-configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346.4.2. Disabling Specific Auto-configuration Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 6.5. Spring Beans and Dependency Injection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 6.6. Using the @SpringBootApplication Annotation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6.7. Running Your Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.7.1. Running From an IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.7.2. Running as a Packaged Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.7.3. Using the Maven Plugin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.8. Developer Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416.8.1. Diagnosing Classloading Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.8.2. Property Defaults. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.8.3. Automatic Restart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Logging Changes in Condition Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Excluding Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Watching Additional Paths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Disabling Restart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Using a Trigger File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Customizing the Restart Classloader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Known Limitations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.8.4. LiveReload. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.8.5. Global Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Configuring File System Watcher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506.8.6. Remote Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Running the Remote Client Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Remote Update. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.9. Packaging Your Application for Production. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536.10. What to Read Next. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537. Core Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 7.1. SpringApplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547.1.1. Startup Failure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557.1.2. Lazy Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567.1.3. Customizing the Banner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577.1.4. Customizing SpringApplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587.1.5. Fluent Builder API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597.1.6. Application Availability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Liveness State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Readiness State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Managing the Application Availability State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607.1.7. Application Events and Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647.1.8. Web Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657.1.9. Accessing Application Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657.1.10. Using the ApplicationRunner or CommandLineRunner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667.1.11. Application Exit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677.1.12. Admin Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697.1.13. Application Startup tracking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 7.2. Externalized Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707.2.1. Accessing Command Line Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Profile Specific Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Importing Additional Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Importing Extensionless Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Using Configuration Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Property Placeholders. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Working With Multi-Document Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Activation Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 7.2.4. Encrypting Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 7.2.5. Working With YAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Mapping YAML to Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Directly Loading YAML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 7.2.6. Configuring Random Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 7.2.7. Configuring System Environment Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 7.2.8. Type-safe Configuration Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86JavaBean Properties Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Constructor Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Enabling @ConfigurationProperties-annotated Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Using @ConfigurationProperties-annotated Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Third-party Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Relaxed Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Merging Complex Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Properties Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 @ConfigurationProperties Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 @ConfigurationProperties vs. @Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7.3. Profiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 7.3.1. Adding Active Profiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 7.3.2. Profile Groups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 7.3.3. Programmatically Setting Profiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.3.4. Profile-specific Configuration Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.4. Logging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.4.1. Log Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.4.2. Console Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Color-coded Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 7.4.3. File Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 7.4.4. File Rotation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 7.4.5. Log Levels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.4.6. Log Groups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Environment Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 7.4.10. Log4j2 Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Profile-specific Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Environment Properties Lookup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Log4j2 System Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7.5. Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7.6. JSON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 7.6.1. Jackson. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Custom Serializers and Deserializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Mixins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 7.6.2. Gson. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 7.6.3. JSON-B. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 7.7. Task Execution and Scheduling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 7.8. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 7.8.1. Test Scope Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7.8.2. Testing Spring Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7.8.3. Testing Spring Boot Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Detecting Web Application Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Detecting Test Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Using the Test Configuration Main Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Excluding Test Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Using Application Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Testing With a Mock Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Testing With a Running Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Customizing WebTestClient. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Using JMX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Using Metrics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Using Tracing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Mocking and Spying Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Auto-configured Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Auto-configured JSON Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Auto-configured Spring MVC Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Auto-configured Spring WebFlux Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Auto-configured Spring GraphQL Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Auto-configured Data Cassandra Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Auto-configured Data Couchbase Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Auto-configured Data Elasticsearch Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176。
Spring框架参考文档-5.0.0-中文完整版AuthorsRod Johnson , Juergen Hoeller , Keith Donald , Colin Sampaleanu , Rob Harrop , Thomas Risberg , Alef Arendsen , Darren Davison , Dmitriy Kopylenko , Mark Pollack , Thierry Templier , Erwin Vervaet , Portia Tung , Ben Hale , Adrian Colyer , John Lewis , Costin Leau , Mark Fisher , Sam Brannen , Ramnivas Laddad , Arjen Poutsma , Chris Beams , Tareq Abedrabbo , Andy Clement , Dave Syer , Oliver Gierke , Rossen Stoyanchev , Phillip Webb , Rob Winch , Brian Clozel , Stephane Nicoll , Sebastien Deleuze版本号:5.0.0.RELEASECopyright ? 2004-2016Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.文档官网:https://docs.spring.io/spring/docs/5.0.0.RELEASE/spring-framework-reference/index.html现在官网的5.0.0已经在2017年9月28日出了release版,为此翻译了5.0.0版本(从4.3.10升级到此版本,如果还在使用4.3.10版本,请看本人的前一个版本),翻译前后历时15天,十多次的修改和校对。
最全⾯的Spring学习笔记致⼒于提供⼀种⽅法管理你的业务对象。
在⼤量Java EE的应⽤中,随处可见Spring。
今天我将简单的介绍⼀下Spring这个框架。
本⽂适合读者:想学Spring的Java开发者刚⽤Spring不久的⼈Why为什么要使⽤Spring?Spring主要两个有功能为我们的业务对象管理提供了⾮常便捷的⽅法:DI(Dependency Injection,依赖注⼊)AOP(Aspect Oriented Programming,⾯向切⾯编程)Java Bean每⼀个类实现了Bean的规范才可以由Spring来接管,那么Bean的规范是什么呢?必须是个公有(public)类有⽆参构造函数⽤公共⽅法暴露内部成员属性(getter,setter)实现这样规范的类,被称为Java Bean。
即是⼀种可重⽤的组件。
DI-依赖注⼊简单来说,⼀个系统中可能会有成千上万个对象。
如果要⼿⼯维护它们之间的关系,这是不可想象的。
我们可以在Spring的XML⽂件描述它们之间的关系,由Spring⾃动来注⼊它们——⽐如A类的实例需要B类的实例作为参数set进去。
AOP-⾯向切⾯编程就以⽇志系统为例。
在执⾏某个操作前后都需要输出⽇志,如果⼿⼯加代码,那简直太可怕了。
⽽且等代码庞⼤起来,也是⾮常难维护的⼀种情况。
这⾥就需要⾯向切⾯来编程How关于BeanBean的⽣命周期如你所见,在bean准备就绪之前,bean⼯⼚执⾏了若⼲启动步骤。
我们对图进⾏详细描述:1. Spring对bean进⾏实例化;2. Spring将值和bean的引⽤注⼊到bean对应的属性中;3. 如果bean实现了BeanNameAware接⼝,Spring将bean的ID传递给setBean-Name()⽅法;4. 如果bean实现了BeanFactoryAware接⼝,Spring将调⽤setBeanFactory()⽅法,将BeanFactory容器实例传⼊;5. 如果bean实现了ApplicationContextAware接⼝,Spring将调⽤setApplicationContext()⽅法,将bean所在的应⽤上下⽂的引⽤传⼊进来;6. 如果bean实现了BeanPostProcessor接⼝,Spring将调⽤它们的post-ProcessBeforeInitialization()⽅法;7. 如果bean实现了InitializingBean接⼝,Spring将调⽤它们的after-PropertiesSet()⽅法。
超详细的SpringBoot框架⼊门教程Spring Boot 框架快速⼊门教程以⼤量⽰例讲解了 Spring Boot 在各类情境中的应⽤,让⼤家可以跟着⽼师的思维和代码快速理解并掌握。
适⽤于 Java 开发⼈员,尤其是初学 Spring Boot 的⼈员和需要从传统 Spring 转向 Spring Boot 开发的技术⼈员。
下边是动⼒节点的SpringBoot教程⾮常适合初学⼊门,讲的⾮常详细,⽽且全程⽆废话!视频链接:1. Spring Boot简介Spring 诞⽣时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品。
⽆需开发重量级的 Enterprise JavaBean(EJB),Spring 为企业级Java 开发提供了⼀种相对简单的⽅法,通过依赖注⼊和⾯向切⾯编程,⽤简单的Java 对象(Plain Old Java Object,POJO)实现了 EJB 的功能。
虽然 Spring 的组件代码是轻量级的,但它的配置却是重量级的。
第⼀阶段:xml配置在Spring 1.x时代,使⽤Spring开发满眼都是xml配置的Bean,随着项⽬的扩⼤,我们需要把xml配置⽂件放到不同的配置⽂件⾥,那时需要频繁的在开发的类和配置⽂件之间进⾏切换第⼆阶段:注解配置在Spring 2.x 时代,随着JDK1.5带来的注解⽀持,Spring提供了声明Bean的注解(例如@Component、@Service),⼤⼤减少了配置量。
主要使⽤的⽅式是应⽤的基本配置(如数据库配置)⽤xml,业务配置⽤注解第三阶段:java配置Spring 3.0 引⼊了基于 Java 的配置能⼒,这是⼀种类型安全的可重构配置⽅式,可以代替 XML。
我们⽬前刚好处于这个时代,Spring4.x和Spring Boot都推荐使⽤Java配置。
所有这些配置都代表了开发时的损耗。
Spring2.5注释驱动8.4.1 Spring2.5注释驱动注释语法越来越多的被业界所使用,并且注释配置相对于 XML 配置具有很多的优势:它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。
注释和 Java 代码位于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和Java 代码放在一起,有助于增强程序的内聚性。
而采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。
因此在很多情况下,注释配置比 XML 配置更受欢迎,注释配置有进一步流行的趋势。
Spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。
在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立。
代码清单1 Foo.java Foo对象有一个String类型的name属性.package ;public class Foo {private String name;public String toStirng(){return"Foo Name is :" + ;}Set和get方法}代码清单2 Bar.java Bar对象有一个String类型的add属性.package ;public class Bar {private String add;public String toStirng(){return"Bar Add is :" + this.add;}Set和get方法}代码清单3 Main.java Main对象有两个属性分别是Foo和Bar package ;public class Main {private Foo foo;private Bar bar;public String toString(){return"Main : [" + +" "+ + "]";}Set和get方法}代码清单4 配置文件spring-config-beans.xml<bean id="main" class=""><property name="foo" ref="foo"></property><property name="bar" ref="bar"></property></bean><bean id="foo" class=""><property name="name" value="Foo"></property></bean><bean id="bar" class=""><property name="add" value="Bar"></property></bean>代码清单 5 Test.java Test类用于初始化Spring容器并获得main对象package ;import ;importClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {String[] locations = {"spring-config-beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);Main main = (Main) ctx.getBean("main");;}}运行Test类控制台输出以下信息:Main : [Foo Name is :Foo Bar Add is :Bar]这说明Spring已经完成了Bean的创建和装配工作。
1)使用 @Autowired 注释Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
下面我们来看一下使用 @Autowired 进行成员变量自动注入的代码:代码清单6使用 @Autowired 注释的 Main.java,此时可以将Main.java类中的set和get方法删除package ;import ;public class Main {@Autowiredprivate Foo foo;@Autowiredprivate Bar bar;public String toString(){return"Main : [" + +" "+ + "]";}}Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让@Autowired 起作用必须事先在 Spring 容器中声明AutowiredAnnotationBeanPostProcessor Bean代码清单 7 修改配置文件<!-- 该BeanPostProcessor 将自动对标注@Autowired 的Bean 进行注入--><bean class="AutowiredAnnotationBeanPostProcessor"/><!—此时移除main Bean 的属性注入信息--><bean id="main" class=""></bean><bean id="foo" class=""><property name="name" value="Foo"></property></bean><bean id="bar" class=""><property name="add" value="Bar"></property></bean>当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并将其注入。
2)使用@Qualifier 注释Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样就不会产生注入错误了,请看下面代码清单:代码清单8 修改Main.java类中的foo属性注释增加注释@Qualifier("foo1") public class Main {@Autowired@Qualifier("foo1")private Foo foo;@Autowiredprivate Bar bar;public String toString(){return"Main : [" + +" "+ + "]";}}代码清单9 在配置文件中增加id为foo2 Bean定义<bean class="AutowiredAnnotationBeanPostProcessor"/><bean id="main" class=""></bean><bean id="foo1" class=""><property name="name" value="Foo1"></property> </bean><bean id="foo2" class=""><property name="name" value="Foo2"></property> </bean><bean id="bar" class=""><property name="add" value="Bar"></property> </bean>运行Test.java控制台输出如下信息:Main : [Foo Name is :Foo1 Bar Add is :Bar]证明Spring容器成功将foo1注入进main类中3)使用<context:annotation-config/>简化配置Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。
我们知道注释本身是不会做任何事情的,它仅提供元数据信息。
要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
而我们前面所介绍的AutowiredAnnotationBeanPostProcessor 就是处理这些注释元数据的处理器。
但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。
Spring 为我们提供了一种方便的注册这些 BeanPostProcessor 的方式,这就是<context:annotation-config/>。
请看下面的代码清单:代码清单10<context:annotation-config/><bean id="main" class=""></bean><bean id="foo1" class=""><property name="name" value="Foo1"></property></bean><bean id="foo2" class=""><property name="name" value="Foo2"></property></bean><bean id="bar" class=""><property name="add" value="Bar"></property></bean>代码清单中将<bean class="AutowiredAnnotationBeanPostProcessor"/>替换成为<context:annotation-config/><context:annotationconfig/> 将隐式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。