前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot自定义starter

springboot自定义starter

原创
作者头像
无敌小菜鸟
发布2022-02-23 11:14:35
8100
发布2022-02-23 11:14:35
举报
文章被收录于专栏:搬砖笔记

学习springboot自定义starter

开发starter

第一步:创建starter工程hello-spring-boot-starter并配置pom.xml文件

代码语言:javascript
复制
 <?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>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.2.2.RELEASE</version>
         <relativePath/>
     </parent>
     <groupId>cn.itcast</groupId>
     <artifactId>hello-spring-boot-starter</artifactId>
     <version>1.0-SNAPSHOT</version>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-autoconfigure</artifactId>
         </dependency>
     </dependencies>
 </project>

第二步:创建配置属性类HelloProperties

代码语言:javascript
复制
 package cn.itcast.config;
 
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
 /*
  *读取配置文件转换为bean
  * */
 @ConfigurationProperties(prefix = "hello")
 public class HelloProperties {
     private String name;
     private String address;
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this.name = name;
     }
 
     public String getAddress() {
         return address;
     }
 
     public void setAddress(String address) {
         this.address = address;
     }
 
     @Override
     public String toString() {
         return "HelloProperties{" +
                 "name='" + name + '\'' +
                 ", address='" + address + '\'' +
                 '}';
     }
 }

第三步:创建服务类HelloService

代码语言:javascript
复制
 package cn.itcast.service;
 
 public class HelloService {
     private String name;
     private String address;
 
     public HelloService(String name, String address) {
         this.name = name;
         this.address = address;
     }
 
     public String sayHello(){
         return "你好!我的名字叫 " + name + ",我来自 " + address;
     }
 }

第四步:创建自动配置类HelloServiceAutoConfiguration

代码语言:javascript
复制
 package cn.itcast.config;
 
 import cn.itcast.service.HelloService;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 /*
 * 配置类,基于Java代码的bean配置
 * */
 
 @Configuration
 @EnableConfigurationProperties(HelloProperties.class)
 public class HelloServiceAutoConfiguration {
     private HelloProperties helloProperties;
 
     //通过构造方法注入配置属性对象HelloProperties
     public HelloServiceAutoConfiguration(HelloProperties helloProperties) {
         this.helloProperties = helloProperties;
     }
 
     //实例化HelloService并载入Spring IoC容器
     @Bean
     @ConditionalOnMissingBean
     public HelloService helloService(){
         return new HelloService(helloProperties.getName(),helloProperties.getAddress());
     }
 }

第五步:在resources目录下创建META-INF/spring.factories

代码语言:javascript
复制
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 cn.itcast.config.HelloServiceAutoConfiguration

至此starter已经开发完成了,可以将当前starter安装到本地maven仓库供其他应用来使用。

使用starter

第一步:创建maven工程myapp并配置pom.xml文件

代码语言:javascript
复制
 <?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>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.2.2.RELEASE</version>
         <relativePath/>
     </parent>
     <groupId>cn.itcast</groupId>
     <artifactId>myapp</artifactId>
     <version>1.0-SNAPSHOT</version>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <!--导入自定义starter-->
         <dependency>
             <groupId>cn.itcast</groupId>
             <artifactId>hello-spring-boot-starter</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
     </dependencies>
 </project>

第二步:创建application.yml文件

代码语言:javascript
复制
 server:
   port: 8080
 hello:
   name: xiaoming
   address: beijing

第三步:创建HelloController

代码语言:javascript
复制
 package cn.itcast.controller;
 
 import cn.itcast.service.HelloService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 @RequestMapping("/hello")
 public class HelloController {
     //HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入
     @Autowired
     private HelloService helloService;
 
     @GetMapping("/say")
     public String sayHello(){
         return helloService.sayHello();
     }
 }

第四步:创建启动类HelloApplication

代码语言:javascript
复制
 package cn.itcast;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
 public class HelloApplication {
     public static void main(String[] args) {
         SpringApplication.run(HelloApplication.class,args);
     }
 }

执行启动类main方法,

访问地址:

http://localhost:8080/hello/say

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开发starter
  • 使用starter
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档