1,创建一个空工程
2,在new Module创建一个maven工程
3,再new Module创建一个Spring Initializr
4,点击Apply,OK
5,在my-spring-boot-starter的pom.xml文件中引入myproject-spring-boot-starter-autoconfig自动配置包
<groupId>com.myproject.starter</groupId>
<artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<?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.myproject.starter</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.myproject.starter</groupId>
<artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
6,删掉没用的目录结构,及主程序启动类,删掉test目录,test依赖及插件;就引入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 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>1.5.21.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myproject.starter</groupId>
<artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myproject-spring-boot-starter-autoconfig</name>
<description>Demo project for Spring Boot</description>
<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>
7,编写测试用的配置类
@ConfigurationProperties(prefix = "myproject.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getSuffix() {
return suffix;
}
public String getPrefix() {
return prefix;
}
}
public class HelloService {
HelloProperties helloProperties;
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
}
}
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
8,要让配置类启动生效需要在类路径下创建文件夹及配置文件:META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.myproject.starter.HelloAutoConfiguration
9,点击IDEA右侧的Maven Projects,将自定义完成的starter安装到仓库中,由于my-spring-boot-starter依赖的myproject-spring-boot-starter-autoconfig;所以先将myproject-spring-boot-starter-autoconfig安装
10,安装成功后,新创建一个Spring Boot项目,将web组件勾上,测试starter启动是否正常运行:
11,在pom文件中引入自定义的starter
<dependency>
<groupId>com.myproject.starter</groupId>
<artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
12,编写一个测试的Controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHello("boot");
}
}
13,可以在.properties配置文件中配置-前后缀
myproject.hello.prefix=myProject
myproject.hello.suffix=Good
14,然后启动应用,访问:localhost:8080/hello
OK!没问题!
总结:
1,创建一个启动器,只用来做依赖导入
2,写一个自动配置模块
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3,启动器依赖自动配置,使用只需要引入启动器(starter)
Over!
可以结合前面的文章学习Spring Boot!