我们来完成一个例子,在工程中引入自定义的starter,在不做任何配置的情况下,请求 一个链接,自动输出自我介绍
1. 创建starter,添加依赖
<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>cn.fg</groupId>
<artifactId>person-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2. 创建自动配置类
package person.spring.boot.starter.autoconfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //必须是一个web应用,该自动配置才生效
@EnableConfigurationProperties(PersonProperties.class)
@ComponentScan("person.spring.boot.starter")
public class PersonAutoconfigurer {
}
package person.spring.boot.starter.autoconfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "person")
public class PersonProperties {
private String name = "张三";
private Integer age = 18;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package person.spring.boot.starter.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import person.spring.boot.starter.autoconfigurer.PersonProperties;
@Service
public class PersonService {
@Autowired
PersonProperties personProperties;
public String introduce() {
return "大家好,我的名字叫" + personProperties.getName() + ",今年" + personProperties.getAge() + "岁!";
}
}
package person.spring.boot.starter.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import person.spring.boot.starter.service.PersonService;
@Controller
@RequestMapping("person")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("introduce")
@ResponseBody
public String introduce(String name) {
return personService.introduce();
}
}
3. 创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
person.spring.boot.starter.autoconfigurer.PersonAutoconfigurer
4. 创建好后的工程结构
5. 在其他工程中引入starter
<dependency>
<groupId>cn.fg</groupId>
<artifactId>person-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
6. 请求http://127.0.0.1:8080/person/introduce
7. 添加配置换一个人介绍
person:
name: 李四
age: 25