2019独角兽企业重金招聘Python工程师标准>>>
使用spring-boot
这部分目标是更详细的讲解如何使用spring-boot。它包含的主题包括例如编译系统、自动配置和怎么运行你的项目。我们还包含关于spring-boot最好的实战部分。虽然这部分没有关于spring-boot一些特性的知识,但是有许多推荐的内容,如果我们遵循的话,会使开发过程变的简单。如果你还没有看关于《spring-boot开始》部分,那么建议您在看这部分内容前先去看看《spring-boot开始》部分。
第一部分:编译系统
强烈建议您选择一个支持‘dependency management’的编译系统,可以使用从maven中央库已发过的产品。我们建议您使用maven或者grade。你也可以选择其他编译系统(比如ant),但这有可能不能很好的支持spring-boot的很多特性。
1:Dependency management
每一个spring-boot发布都提供它自己的依赖支持。在实战中,让spring-boot管理你的编译配置你并不需要提供这些版本依赖关系。当你要提升spring-boot版本时,这些也自动的提升。你可以使用spring-boot作为第三方库,它包含了所有的spring模型。作为一个标准的资源库能够很好的被maven和grade支持。
2:maven
maven用户可以继承‘spring-boot-stater-parent’项目,它提供了一些明智的默认值。
这个父项目提供一下特性:
a:java 1.6作为默认的编译级别
b:UTF-8编码格式
c:一个依赖管理部分,继承于spring-boot-dependencies可以让你省去
d:明智的资源过滤
e:明智的插件管理
f:对application.properties和application.yml提供明智的资源过滤
最重要的一点:自从默认的配置文件支持spring占位符({...}),maven的过滤则改为@... @占位符使用(你可以使用maven的配置文件resource.delimiter重写规则)
继承start parent
让你的项目继承于'spring-boot-starter-parent'可以简单的设置parent
org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE
在设置之后,在你的项目中你可以重写一个一个属性来定制你的依赖关系。例如,你要升级到另一个Spring Data release train,你可以在pom.xml添加如下
Fowler-SR2
检查 spring-boot-dependencies pom提供支持的属性列表
另一种使用spring-boot方式(不继承start parent)
并不是所有人都喜欢继承'spring-boot-starter-parent'。你可能有你自己的父项目要用,或者你更喜欢申明所有的maven配置。如果你不想使用'spring-boot-starter-parent',你也可以使用dependency management(但是不是plugin management)通过一个 scope=import来使用。
org.springframework.boot
spring-boot-dependencies
1.3.3.RELEASE
pom
import
此种设置方式不允许你定制自己的依赖关系。为了达到相同的结果,你需要在你项目的dependencyManagement中的spring-boot-dependencies入口之前添加一个入口。比如,你想提升到另一个Spring Data release train需在pom.xml中添加如下
org.springframework.data
spring-data-releasetrain
Fowler-SR2
import
pom
org.springframework.boot
spring-boot-dependencies
1.3.3.RELEASE
pom
import
对于重写其他的依赖方式也用此方法修改。
改变java version
'spring-boot-starter-parent'采取了一个相当保守的java兼容性。如果你想遵循我们的建议使用最新的java版本,你可以使用java.version属性。
1.8
使用spring boot maven插件
spring boot包含可以将项目打包成可执行文件的maven插件。如果你想使用可以将其添加到你的
org.springframework.boot
spring-boot-maven-plugin
如果你使用了'spring-boot-starter-parent',你只需要添加这个插件,不需要去配置。除非你想改变'parent'默认的配置。
Starter POMs(翻译不来,自己理解吧。就是spring一些默认的开始模型)
Starter POMs就是一些约定的依赖描述可以加载到你的项目中。你将通过一站式获得所有spring和相关技术信息,不需要通过样板代码和复制粘贴依赖描述。比如你想使用spring和JPA访问数据库,只需引入spring-boot-starter-data-jpa依赖到你的项目中。
这些staters提供了你想讲项目升级和快速运行以及可移植的依赖关系。
以什么命名
所有官方的stater都遵循了相似的命名方式'spring-boot-starter-*',这个*代表应用程序类型。这种命名结构用来帮你查找一个starter。在许多集成Maven的IDE中允许你通过名称来搜寻你要starter。比如Eclipse安装了中插件,在pom.xml文件中键入‘spring-boot-starter’你可以通过ctrl+space来获取更多的列表。
如下的程序提供于spring-boot旗下的org.springframework.boot组
Name | Description |
spring-boot-starter-jta-atomikos | Support for JTA distributed transactions via Atomikos. |
spring-boot-starter-jta-bitronix | Support for JTA distributed transactions via Bitronix. |
spring-boot-starter-mail | Support for javax.mail. |
spring-boot-starter-mobile | Support for spring-mobile. |
spring-boot-starter-mustache | Support for the Mustache templating engine. |
spring-boot-starter-redis | Support for the REDIS key-value data store,including spring-redis. |
spring-boot-starter-security | Support for spring-security. |
spring-boot-starter-social-facebook | Support for spring-social-facebook. |
spring-boot-starter-social-linkedin | Support for spring-social-linkedin. |
spring-boot-starter-social-twitter | Support for spring-social-twitter. |
spring-boot-starter-test | Support for common test dependencies,including JUnit, Hamcrest and Mockito along with the spring-test module. |
spring-boot-starter-thymeleaf | Support for the Thymeleaf templating engine,including integration with Spring. |
spring-boot-starter-velocity | Support for the Velocity templating engine. |
spring-boot-starter-web | Support for full-stack web development,including Tomcat and spring-webmvc. |
spring-boot-starter-websocket | Support for WebSocket development. |
spring-boot-starter-ws | Support for Spring Web Services. |
spring-boot-starter-actuator | Adds production ready features such as metrics and monitoring. |
spring-boot-starter-remote-shell | Adds remote ssh shell support. |
此外,spring boot还提供一些其他技术方面
Name | Description |
spring-boot-starter-jetty | Imports the Jetty HTTP engine (to be used as an alternative to Tomcat). |
spring-boot-starter-log4j | Support the Log4J logging framework. |
spring-boot-starter-logging | Import Spring Boot’s default logging framework (Logback). |
spring-boot-starter-tomcat | Import Spring Boot’s default HTTP engine (Tomcat). |
spring-boot-starter-undertow | Imports the Undertow HTTP engine (to be used as an alternative to Tomcat). |
第二部分:构建你的代码
spring boot不需要任何的代码结构来工作,然而,这有更实用的来帮助。
1:使用默认包
当一个类没有引入包申明时,它认为在默认包中。通常情况下应该避免使用默认包。对于springboot应用程序来说它会引起一些问题。如果使用@ComponentScan, @EntityScan 或者@SpringBootApplication注解,它会从所有的jar包中所有的类去读取。我们建议您不要使用默认包。
2:定位类文件
我们建议您将main的类文件放在root包中在其他所有类文件之上。这个@EnableAutoConfiguration 通常放置在main类文件中,它默认去搜索包中所有包含的子注解。比如,你在写一个JPA应用程序, @EnableAutoConfiguration 注解的文件会去搜索含有@Entity的类文件。
使用root包同样允许@ComponentScan注解而不要指定基础包属性。你也可以使用@SpringBootApplication在你的root包中的main类文件。
这是一个经典的布局
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
这个Application.java文件定义了main方法,遵循基本的@Configuration
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
第三部分:配置类文件
spring-boot支持java基本配置。虽然可以通过XML源文件来调用SpringApplication.run()方法,但是我们建议您的主要的源码是一个@Configuration类。通常定义了main方法的类是@Configuration的首选。
Tip:通常在web程序中使用spring配置是基于XML配置。如果可以尝试一下与其等同的java基础配置。搜索Enable*开始的注解是一个好的开始。
1:导入额外的配置类文件
你不需要把所有的 @Configuration放到类中。@Import注解可以导入额外的配置类文件。另一种选择,你可以使用@ComponentScan注解自动加载包含@Configuration的spring组件类。
2:导入XML配置
如果你必须使用XML来配置,我们建议您以@Configuration类开始。你可以稍后使用 @ImportResource 注解来加载XML配置。
第四部分:自动配置
spring boot企图自动配置你的spring应用程序建立在你的jar依赖之上。例如,如果HSQLDB在你的类路劲中,并且你没有手动的去配置任何数据库连接的bean,我们会自动配置内存数据库。
你需要选择加入自动配置通过使用@EnableAutoConfiguration 和@SpringBootApplication注解注入到你的一个@Configuration类中。
1:逐步更换自动配置
自动配置是非入侵的,在任何一点上你可以定义你自己的配置来替代原有的自动配置。例如,你添加你自己的DataSource Bean,那么默认的嵌入式数据库就会被替换掉。
若果你想找到什么样的自动配置当前被应用和为什么,将你的应用程序在debug模式下启动。这将会在控制台打印自动配置的日志。
2:禁用一些特定的自动配置
若果你觉得一些特定的自动配置不是你想要的,你可以使用 @EnableAutoConfiguration 的排除属性来禁用。
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
如果这个类不在你的类路径中,你可以使用此注解的excludeName属性来指定有效的类名。最后,你可有通过spring.autoconfigure.exclude 属性来屏蔽你想要的所有自动配置类。
第五部分:Spring bean和依赖注入
你可以自由使用任何标准的springframework技术来定义你的bean和它们的已注入的依赖关系。简单来说,我们通常使用@ComponentScan结合 @Autowired 注入的构造器来查找你定义的Bean。
如果你构建你的代码是按建议的来(将Application类文件放在root包中),你可以使用 @ComponentScan不需要任何参数。所有的应用程序组件(@Component,@Service, @Repository, @Controller etc.)都会被自动注册到Spring Beans中。
这有个案例,@Service被用于构造器注入来获取一个被注入的RiskAssessor Bean
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
第六部分:使用@SpringBootApplication注解
许多Spring boot开发者总是有个main类使用了@Configuration,@EnableAutoConfiguration and @ComponentScan注解。这些注解通常被绑定在一起使用,Spring boot提供一个@SpringBootApplication注解来选择。
@SpringBootApplication等同于使用@Configuration,@EnableAutoConfiguration,@ComponentScan的默认值。
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}