网站建设 部署与发布视频教程,如今做知乎类网站怎么样,曲阜做网站,鸿鹄网站建设starter1. target2. 手写启动器~2.1 自动装配#xff0c;自定义属性2.2 启动器#xff0c;引用自动装配模块3. 在自己的项目引用上面的starter1. target 1. 启动器只用来做依赖导入(导入配置模块)2. 专门来写一个自动配置模块3. 启动器依赖自动配置#xff1b;别人只需要引入…
starter1. target2. 手写启动器~2.1 自动装配自定义属性2.2 启动器引用自动装配模块3. 在自己的项目引用上面的starter1. target 1. 启动器只用来做依赖导入(导入配置模块)2. 专门来写一个自动配置模块3. 启动器依赖自动配置别人只需要引入启动器starterxxx-spring-boot-starter; 自定义启动器名分析
一个启动器需要两个模块一个是自动装配模块自动装配属性定义好装配规则。
第二个是启动器引用自动配置模块。
将两个项目打包到maven仓库或者私服仓库
自己的其它项目引用启动器就行了
在yaml或者properties中配置好启动器的属性就行了。需要的注解 Configuration 指定这是一个配置类ConditionalOnXXX 指定条件成立下自动配置类生效AutoConfigureAfter 指定自动配置的顺序Bean 给容器中添加组件ConfigurationProperites 结合xxxProperties 类绑定相关的配置EnableConfigurationProperties // 让xxxProperties生效加入容器中自动配置类如何能加载将需要启动就加载的自动配置类配置在META-INF/spring.properteis 中2. 手写启动器~
2.1 自动装配自定义属性
自动配置pom只留下下面的依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/versionrelativePath/ /parent!-- 自动配置模块... --groupIdcn.bitqian/groupIdartifactIdbitqian-spring-boot-starter-autoconfigurer/artifactIdversion0.0.1-SNAPSHOT/versionnamebitqian-spring-boot-starter-autoconfigurer/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency/dependencies/project
启动器的属性boot中常见的xxxProperties类
package cn.bitqian.starter;import org.springframework.boot.context.properties.ConfigurationProperties;ConfigurationProperties(prefix bitqian.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;}
}
启动器功能方法
package cn.bitqian.starter;/*** author echo lovely* date 2020/11/8 11:39*/
public class HelloService {private HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties helloProperties;}/*** 启动器的方法配置前后缀实现sayHello* param name parameter* return string*/public String sayHello(String name) {return helloProperties.getPrefix() name helloProperties.getSuffix();}}
自动装配类
package cn.bitqian.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 {Autowiredprivate HelloProperties helloProperties;Beanpublic HelloService helloService() {HelloService helloService new HelloService();helloService.setHelloProperties(helloProperties);return helloService;}}
创建META-INF文件夹配置自动装配类在spring.factories里面
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration\
cn.bitqian.starter.HelloServiceAutoConfiguration
双击安装到maven仓库
2.2 启动器引用自动装配模块
启动器模块依赖自动配置模块
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- 启动器 --groupIdcn.bitqian/groupIdartifactIdbitqian-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version!-- bitqian springboot 启动器依赖导入 --dependenciesdependencygroupIdcn.bitqian/groupIdartifactIdbitqian-spring-boot-starter-autoconfigurer/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependencies/project
安装到maven仓库
3. 在自己的项目引用上面的starter
创建springboot项目引入依赖 编写controller
package cn.bitqian;import cn.bitqian.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class DemoController {AutowiredHelloService helloService;RequestMapping(/hello)public String hello() {return hello, helloService.sayHello( rye );}}
自动装配, 嘤嘤嘤
bitqian.hello.prefixi like
bitqian.hello.suffixso much
so- - -