16-自定义starter
自定义stater
1、starter说明
启动器模块是一个空的jar文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库
2、starter命名规约
-
官方命名方式
前缀:spring-boot-starter-xxx,如spring-boot-starter-web...
-
自定义命名
前缀:xxx-spring-boot-starter,如mybatis-spring-boot-starter
3、实现步骤
1、我们在idea-springboot项目中新建一个普通的maven模块:jie-spring-boot-starter
2、然后再创建一个springboot模块:jie-spring-boot-starter-autoconfigure
IDEA创建Spring Boot项目无法连接http://start.spring.io 解决方法:在custom里写http://start.aliyun.com
3、基本框架如下
4、在我们到starter模块中导入autoconfigure模块依赖
6、将autoconfigure模块中多余文件删掉,pom文件中只留下一个spring官网的starter,这是所有启动器的基本配置,如下
7、我们在自动配置模块中编写一个服务类HelloService和一个属性配置类HelloProperties
package com.study;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+name+helloProperties.getSuffix();
}
}
package com.study;
import org.springframework.boot.context.properties.ConfigurationProperties;
//前缀使用"com.study"
@ConfigurationProperties("com.study")
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;
}
}
8、编写自动配置类,将HelloProperties这个属性配置类以及服务类HelloService注入bean,然后测试
package com.study;
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
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnWebApplication//当项目市web应用的时候才生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
9、在resources目录下新建一个自己的META-INF/spring.factories
#Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.study.HelloServiceAutoConfiguration
10、编写完成后将两个模块安装到打包安装到maven仓库中
需要注意的是,之前我们说过,这个starter就是一个空壳,一个空的jar包,正真器作用的事这个starter中pom文件导入的这个autoconfigure依赖。所以我们在安装这两个模块是由顺序的,由于stater依赖autoconfigure,所以autoconfigure要先被安装到仓库中,然后再安装starter模块。
11、新建一个springboot项目测试我们的启动器
因为我们的启动器需只能在web应用中才会生效,所以需要添加web相关jar包
在项目pom文件中添加自定义的starter
然后编写一个控制类HelloController,来测试我们自己在starter中写的接口(虽然说在autoconfigure中写的,但是现在他们说一个整体,叫stater)
package com.study.controller;
import com.study.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloController {
@Resource
HelloService helloService;
@RequestMapping("/hello")
public String hello(){
return helloService.sayHello("<-自定义stater->");
}
}
然后我们在配置文件中修改HelloProperties中的属性值
com.study.prefix="666"
com.study.suffix="mmm"
启动项目,输入URL,结果如下
从上图很明显,我们的stater生效了,可以在另一个springboot项目中被调用。