SpingBoot(二)——场景启动器
init项目 hello SpringBoot!的运行并非没用导入spring-core spring-web spring-webmvc等相关的jar包,而是将这些jar包根据应用分为不同的场景,由场景启动器管理自动导入,上述jar包包括tomcat都是由
<!--web场景启动器管理导入 pring-core spring-web spring-webmvc核心包,tomcat相关jar包,及json相关jar包等才使得 hello SpringBoot的运行,导入jar包的版本依然受到父项目的管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
自定义启动器
- 启动模块
普通maven项目即可只做依赖导入
<?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.springboot.starter</groupId>
<artifactId>me-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!--自动配置模块-->
<groupId>com.springboot.starter</groupId>
<artifactId>me-spring-boot-starter-autoconfigur</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 自动配置模块
<?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>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.starter</groupId>
<artifactId>me-spring-boot-starter-autoconfigur</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>me-spring-boot-starter-autoconfigur</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--必须的场景依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
新建一个配置类
/**
* application.properties中配置
* me.hello.prefix=XX
* me.hello.suffix=XX
*或application.yml配置方法则为
* me
* hello
* prefix=XX
* suffix=XX
*
*/
@ConfigurationProperties(prefix = "me.hello")
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;
}
}
编写service
public class HelloService {
//将配置引入
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
//方法参数 给一个姓名 返回欢迎语句 语句由三部分组成 前缀+name+后缀
public String sayHello(String name){
return helloProperties.getPrefix()+name+helloProperties.getSuffix();
}
}
编写自动配置类
@Configuration //定义配置类
@ConditionalOnWebApplication //在web环境中生效
@EnableConfigurationProperties(HelloProperties.class) //指明让HelloProperties来装载配置
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service=new HelloService();
/**
* 设置无配置时的默认配置
*/
if(helloProperties.getSuffix()==null){
helloProperties.setSuffix("!欢迎来到SpringBoot");
}
if(helloProperties.getPrefix()==null){
helloProperties.setPrefix("你好");
}
service.setHelloProperties(helloProperties);
return service;
}
}
配置META-INF/spring.factories
至此一个简单的场景启动器完成用maven依次导入本地仓库即可引用先导>me-spring-boot-starter-autoconfigur
在导me-spring-boot-starter
新建SpringBoot 导入web模块
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入自定义模块-->
<dependency>
<groupId>com.springboot.starter</groupId>
<artifactId>me-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String testHello(){
return helloService.sayHello("King");
}
}
即可实现
前后缀可在application.properties中配置
me.hello.prefix=前缀
me.hello.suffix=前缀