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

springboot自定义starter

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

springboot自定义starter

目录
  • 1.创建boot-07-hello-starter-autoconfigure模块
    • 1.1 创建实体类HelloProperties
    • 1.2 创建服务HelloService
    • 1.3 创建配置类HelloServiceAutoConfiguration
    • 1.4 在resources/meta-INF目录下创建spring.factories文件
    • 1.5 运行clean和install使之放入maven仓库中
  • 2. 创建boot-07-hello-starter模块
    • 2.1 在pom.xml文件中引入boot-07-hello-starter-autoconfigure依赖
    • 2.2 运行clean和install使之放入maven仓库中
  • 3. 创建boot-07-hello-test进行测试
    • 3.1 引入boot-07-hello-starter依赖
    • 3.2 创建RestController测试
    • 3.3 需要在配置文件中添加内容
  • 4. 运行测试
  • 5. 代码总思路

1.创建boot-07-hello-starter-autoconfigure模块 1.1 创建实体类HelloProperties
@ConfigurationProperties("szp.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;
    }
}
1.2 创建服务HelloService
public class HelloService {
    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String userName){
        return helloProperties.getPrefix() + ": " + userName + ">" + helloProperties.getSuffix();
    }
}

1.3 创建配置类HelloServiceAutoConfiguration
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)   //默认HelloProperties放在容器中
//当容器中没有HelloService的时候将其放入容器
public class HelloServiceAutoConfiguration {

    @Bean
    public HelloService helloService(){
        return new HelloService();
    }

}
1.4 在resources/meta-INF目录下创建spring.factories文件
#使之可以自动加载需要的配置文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.szp.hello.auto.HelloServiceAutoConfiguration
1.5 运行clean和install使之放入maven仓库中

2. 创建boot-07-hello-starter模块 2.1 在pom.xml文件中引入boot-07-hello-starter-autoconfigure依赖
		
        
            com.szp
            boot-07-hello-starter-autoconfigure
            0.0.1-SNAPSHOT
        
2.2 运行clean和install使之放入maven仓库中

3. 创建boot-07-hello-test进行测试 3.1 引入boot-07-hello-starter依赖
        
        
            org.szp
            boot-07-hello-starter
            1.0-SNAPSHOT
        
3.2 创建RestController测试
@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("hello")
    public String sayHello(){
        String s = helloService.sayHello("张三");
        return s;
    }
}
3.3 需要在配置文件中添加内容
szp.hello.prefix=szp
szp.hello.suffix=world
4. 运行测试

5. 代码总思路

前面的操作可使得引入maven依赖

配置类使得当容器中没有HelloService时将其放入容器,使其可以自动注入。且将HelloProperties放入容器中使其生效。

HelloProperties中@ConfigurationProperties(“szp.hello”)表明了会从配置文件的szp.hello获取prefix和suffix的内容。

HelloController中自动注入了HelloService,HelloService中又自动注入了HelloProperties。

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

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

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