关键词:
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中
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gong.starter</groupId> <artifactId>gong.spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--引入自动配置模块--> <dependency> <groupId>com.gong.starter</groupId> <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
我们看下gong-spring-boot-starter-autoconfigurer中的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gong.starter</groupId> <artifactId>gong-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gong-spring-boot-starter-autoconfigurer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--引入spring-boot-starter:所有starter基本配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
删除掉创建项目时自动配置的其它东西,只需要一个标红的依赖即可。
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中导入自己定义的场景启动器:
<!--引入自定义的starter--> <dependency> <groupId>com.gong.starter</groupId> <artifactId>gong.spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
然后编写application.properties定义场景启动器的属性:
gong.hello.prefix=GONG
gong.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; @RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("/hello") public String hello(){ return helloService.sayHelloGong("haha"); } }
启动服务器:
完美。
玩转springboot原理篇(自动装配前凑之自定义stater)(代码片段)
的pom文件中可以看出,mybatis-spring-boot-starter包会自动引入mybatis-spring-boot-autoconfigure以及mybatis相关依赖包。SqlSessionFactoryExceptionlogger.debug(encoding=xmlns:xsi=xsi:schemaLocation=<modelVersion><groupI 查看详情
springboot之自定义查询query
下面讲解下SpringBoot之自定义查询Query的实例SpringBoot之自定义查询Query有HQL语句查询(Hibernate),还可以采用sql语句本地查询BookDao类查询接口1packagecom.hik.dao;23importjava.util.List;45importorg.springframework.data.jpa.repository.J 查看详情
springboot系列之自定义枚举类的数据校验注解
SpringBoot系列之自定义枚举类的数据校验注解业务场景:数据校验,需要对枚举类型的数据传参,进行数据校验,不能随便传参。拓展,支持多个参数的枚举数据校验在网上找到很多参考资料,所以本博客基于这些博客进行拓展... 查看详情
springcloud系列之自定义gatewayfilterfactory(代码片段)
...本配置知道自定义GatewayFilterFactory类环境准备:JDK1.8SpringBoot2.2.3SpringCloud(Hoxton.SR7)Maven3.2+开发工具IntelliJIDEAsmartGit新增SpringBootInitializer项目:NewModule->SpringInitializer,选择jdk版本,至少jdk8packaging选择jar,javav... 查看详情
springboot整合springsecurity之自定义退出(代码片段)
一security默认的退出Springsecurity默认实现了logout退出,访问/logout: 实现逻辑:点击“LogOut”退出成功。退出后访问其它url判断是否成功退出。二自定义退出2.1配置文件中配置在WebSecurityConfifig的protectedvoidconfifigure(HttpSe... 查看详情
springboot之自定义场景启动器(代码片段)
1.场景启动器SpringBoot的自动配置原理中不可或缺的就是那些已经定义好的场景启动器,只要导入某个场景启动器我们的应用就拥有了该场景下的一些核心Bean,有利于快速开发,比如引入Web的场景启动器:<dependen... 查看详情
springboot整合springsecurity之自定义认证(代码片段)
一自定义认证页面1.1说明1.如果用户没有自定义登录页面,springsecurity默认会启动自身内部的登录页面,尽管自动生成的登录页面很方便快速启动和运行,但大多数应用程序都希望定义自己的登录页面。1.2自定义登录... 查看详情
springboot学习笔记自定义starter(代码片段)
springBoot学习笔记(四)自定义starter自定义starter自定义zdy-spring-boot-starter编写simpleBeanMyAutoConfiguration注解EnableRegisterServer配置标记类ConfigMarker创建/META-INF/spring.factories结果展示自定义starterstarter机制Sp 查看详情
springboot核心-自定义starter(代码片段)
...1.4.自动配置类1.5.注册配置2.使用自定义的starter2.1.创建好SpringBoot项目2.2.引入我们自定义的starter3.查看引入的具体依赖4.工具类中使用5.启动测试为了加深对SpringBoot中自动装配的理解,我们自定义一个starter 查看详情
springboot编写自定义的starter
在之前的文章中,我们分析过SpringBoot内部的自动化配置原理和自动化配置注解开关原理。我们先简单分析一下mybatisstarter的编写,然后再编写自定义的starter。mybatis中的autoconfigure模块中使用了一个叫做MybatisAutoConfiguration的自动化... 查看详情
springboot编写自定义starter
根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库1.建立一个empty工程,建立一个普通maven模块xxxStarter,建立一个SpringBoot模块xxxAutoConfigurer,前者... 查看详情
springboot学习-自定义starter
自己开发一个springbootstarter的步骤1.新建一个项目(全部都基于maven),比如新建一个spring-boot-starter-redis的maven项目pom.xml:1<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 查看详情
自定义springboot-starter实现日志打印
1.starter命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-logging,spring-boot-starter-web, 如果是第三方的sta 查看详情
springboot-自定义starter
步骤创建xxxAutoConfiguration类添加@Configuration注解修饰添加@Conditionalxxx注解做注入条件判断添加@AutoConfigureOrder注解注入顺序添加@AutoConfigureAfter注解注入后操作创建xxxProperties配置类添加@ConfigurationProperties注解映射相关配置xxxAutoConfigu... 查看详情
springboot怎么自定义一个starter
SpringBoot怎么自定义一个Starterstarter是什么spring-boot-starter是spring-boot的一个非常重要组成部分。spring-boot-starter可以理解为一个可拔插式的组件。它可以让模块开发独立化,相互间依赖更加松散,也可以更加方便地集成。 &n... 查看详情
springboot——springboot四大核心之起步依赖(自定义starter)(代码片段)
...目录:1.开始2.聊聊起步依赖3.自定义starter3.1新建一个SpringBoot普通项目3.2在这个项目的pom文件添加依赖3.3自定义一个XXXProperties属性配置类3.4自定义一个Service3.5自定义一个XXXAutoConfig自动配置类3.6重点:spring.factories配置文... 查看详情
springboot入门教程(三十一):自定义starter
...比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。在springboot中,使用的最多的就是starter。starter可以理解为一个可拔插式的插件,例如,你想使用jdbc插件,那么可以使用spring-boot-starter-jdbc;如果想使用mongodb,可以使用spring-... 查看详情
springboot自定义starter
1,创建一个空工程2,new一个Modules ----------------maven(启动器):springboottest-spring-boot-starter3,new一个Modules ----------------spring(做自动配置的):springboottest-spring-boot-starter-autoconfigurer4,启动器pom文件 查看详情