Springboot将常见的业务封装成一个个的starter(也加场景),例如:spring-boot-starter-web、spring-boot-starter-jpa、…
我们也可以将我们自己的业务封装成starter,然后打成jar包,这样别人也可以在项目中引用我们的starter
我们使用IDEA进行开发,但是IDEA中只能打开一个工程,而我们有三个项目,一个是我们的starter,一个是我们编写的autoconfiguration starter,还要一个用于测试
解决的办法是先在IDEA中创建一个空的project,然后将我们的项目都放入到空的project中
1、打开IDEA,创建一个空的项目用于存放项目
步骤:file->new->project->empty project->next->fonish
2、在空的工程中创建maven项目,名字叫做helloworld-spring-boot-starter,在maven项目中写starter
步骤:file->new->project structure->点击modules->点击界面左上角的’+’号选择new module->选择创建Maven项目
点击next
点击next,建议将Maven项目的名字按照Springboot要求的命名规范改成xxx-spring-boot-starter
点击finish,maven项目就创建成功了
3、在空的工程中创建一个Springboot项目,名字叫做helloworld-spring-boot-starter-autoconfigutate,用于编写starter的自动配置
步骤:在project structure界面选择modules->点击左上角的’+’->选择new module->选择Spring Initializr创建Springboot项目
点击next
点击next,我们先只选择web
点击next
点击finish,Springboot项目创建成功
生成的项目结构如下图所示,一个project中包含了两个项目
上面我们就把项目创建好了,下面我们来编写我们自己的starter。
4、开始编写autoconfigutate starter
开发helloworld-spring-boot-starter-autoconfigutate
1、删除主程序类(即带main方法的类)
2、删除resource路径下的application.properties配置文件和static、templates文件夹
3、修改pom.xml文件,只保留spring-boot-starter,这个starter是所有Springboot都必须有的基本starter
<?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>com.helloworld-starter</groupId>
<artifactId>helloworld-spring-boot-starter-autoconfigutate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld-spring-boot-starter-autoconfigutate</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.15.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
4、编写HelloworldProperties配置类
package com.starter.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
// 从配置文件中读取以helloworld开头的配置,并自动与HelloworldProperties中的属性绑定
@ConfigurationProperties(prefix = "helloworld")
public class HelloworldProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public String getSuffix() {
return suffix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
5、编写HelloworldService类
package com.starter.service;
import com.starter.properties.HelloworldProperties;
public class HelloworldService {
private HelloworldProperties helloworldProperties;
public HelloworldProperties getHelloworldProperties() {
return helloworldProperties;
}
public void setHelloworldProperties(HelloworldProperties helloworldProperties) {
this.helloworldProperties = helloworldProperties;
}
public String sayHello(String name) {
return helloworldProperties.getPrefix()+ "-" + name + "-" +helloworldProperties.getSuffix();
}
}
6、编写HelloworldAutoConfiguration类,将HelloworldService注入到Spring容器中
package com.starter.configuration;
import com.starter.properties.HelloworldProperties;
import com.starter.service.HelloworldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication // 只有是web项目才生效
@EnableConfigurationProperties(HelloworldProperties.class)
public class HelloworldAutoConfiguration {
@Autowired
private HelloworldProperties helloworldProperties;
// 将HelloworldService注入到spring容器中
@Bean
public HelloworldService helloworldService() {
HelloworldService helloworldService = new HelloworldService();
helloworldService.setHelloworldProperties(helloworldProperties);
return helloworldService;
}
}
7、在resource路径下创建spring.factories文件,并将我们自己的自动配置类添加进去,当Springboot启动是会自动执行我们的自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.starter.configuration.HelloworldAutoConfiguration
8、项目目录结构:
9、将helloworld-spring-boot-starter-autoconfigutate打成jar包放入maven仓库中
步骤:maven projects->helloworld-spring-boot-starter-autoconfigutate->Lifecycle ->install
5、helloworld-spring-boot-starter启动器
1、在pom.xml中引入helloworld-spring-boot-starter-autoconfigutate的坐标地址
<?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>com.helloworld.starter</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>com.helloworld-starter</groupId>
<artifactId>helloworld-spring-boot-starter-autoconfigutate</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2、项目的目录结构
3、将helloworld-spring-boot-starter项目打成jar包放入maven仓库中
步骤:maven projects->helloworld-spring-boot-starter->Lifecycle ->install
6、编写测试类
创建Springboot项目helloworld-spring-boot-starter-test测试类,使用我们自己的starter
1、在pom.xml中引入我们自己的starter
<!-- 引入自定义的starter -->
<dependency>
<groupId>com.helloworld.starter</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
helloworld-spring-boot-starter依赖于helloworld-spring-boot-starter-autoconfigutate
2、在application.properties配置文件中添加配置
helloworld.prefix=start
helloworld.suffix=end
3、编写HelloworldController测试
@RestController
public class HelloworldController {
@Autowired
private HelloworldService helloworldService;
@GetMapping(value = "/sayHello")
public String helloworld(String name) {
return helloworldService.sayHello(name);
}
}
4、浏览器访问结果