如果我的博客能够帮到大家能够点个赞,关注一下,以后还会更新更过JavaWeb的高级技术,大家的支持就是我继续更新的动力。谢谢咯。
有时候,我们在使用SpringBoot的时候很多都是使用SpringBoot已经写好的场景启动器,但是这些启动只能满足一些常用的大部分功能,但是我们想要完成自己的一些小功能,并不能完成,所以我们需要自己定义场景启动器来完成我们需要完成的功能。
以下介绍一下,SpringBoot自定义场景启动器的用法。
一、搭建环境
- 创建一个Mavn的空工程
- 创建一个Maven用来定义场景启动器
- 定义一个SpringBootInitalizer 来实现自动配置
- 完成之后,删除Maven工程的src 和test 目录,删除springboot工程的test目录,和所有配置文件中的插件依赖,结构如下:
- 创建一个HelloProperties如下:
package com.nyist.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "nyist.hello") //用于绑定 yml 和 实体类的属性
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
- HelloService.java
package com.nyist.starter;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHelloNyist(String name){
return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
}
}
- HelloServiceAutoConfiguration.java
package com.nyist.starter;
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.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //web 应用才生效
@EnableConfigurationProperties(HelloProperties.class) //让属性文件生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean //加入到容器中去
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
- spring.factories 用来配置应用启动时候会自动启动的配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nyist.starter.HelloServiceAutoConfiguration
- pom.xml 依赖如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nyist.starter</groupId>
<artifactId>nyist-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nyist-spring-boot-starter-autoconfigure</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入 spring-boot-starter 所有自动配置器 都需要引入的配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
- nyist-spring-boot-stater 工程下的pom.xml文件如下:
<?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.nyist.staryer</groupId>
<artifactId>nyist-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--引入自动配置-->
<dependency>
<groupId>com.nyist.starter</groupId>
<artifactId>nyist-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
完成以上操作之后,将这些模块安装到maven仓库中去
二、创建新工程
1.创建一个新的SpringBoot工程,测试定义得 自定义得启动器是否能用
工程结构如下:
- Controller如下:
package com.nyist.springboot08startertest.controller;
import com.nyist.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloContoller {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHelloNyist("哈哈哈-");
}
}
- 运行结果: