SpringBoot:5.springboot原理深入了解

5.springboot原理深入了解

SpringBoot:5.springboot原理深入了解

自定义Starter:

  1. 新建一个空项目

  2. 在该项目中添加一个普通的maven模块

  3. 再改项目中的依赖中添加:

    <dependency>
        <groupId>com.ariverh</groupId>
        <artifactId>ariverh-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.4.0</version>
    </dependency>
    
  4. 再添加一个springboot模块

  5. 再pom中只留下

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
  6. 建立一下目录结构
    SpringBoot:5.springboot原理深入了解

  7. 编写各类:

    public class Hello {
        private HelloProperties helloProperties;
    
        public HelloProperties getHelloProperties() {
            return helloProperties;
        }
    
        public void setHelloProperties(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    
        public String getHello(){
            return helloProperties.getPrefix()+"-hello-"+helloProperties.getSuffix();
        }
    }
    
    @ConfigurationProperties(prefix = "ariverh.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;
        }
    }
    
    @Configuration
    @ConditionalOnWebApplication
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloAutoConfiguration {
        @Autowired
        HelloProperties helloProperties;
    
        @Bean
        public Hello hello(){
            Hello hello = new Hello();
            hello.setHelloProperties(helloProperties);
            return  hello;
        }
    }
    
    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.ariverh.mystarter.HelloAutoConfiguration
    
  8. 打包这两个模块

  9. 新建一个springboot项目

  10. 导入启动器

    <dependency>
        <groupId>com.ariverh</groupId>
        <artifactId>ariverh-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    
  11. 编写测试

@RestController
@RequestMapping("/ariverh")
public class HelloController {
    @Autowired
    Hello hello;
    @RequestMapping("/hello")
    public String hello(){
        String hello = this.hello.getHello();
        return hello;
    }
}
  1. 在application.properties中编写配置

    ariverh.hello.prefix=yyy
    ariverh.hello.suffix=sss
    
  2. 测试
    SpringBoot:5.springboot原理深入了解

上一篇:LeetCode题解(1292):元素和小于等于阈值的正方形的最大边长(Python)


下一篇:VS-Code用户代码片段 - 快捷生成代码