SpringBoot(10)—— 自定义Starter(重要!!!)

自定义Starter(重要!!!)


自定义Starter

项目–>自定义Starter–>自定义Starter-autoconfigurer
1. 创建自定义Starter-autoconfigurer项目(自动配置模块)

pom.xml:引入spring-boot-starter所有starter的基本配置

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
2. 在Starter-autoconfigurer项目下创建HelloProperties自定义属性配置

(1)@ConfigurationProperties(prefix = "lemon.hello"):指定属性配置前缀为lemon.hello,用于绑定配置文件中以lemon.hello开头的配置
(2)需要添加get,set方法才能进行使用

HelloProperties

@ConfigurationProperties(prefix = "lemon.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;
    }
}

3. 在Starter-autoconfigurer项目下创建HelloService,用于编写实际方法

HelloService

public class HelloService {

    HelloProperties helloProperties;

    public String sayHello(String name){
        return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}
4. 在Starter-autoconfigurer项目下创建HelloServiceAutoConfiguration自动配置类,给容器中添加HelloService组件

(1)@Configuration:标注这是一个配置类
(2)@ConditionalOnWebApplication:只有在Web应用下才生效
(3)@EnableConfigurationProperties(HelloProperties.class):让HelloProperties配置生效

HelloServiceAutoConfiguration

@Configuration
@ConditionalOnWebApplication // Web应用才生效
@EnableConfigurationProperties(HelloProperties.class) // 让HelloProperties配置生效
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();

        service.setHelloProperties(helloProperties);

        return service;
    }
}
5. 在Starter-autoconfigurer项目下配置加载自己创建的自动配置类

application.yml

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.lemon.starter.HelloServiceAutoConfiguration
6. 创建自定义Starter(启动器模块)

只需要引入自定义的自动配置模块自定义Starter-autoconfigurer
pom.xml

	<dependency>
	    <groupId>com.lemon.starter</groupId>
	    <artifactId>lemon-spring-boot-starter-autoconfigurer</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	</dependency>
7. 使用Maven对这两个自定义模块进行install
8. 测试

(1)pom.xml:只需要引入自定义Starter启动器即可

	<dependency>
	    <groupId>com.lemon.starter</groupId>
	    <artifactId>lemon-spring-boot-starter</artifactId>
	    <version>1.0-SNAPSHOT</version>
	</dependency>

(2)创建HelloController类进行测试,自动注入HelloService即可调用sayHello方法

HelloController

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello("haha");
    }

}

(3)在全局配置文件中配置HelloService的属性(前后缀)

application.properties

lemon.hello.prefix=Lemon
lemon.hello.suffix=HELLO WORLD

效果:
访问http://localhost:8080/hello,显示Lemon-hahaHELLO WORLD

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值