Spring Boot 学习笔记(六)—— 自定义starter

我们来完成一个例子,在工程中引入自定义的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值