1.说明
springboot是一个高度集成spring和springmvc的框架,此外在使用其他框架如msyql、mybtis等时,
只需要添加他们的starter既可使用。
在此,笔者创建一个maven项目创建两个模块,一个模块打包成jar,然后在test模块中调用它的功能来发送请求。
2.创建工程
1.创建一个空项目,名为hello-spring-boot-starter
2.添加子模块,创建一个springboot工程,名为hello-spirng-boot-starter-autoconfigure
3.hello-spirng-boot-starter-autoconfigure的目录结构,删除启动类、test目录,在resources目录下创建META-INF/spring.factories文件,
同时删除pom文件里的test插件
3.编码
1.我们在hello-spirng-boot-starter-autoconfigure实现一个sayHello功能,然后在测试模块中调用。
创建HelloProperties 类,作为配置类,目的是后面使用这个自动配置,可以测试模块的application.properties 文件中配置:
package com.wyu.hellospirngbootstarterautoconfigure.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.wyu")
public class HelloProperties {
/**
* 默认name为xiaoming
*/
private String name = "xiaoming";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,则使用默认的,默认name为xiaoming
2.创建HelloService实现类
package com.wyu.hellospirngbootstarterautoconfigure.service;
import com.wyu.hellospirngbootstarterautoconfigure.properties.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getName() + " say hello to " + name;
}
}
3.创建HelloAutoConfig自动配置类
package com.wyu.hellospirngbootstarterautoconfigure.config;
import com.wyu.hellospirngbootstarterautoconfigure.properties.HelloProperties;
import com.wyu.hellospirngbootstarterautoconfigure.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({HelloProperties.class})
@Import(HelloService.class)
@ConditionalOnProperty(prefix = "com.wyu", value = "enabled", matchIfMissing = true)
public class HelloAutoConfig {
}
- @EnableConfigurationProperties:启用属性配置,将读取HelloProperties里面的属性。
- @ConditionalOnProperty:判断指定的属性是否具备指定的值。
4.打包hello-spirng-boot-starter-autoconfigure模块
通过mvn clean install将hello-spirng-boot-starter-autoconfigure打包成一个jar包
mvn clean install
5.测试
1.创建一个测试工程,添加我们自定义的starter的依赖
<dependency>
<groupId>com.wyu</groupId>
<artifactId>hello-spirng-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.在测试工程的application.properties配置文件中,我们可以添加配置
com.wyu.name=dacheng
3.编写controller
package com.wyu.test;
import com.wyu.hellospirngbootstarterautoconfigure.service.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 controller {
@Autowired
private HelloService helloService;
@RequestMapping("/sayHello")
public String sayHello(String name){
return helloService.sayHello(name);
}
}
4.运行测试工程的效果,我们可以看到在test通过添加hello-spirng-boot-starter-autoconfigure的starter成功调用其功能