springboot构建自己的starter.docx

上传人:b****7 文档编号:23985881 上传时间:2023-05-23 格式:DOCX 页数:6 大小:16.32KB
下载 相关 举报
springboot构建自己的starter.docx_第1页
第1页 / 共6页
springboot构建自己的starter.docx_第2页
第2页 / 共6页
springboot构建自己的starter.docx_第3页
第3页 / 共6页
springboot构建自己的starter.docx_第4页
第4页 / 共6页
springboot构建自己的starter.docx_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

springboot构建自己的starter.docx

《springboot构建自己的starter.docx》由会员分享,可在线阅读,更多相关《springboot构建自己的starter.docx(6页珍藏版)》请在冰豆网上搜索。

springboot构建自己的starter.docx

springboot构建自己的starter

springboot构建自己的starter

前言

下面我们来通过示例来构建自己的starterpom并完成自动配置,以此来更深入的理解springboot的工作原理。

需求

该starter提供PersonService,并实现自动配置PersonService

一、新建一个Maven工程

工程pom依赖如下:

[java]viewplaincopy<?

xmlversion="1.0"encoding="UTF-8"?

><projectxmlns="http:

//maven.apache.org/POM/4.0.0"xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xsi:

schemaLocation="http:

//maven.apache.org/POM/4.0.0http:

//maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.chhliu.springboot.starter.helloworld</groupId><artifactId>springboot-starter-helloworld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><!

--以下两个依赖是自动配置的依赖--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>1.4.3.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>1.4.3.RELEASE</version><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>二、创建自动配置类

有对这部分不了解的,可以参考我的另一篇博客springboot中多环境配置支持

[java]viewplaincopypackagecom.chhliu.springboot.starter.helloworld;importorg.springframework.boot.context.properties.ConfigurationProperties;/***描述:

人员信息自动配置属性类*@authorchhliu*创建时间:

2017年2月13日下午9:

05:

34*@version1.2.0*/@ConfigurationProperties(prefix="person.proterties.set")//定义application.properties配置文件中的配置前缀publicclassPersonServiceProperties{//姓名privateStringname;//年龄privateintage;//性别,不配置的时候默认为person.proterties.set=manprivateStringsex="man";//身高privateStringheight;//体重privateStringweight;……省略getter,setter方法……}三、编写服务类

 

[java]viewplaincopypackagecom.chhliu.springboot.starter.helloworld.service;importcom.chhliu.springboot.starter.helloworld.PersonServiceProperties;publicclassPersonService{privatePersonServicePropertiesproperties;publicPersonService(PersonServicePropertiesproperties){this.properties=properties;}publicPersonService(){}publicStringgetPersonName(){returnproperties.getName();}publicintgetPersonAge(){returnproperties.getAge();}publicStringgetPersonSex(){returnproperties.getSex();}}四、自动配置类

 

[java]viewplaincopypackagecom.chhliu.springboot.starter.helloworld.configuration;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.autoconfigure.condition.ConditionalOnClass;importorg.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importcom.chhliu.springboot.starter.helloworld.PersonServiceProperties;importcom.chhliu.springboot.starter.helloworld.service.PersonService;@Configuration//配置注解@EnableConfigurationProperties(PersonServiceProperties.class)//开启指定类的配置@ConditionalOnClass(PersonService.class)//当PersonService这个类在类路径中时,且当前容器中没有这个Bean的情况下,开始自动配置@ConditionalOnProperty(prefix="person.proterties.set",value="enabled",matchIfMissing=true)//指定的属性是否有指定的值publicclassPersonServiceAutoConfiguration{@AutowiredprivatePersonServicePropertiesproperties;@Bean@ConditionalOnMissingBean(PersonService.class)//当容器中没有指定Bean的情况下,自动配置PersonService类publicPersonServicepersonService(){PersonServicepersonService=newPersonService(properties);returnpersonService;}}五、注册配置

1、在src/main/resources新建META-INF文件夹

2、在META-INF文件夹下新建spring.factories文件

注意:

spring.factories并非是必须的,可以在启动类上添加如下注解进行自动配置

[java]viewplaincopy@ImportAutoConfiguration({PersonServiceAutoConfiguration.class})

3、注册配置自动配置类

[java]viewplaincopyorg.springframework.boot.autoconfigure.EnableAutoConfiguration=com.chhliu.springboot.starter.helloworld.configuration.PersonServiceAutoConfiguration

六、将上面构建的starter安装到本地

[java]viewplaincopy安装命令如下:

mvncleaninstall七、新建springboot项目,并将上面的starter作为依赖

 

[html]viewplaincopy<?

xmlversion="1.0"encoding="UTF-8"?

><projectxmlns="http:

//maven.apache.org/POM/4.0.0"xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xsi:

schemaLocation="http:

//maven.apache.org/POM/4.0.0http:

//maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.chhliu.springboot.starter.person</groupId><artifactId>springboot-starter-person</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version><relativePath/><!

--lookupparentfromrepository--></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><!

--引入自己的starter--><groupId>com.chhliu.springboot.starter.helloworld</groupId><artifactId>springboot-starter-helloworld</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>八、添加属性到application.properties配置文件中

 

[java]viewplaincopyperson.proterties.set.name=chhliuperson.proterties.set.age=28person.proterties.set.sex=womanperson.proterties.set.height=160person.proterties.set.weight=50kg九、编写Controller

 

[java]viewplaincopypackagecom.chhliu.springboot.starter.person.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importcom.chhliu.springboot.starter.helloworld.service.PersonService;@RestControllerpublicclassPersonServiceController{@Autowired//自动注入PersonServiceprivatePersonServicepersonService;@GetMapping("get/name")publicStringgetPersonName(){returnpersonService.getPersonName();//调用PersonService服务的方法}@GetMapping("get/sex")publicStringgetPersonSex(){returnpersonService.getPersonSex();}}十、测试

在浏览器中输入http:

//localhost:

8080/get/sex

测试结果如下:

[java]viewplaincopywoman发现我们在使用我们自定义的starter过程中,并没有创建PersonService实例对象,直接通过@Autowired注解就实现了自动注入,而真正的自动注入过程则是在starter中实现的,通过上面的几个步骤,就实现了自己构建自己的starter,在springboot中,除了官方提供的starter之外,第三方也提供了不少的starter,通过这种方式,极大的对springboot进行的扩展

十一、延伸知识

在构建starter的过程中,涉及到一些注解,这里也做一个统一的说明

[java]viewplaincopy@ConditionalOnBean:

当容器中有指定的Bean的条件下@ConditionalOnClass:

当类路径下有指定的类的条件下@ConditionalOnExpression:

基于SpEL表达式作为判断条件@ConditionalOnJava:

基于JVM版本作为判断条件@ConditionalOnJndi:

在JNDI存在的条件下查找指定的位置@ConditionalOnMissingBean:

当容器中没有指定Bean的情况下@ConditionalOnMissingClass:

当类路径下没有指定的类的条件下@ConditionalOnNotWebApplication:

当前项目不是Web项目的条件下@ConditionalOnProperty:

指定的属性是否有指定的值@ConditionalOnResource:

类路径下是否有指定的资源@ConditionalOnSingleCandidate:

当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean@ConditionalOnWebApplication:

当前项目是Web项目的条件下

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1