您当前的位置: 首页 >  ar

彭世瑜

暂无认证

  • 0浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot学习笔记-8:第八章 Spring Boot 自定义 starters

彭世瑜 发布时间:2020-08-10 09:51:43 ,浏览量:0

第八章 Spring Boot 自定义 starters

自动配置类

@Configuration   // 指定这个类是配置类
@Conditionalxxx  // 指定条件成立的情况下自动配置类生效
@AutoConfigureAfter // 指定自动配置类的顺序
@Bean // 给容器中添加组件
@ConfigurationProperties  // 结合相关Properties类来绑定相关的配置
@EnableConfigurationProperties // 让Properties生效加入到容器中

启动器 Starter

启动器模块是一个空的 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库

命名规约: 推荐使用一下命名规约

官方命名空间: 前缀:spring-boot-starter- 模式:spring-boot-starter-模块名 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间: 前缀:-spring-boot-starter 模式:模块名-spring-boot-starter 举例:mybatis-spring-boot-starter

自定义 starter

1、Idea 创建空工程 Empty Project

2、新建 spring 自动配置模块

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
        
        
    
    com.mouday
    mouday-spring-boot-starter-autoconfigurer
    0.0.1-SNAPSHOT
    mouday-spring-boot-starter-autoconfigurer
    Demo project for Spring Boot

    
        1.8
    

    
    
        
            org.springframework.boot
            spring-boot-starter
        
    

属性文件 HelloProperties.java

package com.mouday.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;


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

HelloService.java

package com.mouday.starter;

public class HelloService {
    HelloProperties properties;

    public String  sayHello(String name){
        return properties.getPrefix() + name + properties.getSuffix();
    }

    public HelloProperties getProperties() {
        return properties;
    }

    public void setProperties(HelloProperties properties) {
        this.properties = properties;
    }
}

配置类

package com.mouday.starter;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setProperties(helloProperties);
        return service;
    }
}

src/main/resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mouday.starter.HelloServiceAutoConfiguration

3、新建 maven starter 模块

pom.xml



    4.0.0

    com.mouday
    mouday-spring-boot-starter
    1.0-SNAPSHOT

    
        
            com.mouday
            mouday-spring-boot-starter-autoconfigurer
            0.0.1-SNAPSHOT
        
    

测试

引入依赖 pom.xml


    com.mouday
    mouday-spring-boot-starter
    0.0.1-SNAPSHOT

修改配置文件 application.properties

mouday.hello.prefix=hello
mouday.hello.suffix=hi

新建测试 Controller

package com.example.demo.controller;

import com.mouday.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired
    HelloService helloService;

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

http://localhost:8080/hello?name=Tom

访问结果

helloTomhi

学习新技术:

一看文档,二看源码

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.3725s