栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实现自定义starter步骤

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实现自定义starter步骤

一、创建项目工程

在IDEA中创建一个空项目,两个moudle,分别是sunshine-spring-boot-starter和sunshine-spring-boot-starter-autoconfigurer

1、创建一个空项目 

 

 点击finish

 创建moudle

新建一个sunshine-spring-boot-starter   maven项目

 

 

 

 点击finish即可

再新建一个moudle为sunshine-spring-boot-starter-autoconfigurer

 

 

 

点击finish即可

 点击ok即可,项目创建完毕

 

 

二、实现自定义starter

(1)首先将sunshine-spring-boot-starter-autoconfigurer中启动类删除,以及test文件夹。这些可以不需要

pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.atguigu.starter
    atguigu-spring-boot-starter-autoconfigurer
    0.0.1-SNAPSHOT
    atguigu-spring-boot-starter-autoconfigurer
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter
        

    



1、创建HelloService

package com.atguigu.starter;

public class HelloService {

    HelloProperties helloProperties;

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

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

2、创建HelloProperties

package com.atguigu.starter;

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

@ConfigurationProperties(prefix = "atguigu.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、创建HelloServiceAutoConfiguration

package com.atguigu.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

4、创建meta_INF文件夹并创建spring.factories文件

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.atguigu.starter.HelloServiceAutoConfiguration

(2)在sunshine-spring-boot-starter的pom文件中引入atguigu-spring-boot-starter-autoconfigurer

pom文件



    4.0.0

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

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

三、测试

新建一个web项目

在application.properties添加内容

atguigu.hello.prefix=sunshine
atguigu.hello.suffix=Hello World

新建HelloController类

package com.atguigu.controller;

import com.atguigu.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.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.say("你好");
    }
}

测试项目可以正常启动 

在浏览器中输入http://localhost:8080/hello

结果为 sunshine-你好Hello World

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号