写点什么

【Spring Boot 6】自定义 starter,花了6个月肝完阿里技术官的笔记

  • 2021 年 11 月 10 日
  • 本文字数:1704 字

    阅读完需:约 6 分钟

![](https://img-blog.cs


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


dnimg.cn/2020072813550956.jpg)




4、内部代码



(1)spring-boot-starter-autoconfigurer module

package com.springboot.starter;


import org.springframework.beans.factory.annotation.Autowired;


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


import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;


/**


  • @author:素小暖

  • @date:2020/2/9

  • @desc:


*/


@Configuration


@EnableConfigurationProperties(HelloProperties.class)


public class HelloAuto {


@Autowired


HelloProperties helloProperties;


@Bean


public HelloService helloService() {


HelloService helloService = new HelloService();


helloService.setHelloProperties(helloProperties);


return helloService;


}


}


package com.springboot.starter;


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


/**


  • @author:素小暖

  • @date:2020/2/9

  • @desc:


*/


@ConfigurationProperties(prefix = "springboot.hello")


public class HelloProperties {


private String name;


public String getName() {


return name;


}


public void setName(String name) {


this.name = name;


}


}


package com.springboot.starter;


/**


  • @author:素小暖

  • @date:2020/2/9

  • @desc:


*/


public class HelloService {


HelloProperties helloProperties;


public String hello() {


return "hello" + helloProperties.getName();


}


public HelloProperties getHelloProperties() {


return helloProperties;


}


public void setHelloProperties(HelloProperties helloProperties) {


this.helloProperties = helloProperties;


}


}


spring.properties 文件


org.springframework.boot.autoconfigure.EnableAutoConfiguration=\


com.springboot.starter.HelloAuto


POM 文件


<?xml version="1.0" encoding="UTF-8"?>


<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>com.springboot</groupId>


<artifactId>spring-boot-starter-autoconfigurer</artifactId>


<version>0.0.1-SNAPSHOT</version>


<packaging>jar</packaging>


<name>spring-boot-starter-autoconfigurer</name>


<description>Demo project for Spring Boot</description>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.0.4.RELEASE</version>


</parent>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>


<java.version>1.8</java.version>


</properties>


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter</artifactId>


</dependency>


</dependencies>


</project>

(2)?springbootstarter

启动器比较简单,只有 pom 文件,引入自动配置即可。


<?xml version="1.0" encoding="UTF-8"?>


<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>wms</groupId>


<artifactId>springboot-starter</artifactId>


<version>1.0-SNAPSHOT</version>


<dependencies>


<dependency>


<groupId>com.springboot</groupId>


<artifactId>spring-boot-starter-autoconfigurer</artifactId>


<version>0.0.1-SNAPSHOT</version>


</dependency>


</dependencies>


</project>


5、?测试,新建 springboot 项目,pom 文件中引入启动器




<dependency>


<groupId>springboot</groupId>

评论

发布
暂无评论
【Spring Boot 6】自定义starter,花了6个月肝完阿里技术官的笔记