linux怎么查看本机内存大小
306
2022-12-12
springboot自定义starter启动器的具体使用实践
目录第一步、创建 xxx-spring-boot-starter 的spring Initializr模块第二步、删除不需要的内容(启动类、除下面spring-boot-starter的其它依赖,maven编译插件)第三步、写代码,对外提供一些自己写的类第四步、在resources资源文件夹下创建一个META-INF文件夹,并创建一个spring.factories文件第五步、将该项目发布的maven仓库,或者安装到本地仓库中让其它项目能使用的到第六步、测试自己定义的启动器使用有效
第一步、创建 xxx-spring-boot-starter 的spring Initializr模块
第二步、删除不需要的内容(启动类、除下面spring-boot-starter的其它依赖,maven编译插件)
如下是完整的pom.xml
实际上如果当前starter需要引用其它依赖加入到dependences里面即可,这里只做演示项目
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
项目结构截图
第三步、写代码,对外提供一些自己写的类
创建HelloProperties.java,直接复制下面代码,然后选择包进行粘贴,Idea会自动创建对应类代码设置好包名
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello") // 对外提供的前缀,相当于其它引入当前starter在properties文件使用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;
}
}
创建HelloService.java直接复制下面代码,选择包进行粘贴即可生成
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();
}
}
创建配置类(和前面一样复制粘贴即可)HelloServiceAutoConfiguration.java,将HelloService注入到IOC容器中
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环境下才生效,相关的其它条件可以使用@ConditionXXX
@EnableConfigurationProperties(HelloProperties.class) // 表示HelloProperties作为配置类使用
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties; // 作为配置类目的就是想在sayHello方法返会的字符串加上前缀和后缀
@Bean
public HelloService helloService() { // 将HelloService注入到IOC容器
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
第四步、在resources资源文件夹下创建一个META-INF文件夹,并创建一个spring.factories文件
如下面截图
内容则是将@Configuration配置类加入,目的是将配置加入到外部的IOC容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
idea中右键copy–》copy reference,将复制的值填入上面=右边
注意:如果有多个AutoConfiguration则用逗号分开,还有回车小心前面的空格,最好没有其它字符。
例如:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\
top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration
第五步、将该项目发布的maven仓库,或者安装到本地仓库中让其它项目能使用的到
本地安装为例:
成功后即可
第六步、测试自己定义的启动器使用有效
创建一个springboot项目
勾选web模块即可,然后加入自定义启动器的gav依赖
在启动类中加入内部类(这里为http://了方便演示不按照规范创建包)
如下示例代码
启动类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.huashengshu.myspringboothupiWXstarter.HelloService;
@SpringBootApplication
public class DemoApplication {
@RestController
public class HelloController {
@Autowired
HelloService helloService; // 注入HelloService
@GetMapping("/hellohttp://") // 暴露一个/hello 请求路径对外提供服务
public String hello(){
return helloService.sayHello("zhang san"); // 返回带有前缀和后缀中间是 "zhang san"的字符串
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
properties文件
因为使用了@ConfigurationProperties(prefix = "hello")注解所以在当前项目的properties文件中使用hello前缀调用即可对成员属性赋值
如下
hello.prefix=HUASHENGSHU
hello.suffix=Hello World
运行当前项目,访问/hello验证是否有效
如下:
说明自定义starter成功。
其它业务代码,根据自己的需求自己加入依赖,也就是说可以自己定义starter提供给其它人用!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~