starter
- 1. target
- 2. 手写启动器~
- 2.1 自动装配,自定义属性
- 2.2 启动器,引用自动装配模块
- 3. 在自己的项目引用上面的starter
1. target
1. 启动器只用来做依赖导入(导入配置模块)2. 专门来写一个自动配置模块3. 启动器依赖自动配置;别人只需要引入启动器(starter)xxx-spring-boot-starter; 自定义启动器名分析:
一个启动器需要两个模块,一个是自动装配模块,自动装配属性,定义好装配规则。
第二个是启动器引用自动配置模块。
将两个项目打包到maven仓库,或者私服仓库,
自己的其它项目引用启动器就行了,
在yaml或者properties中配置好启动器的属性就行了。
需要的注解
@Configuration 指定这是一个配置类@ConditionalOnXXX 指定条件成立下,自动配置类生效@AutoConfigureAfter 指定自动配置的顺序@Bean 给容器中添加组件@ConfigurationProperites 结合xxxProperties 类绑定相关的配置@EnableConfigurationProperties // 让xxxProperties生效加入容器中自动配置类如何能加载将需要启动就加载的自动配置类,配置在META-INF/spring.properteis 中
2. 手写启动器~
2.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 https://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.3.4.RELEASE</version><relativePath/> </parent><!-- 自动配置模块... --><groupId>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><name>bitqian-spring-boot-starter-autoconfigurer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies></project>
启动器的属性,boot中常见的xxxProperties类
package cn.bitqian.starter;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "bitqian.hello") // 配置别名
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;}
}
启动器功能方法
package cn.bitqian.starter;/*** @author echo lovely* @date 2020/11/8 11:39*/
public class HelloService {private HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}/*** 启动器的方法,配置前后缀,实现sayHello* @param name parameter* @return string*/public String sayHello(String name) {return helloProperties.getPrefix() + name + helloProperties.getSuffix();}}
自动装配类
package cn.bitqian.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 {@Autowiredprivate HelloProperties helloProperties;@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService;}}
创建META-INF文件夹,配置自动装配类,在spring.factories里面
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.bitqian.starter.HelloServiceAutoConfiguration
双击安装到maven仓库
2.2 启动器,引用自动装配模块
启动器模块,依赖自动配置模块
<?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>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><!-- bitqian springboot 启动器,依赖导入 --><dependencies><dependency><groupId>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>
安装到maven仓库
3. 在自己的项目引用上面的starter
- 创建springboot项目,引入依赖
- 编写controller
package cn.bitqian;import cn.bitqian.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DemoController {@AutowiredHelloService helloService;@RequestMapping("/hello")public String hello() {return "hello, " + helloService.sayHello(" rye ");}}
- 自动装配, 嘤嘤嘤
bitqian.hello.prefix=i like
bitqian.hello.suffix=so much
- so- - -