13.自定义starter启动器

starter:

​ 1、这个场景需要使用到的依赖是什么?

​ 2、如何编写自动配置

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
//自动配置类要能加载
//将需要启动就加载的自动配置类,配置在META-INF/spring.factories
eg:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

​ 3、模式:

启动器只用来做依赖导入;

专门来写一个自动配置模块;

启动器依赖自动配置;别人只需要引入启动器(starter)

命名:自定义启动器名-spring-boot-starter eg:mybatis-spring-boot-starter;

步骤:

1)、启动器模块

引入自动配置模块依赖

<?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.stt.mystarter</groupId>
<artifactId>home-springboot-mystarter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.stt.mystarter</groupId>
<artifactId>home-springboot-myspringbootstarter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

2)、自动配置模块

引入spring-boot-starter依赖,这是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 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>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stt.mystarter</groupId>
<artifactId>home-springboot-myspringbootstarter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>home-springboot-myspringbootstarter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter依赖,所有starter基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

自动配置类删除启动类与主配置文件:

组件类:HelloService

package com.stt.mystarter;
public class HelloService {
// 注入属性类,提供get、set方法
private HelloProperites helloProperites;
public HelloProperites getHelloProperites() {
return helloProperites;
}
public void setHelloProperites(HelloProperites helloProperites) {
this.helloProperites = helloProperites;
}
// 配置组件功能
public String sayHello(String name){
return helloProperites.getPrefix()+"-"+name+"-"+helloProperites.getSuffix();
}
}

属性类:HelloProperites

package com.stt.mystarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "myspring.mystarter")
public class HelloProperites {
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;
}
}

自动配置类:HelloServiceAutoConfigration

package com.stt.mystarter;
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(HelloProperites.class) // 关联HelloProperites
public class HelloServiceAutoConfigration {
@Autowired
HelloProperites helloProperites;
@Bean // 组件类注入容器中
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperites(helloProperites);
return helloService;
}
}

配置类路径文件:

自动配置类要能加载,将需要启动就加载的自动配置类,配置在META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.stt.mystarter.HelloServiceAutoConfigration // 自动配置类的全路径名

先将自动配置类模块安装到本地仓库中,再安装启动器模块:

测试: 新建工程

pom.xml

<!--引入自定义的starter-->
<dependency>
<groupId>com.stt.mystarter</groupId>
<artifactId>home-springboot-mystarter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

controller:

package com.example.demo.controller;
import com.stt.mystarter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello/{name}")
public String printHello(@PathVariable("name") String name){
return helloService.sayHello(name);
}
}

application.properties

myspring.mystarter.prefix=sxl
myspring.mystarter.suffix=sh

结果:

posted @   Lz_蚂蚱  阅读(45)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起