springboot自定义starter实现过程图解
这篇文章主要介绍了springboot自定义starter实现过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、创建一个EmptyProject
2、在该工程中点击+,选择newmodule,新建一个maven工程
点击确定。
3、在该工程中点击+,选择newmodule,新建一个SpringInitializr工程
后面直接默认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 DemoprojectforSpringBoot 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
packagecom.gong.starter; publicclassHelloService{ HelloPropertieshelloProperties; publicHelloPropertiesgetHelloProperties(){ returnhelloProperties; } publicvoidsetHelloProperties(HelloPropertieshelloProperties){ this.helloProperties=helloProperties; } publicStringsayHelloGong(Stringname){ returnhelloProperties.getPrefix()+"-"+name+helloProperties.getSuffix(); } }
sayHelloGong方法可以获取HelloProperties中的属性,包括前缀和后缀,然后返回:前缀-name后缀。
HelloProperties.java
packagecom.gong.starter; importorg.springframework.boot.context.properties.ConfigurationProperties; //绑定所有以gong.hello开头的配置 @ConfigurationProperties(prefix="gong.hello") publicclassHelloProperties{ privateStringprefix; privateStringsuffix; publicStringgetPrefix(){ returnprefix; } publicvoidsetPrefix(Stringprefix){ this.prefix=prefix; } publicStringgetSuffix(){ returnsuffix; } publicvoidsetSuffix(Stringsuffix){ this.suffix=suffix; } }
绑定配置以及定义属性。
HelloServiceAutoConfiguration.java
packagecom.gong.starter; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; importorg.springframework.boot.context.properties.EnableConfigurationProperties; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; @Configuration//是一个配置类 @ConditionalOnWebApplication//web应用才生效 @EnableConfigurationProperties(HelloProperties.class)//让属性文件生效 publicclassHelloServiceAutoConfiguration{ @Autowired HelloPropertieshelloProperties; @Bean publicHelloServicehelloService(){ HelloServiceservice=newHelloService(); service.setHelloProperties(helloProperties); returnservice; } }
自动配置类。要加入三个注解,并对方法使用@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=GONG gong.hello.suffix=HELLOWORLD
接着编写一个测试类:
packagecom.gong.springbootjpa.controller; importcom.gong.starter.HelloService; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassHelloController{ @Autowired HelloServicehelloService; @GetMapping("/hello") publicStringhello(){ returnhelloService.sayHelloGong("haha"); } }
启动服务器:
完美。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。