资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot中如何自定义starter-创新互联

springboot中如何自定义starter,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、虚拟空间、营销软件、网站建设、曲麻莱网站维护、网站推广。

1、创建一个Empty Project

2、在该工程中点击+,选择new module,新建一个maven工程

点击确定。

3、在该工程中点击+,选择new module,新建一个Spring Initializr工程

后面直接默认next,然后点击finishi。

两个都创建完毕之后点击apply,点击OK。得到如下结构:

4、在gong-spring-boot-starter中引入gong-spring-boot-starter-autoconfigurer,即在gong-spring-boot-starter的pom.xml中

 4.0.0  com.gong.starter  gong.spring-boot-starter  1.0-SNAPSHOT                com.gong.starter      gong-spring-boot-starter-autoconfigurer      0.0.1-SNAPSHOT      

我们看下gong-spring-boot-starter-autoconfigurer中的pom.xml

 4.0.0      org.springframework.boot    spring-boot-starter-parent    2.2.4.RELEASE        com.gong.starter  gong-spring-boot-starter-autoconfigurer  0.0.1-SNAPSHOT  gong-spring-boot-starter-autoconfigurer  Demo project for Spring Boot      1.8                  org.springframework.boot      spring-boot-starter      

删除掉创建项目时自动配置的其它东西,只需要一个标红的依赖即可。

5、在gong-spring-boot-starter-autoconfigurer就可以编写场景启动的相关逻辑啦。

(1)新建如下目录结构及文件

springboot启动入口可以删去,resources下文件删去,test文件夹删去。在com.gong.starter下新建以上三个java文件,在resources下新建META-INF文件夹,再新建spring.factories文件。

HelloService.java

package com.gong.starter;public class HelloService {  HelloProperties helloProperties;  public HelloProperties getHelloProperties() {    return helloProperties;  }  public void setHelloProperties(HelloProperties helloProperties) {    this.helloProperties = helloProperties;  }  public String sayHelloGong(String name){    return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();  }}

sayHelloGong方法可以获取HelloProperties中的属性,包括前缀和后缀,然后返回:前缀-name后缀。

HelloProperties.java

package com.gong.starter;import org.springframework.boot.context.properties.ConfigurationProperties;//绑定所有以gong.hello开头的配置@ConfigurationProperties(prefix = "gong.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;  }}

绑定配置以及定义属性。

HelloServiceAutoConfiguration.java

package com.gong.starter;import org.springframework.beans.factory.annotation.Autowired;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 service = new HelloService();    service.setHelloProperties(helloProperties);    return service;  }}

自动配置类。要加入三个注解,并对方法使用@Bean标注。

最后,就是在spring.factories中进行配置自动注册:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.gong.starter.HelloServiceAutoConfiguration

这样,我们自己定义的场景启动器就完成了。

接下来新建一个springboot项目进行测试,首先在pom.xml中导入自己定义的场景启动器:

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

然后编写application.properties定义场景启动器的属性:

gong.hello.prefix=GONGgong.hello.suffix=HELLO WORLD

接着编写一个测试类:

package com.gong.springbootjpa.controller;import com.gong.starter.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {  @Autowired  HelloService helloService;  @GetMapping("/hello")  public String hello(){    return helloService.sayHelloGong("haha");  }}

看完上述内容,你们掌握springboot中如何自定义starter的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站栏目:springboot中如何自定义starter-创新互联
转载来于:http://cdkjz.cn/article/csspcc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220