什么是 SpringBoot
早期的 Spring 是通过 XML 的方式进行配置为项目提供了良好的灵活性和扩展性,但是随着项目的发展,大量的 XML 文件存在于项目之中,导致 Java 项目变得越来越庞大,管理和维护起来非常不易,后来随着注解的流行,spring 提供了注解配置的支持,注解大量简化了 spring 的配置,但依然没有彻底解决配置的繁杂问题。一个时代有一个时代的产物,随着技术的发展,Spring社区推出了Spring Boot,它的理念是约定大于配置,通过这种方式实现自动配置,降低项目搭建的复杂度,从而使开发人员不再需要定义复杂的配置。
SpringBoot 优点
1、自动配置(auto-configuration)
通过扫描 Starter 包的 META-INF 目录下 spring.factories 文件,实现加载自动化配置类。spring.factories 具体的配置方式后面讲解。
2、Starter 组件集成
通过 Starter 的方式集成各种三方组件,比如 mybatis、hibernate、redis、web、freemarker等。当然这都需要三方组件提供相应的 Starter 组件。Starter 的方式是需要整个业务进行支持的,如果大家都不开发 Starter,SpringBoot 是没法使用这些组件的。然而大家对 SpringBoot 的理念都是认同的,现在的三方组件基本上都有提供 SpringBoot 支持。
上面简单的介绍了 SpringBoot,基于 SpringBoot 我们可以很容易的配置三方插件,同时 SpringBoot 也提供了自定义 SpringBoot Starter 的方法,接下来我们进入主题,通过代码讲解如何自定义SpringBoot Starter。
自定义SpringBoot Starter
1、引入 SpringBoot 自动化配置依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>org.learn</groupId>
<artifactId>spring-boot-starter-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
2、创建配置属性类
1、ConfigurationProperties 为配置属性类,application.properties 文件配置的所有属性都是通过它来读取的
2、prefix = “helloworld” 代表该属性对象在 application.properties 中的前缀,
3、在配置文件中 helloworld.name=springboot,就可以改变 name 的值。
@ConfigurationProperties(prefix = "helloworld")
public class HelloworldProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接下来我们就可以通过 @Autowired 进行注入并使用了。
3、创建 Starter 包的服务类
public class HelloworldService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String sayHello() {
return "hello," + name;
}
}
4、创建自动配置类
@Configuration
/// 当 HelloworldService 在类路径的条件下
@ConditionalOnClass({HelloworldService.class})
/// 将 application.properties 文件配置的属性进行映射
@EnableConfigurationProperties(HelloworldProperties.class)
public class HelloworldAutoConfiguration {
@Autowired
private HelloworldProperties hellowordProperties;
@Bean
/// 当容器不存在 HelloworldService 类的时候进行创建
@ConditionalOnMissingBean(HelloworldService.class)
public HelloworldService helloworldService() {
HelloworldService helloworldService = new HelloworldService();
helloworldService.setName(hellowordProperties.getName());
return helloworldService;
}
}
5、配置 spring.factories
在 META-INF 目录下创建 spring.factories 文件, SpringBoot 通过扫描 META-INF/spring.factories 文件来加载项目的自动化配置类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.learn.HelloworldAutoConfiguration
执行 mvn install 将应用打包只本地工程,接下来我们创建一个新工程测试我们的 Starter。
6、测试 Starter
1、配置 pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<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>org.learn</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.learn</groupId>
<artifactId>spring-boot-starter-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.14.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.learn</groupId>
<artifactId>spring-boot-starter-helloworld</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2、配置 application.properties 文件
helloworld.name = springboot
3、编写启动类
package org.learn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zhibo
* @date 2019/5/15 17:28
*/
@RestController
@SpringBootApplication
public class HelloworldApplication {
@Autowired
private HelloworldService helloworldService;
@GetMapping("/")
public String sayHello() {
return helloworldService.sayHello();
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
4、通过 main 方法启动工程,在浏览器地址栏中输入http://localhost:8080/
,显示结果如下图所示:
成功调用 spring-boot-starter-helloworld 模块。
本文对于 SpringBoot 的注解没有做详细的解释,适合具有一定 SpringBoot 编程能力的同学阅读。文章内容仅代表个人观点,如有不正之处,欢迎批评指正,谢谢大家。