浅谈SpringBoot如何自定义Starters

网友投稿 536 2023-01-17

浅谈SpringBoot如何自定义Starters

浅谈SpringBoot如何自定义Starters

一、Starters原理

1.1 Starters场景启动器

1、场景需要用到的依赖是什么?

比如依赖的jar

2、如何编写自动配置

以WebMvcAutoConfiguration自动配置为例:

@Configuration

@ConditionalOnWebApplication

@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,

WebMvcConfigurerAdapter.class })

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)

@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,

ValidationAutoConfiguration.class })

public class WebMvcAutoConfiguration {

public static final String DEFAULT_PREFIX = "";

public static final String DEFAULT_SUFFIX = "";

@Configuration指定这是一个配置类

@ConditionalOnXXX 在指定条件成立的情况下自动配置类生效

自动装配顺序

在特定自动装配Class之前 @AutoConfigureBefore

在特定自动装配Class之后@AutoConfigureAfter

指定顺序@AutoConfigureOrder

@Bean 给容器中添加组件

@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置

@ConfigurationProperties(prefix = "spring.mvc")

public class WebMvcProperties {

}

@EnableConfigurationProperties 让xxxProperties生效加入到容器中

@Configuration

@Import(EnableWebMvcConfiguration.class)

@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })

public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {

}

配置自动装配Bean:

自动配置类要能加载

将需要启动就加载的自动配置类,将标注@Configuration的自动配置类配置在META‐INF/spring.factories下,自动配置类就会生效

# Auto Configure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\

org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3、模式

启动器(starter)

启动器只用来做依赖导入

专门写一个自动配置模块

启动器依赖自动配置,别人只需要引入启动器(starters)

mybatis-spring-boot-starter 自定义启动器名 -spring-boot-starter

二、自定义Starters

构建项目:

1.先创建一个空工程

2、创建两个模块分别是启动器starter的maven模块和spring的初始化器创建的自动配置模块

启动器maven模块

自定义的starters

spring的初始化器创建模块(创建自动配置相关的模块)

三、代码步骤

在启动器starter的pom文件中引入配置类的坐标ming-spring-boot-starter-autoconfigurer

xmlns:xsi="http://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">

4.0.0

com.ming.springboot

ming-spring-boot-starter

1.0-SNAPSHOT

com.ming.springboot

ming-spring-boot-starter-autoconfigurer

0.0.1-SNAPSHOT

xmlns:xsi="http://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">

4.0.0

com.ming.springboot

ming-spring-boot-starter

1.0-SNAPSHOT

com.ming.springboot

ming-spring-boot-starter-autoconfigurer

0.0.1-SNAPSHOT

写一个打招呼的功能

package com.ming.springboot;

/**

* 打招呼的

*

*/

public class HelloService {

HelloProperties helloProperties;

public HelloProperties getHelloProperties() {

return helloProperties;

}

public void setHelloProperties(HelloProperties helloProperties) {

this.helloProperties = helloProperties;

}

public String sayHello(String name){

return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();

}

}

HelloProperties 和Helloservice 进行属性绑定的

package com.ming.springboot;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "com.ming")

public class HelloProperties {

private String prefix;

private String suffix;

public String getPrefix() {

return prefix;

}

public void setPrefix(String prefix) {

thishttp://.prefix = prefix;

}

public String getSuffix() {

return suffix;

}

public void setSuffix(String suffix) {

this.suffix = suffix;

}

}

自动配置类

package com.ming.springboot;

ixhePSQjmport 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 helloService = new HelloService();

helloService.setHelloProperties(helloProperties);

return helloService;

}

}

然后将这两个模块安装到maven仓库中

先安装配置模块因为starter模块依赖配置模块,别人调用我们的starter模块就行了

然后将启动器starter也装到仓库中,别人就可以用坐标引入了

在别的项目中引入自定义的启动器starter

com.ming.springboot

ming-spring-boot-starter

1.0-SNAPSHOT

配置application.properties

#自定义启动器starter

com.ming.prefix=一起学习

com.ming.suffix=你学费了吗

测试

@Autowired

HelloService helloService;

@Test

public void starterTest(){

String sayHello = helloService.sayHello("自定义starter");

System.out.println(sayHello);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:移动应用开发总分多少(移动应用开发就业岗位)
下一篇:企业app开发 海外(国外开发平台)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~