zoukankan      html  css  js  c++  java
  • SpringBoot编写自定义Starter

    根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库

    1.建立一个empty工程,建立一个普通maven模块xxxStarter,建立一个SpringBoot模块xxxAutoConfigurer,前者依赖后者

    2.xxxAutoConfigurer:

    新建:HelloService:

    public class HelloService {
    
        private HelloProperties helloProperties;
    
        public void setHelloProperties(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
        public HelloProperties getHelloProperties() {
            return helloProperties;
        }
    
        public String hello(){
            return StringFormatter.format("%s:你好,%s欢迎光临",helloProperties.getWeekend(),helloProperties.getName()).getValue();
        }
    }

    新疆:HelloProperties类

    @ConfigurationProperties(prefix = "brx")
    public class HelloProperties {
    
        private String weekend;
    
        private String name;
    
        public String getWeekend() {
            return weekend;
        }
    
        public void setWeekend(String weekend) {
            this.weekend = weekend;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    新建:将启动类修改为一个配置类,并去除pom.xml中的maven插件

    @Configuration
    @EnableConfigurationProperties(HelloProperties.class)//只有在web上下文才起作用
    @ConditionalOnWebApplication
    public class BrxautoconfigApplication {
    
        @Autowired
        HelloProperties helloProperties;
    
        @Bean
        HelloService helloService(){
            HelloService helloService =  new HelloService();
            helloService.setHelloProperties(helloProperties);
            return helloService;
        }
    }

    新建:META-INF/spring.factories:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.brx.demo.brxautoconfig.BrxautoconfigApplication

    3.将starter和Autocofigure项目install到本地仓库

    4.新建一个普通web项目,并添加xxxStarter库,并在application.properties添加:

    brx.weekend="周一"
    brx.name="白gg"

    个人感觉:starter的意义就是把一些内部公用的代码抽成starter,共其他项目引入,并提供自动配置等功能

  • 相关阅读:
    mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
    centos6.5下安装samba服务器与配置
    centos 6.5 安装图形界面【转】
    Linux 下添加用户,修改权限
    Linux下自动调整时间和时区与Internet时间同步
    C#下利用封包、拆包原理解决Socket粘包、半包问题(新手篇)
    Unity脚步之NetworkBehaviour下前进、后退、左右转向的简单移动
    Token 在 Ajax 请求头中,服务端过滤器跨域问题
    【游戏】【暗黑2】重置属性点和技能点
    ASCII
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10287338.html
Copyright © 2011-2022 走看看