Spring Boot-Starter
一个starter其实就是一个maven工程,如图:
所以写一个starter先要新建一个maven工程,然后在引入依赖的包。新建maven项目的pom为:
<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.java.xrc</groupId>
<artifactId>SpringBootStarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- starter的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
HelloProperties类
新建项目完成后,在项目下新建一个包,在包下新建一个HelloProperties类,用来接收application.properties配置文件的信息,代码如下:
package com.java.xrc.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 这个配置类很好理解,将 application.properties 中配置的属性值直接注入到这个实例中,
* @ConfigurationProperties 类型安全的属性注入,即将 application.properties
*文件中前缀为 hello.config 的属性注入到这个类对应的属性上
*/
@ConfigurationProperties("hello.config")
public class HelloProperties {
private static final String DEFAULT_NAME = "zhangsan";
private static final String DEFAULT_MSG = "java";
private String name = DEFAULT_NAME;
private String msg = DEFAULT_MSG;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
HelloService类
然后在创建一个HelloService类,该类是一个starter的业务操作类,会将此类注入进spring容器,供项目调用。
package com.java.xrc.starter;
public class HelloService {
private String msg;
private String name;
public String sayHello() {
return name + " say " + msg + " !";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HelloServiceAutoConfiguration类
接下来就是写一个HelloServiceAutoConfiguration类,这个类是自动注入的核心类,将会将我们的参数类HelloService类与业务操作类HelloService进行绑定,然后向spring容器注入。
package com.java.xrc.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration//表明这是一个配置文件
@EnableConfigurationProperties(HelloProperties.class)//@EnableConfigurationProperties 注解是使我们之前配置的 @ConfigurationProperties 生效,让配置的属性成功的进入 Bean 中。
@ConditionalOnClass(HelloService.class)//HelloService类存在才生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean//将HelloService注入容器
HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setName(helloProperties.getName());
helloService.setMsg(helloProperties.getMsg());
return helloService;
}
}
spring.factories 的文件
每一个starter都会有一个spring.factories文件,来指定自动配置类,该类要放在一个META-INF 的文件夹下。在spring.factories文件写入内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.hello.config.mystarter.HelloServiceAutoConfiguration
整Starter项目结构如下:
打包上传至本地maven库
整个starter就构建成功,接下来打包上传至本地的maven库,在eclipse中打包可以使用maven插件工具,选择项目=》右键=》Run As=》Maven install 后会在项目路径下的target下生成项目的包。然后将包上传到本地maven库,使用maven命令:
mvn install:install-file -Dfile=D:\\SpringBootStarter-0.0.1-SNAPSHOT.jar -DgroupId=com.java.xrc -DartifactId=SpringBootStarter -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
后面的信息与项目pom文件的值一致。
项目使用
新建一个项目,在项目中pom引入:
<dependency>
<groupId>com.java.xrc</groupId>
<artifactId>SpringBootStarter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在application.properties中写入配置值:
hello.config.name=xrc
hello.config.msg=java web
在项目中使用注解,则可以使用starter的HelloService的对象了。
@Autowired
private HelloService helloService;