SpringBoot(三)手写starter pom自动配置
2024-09-01 15:10:15
思想:主要是EnableAutoConfiguration在启动的时候会扫描spring.factories并加载
1在resource下面新建META-INF/spring.factories
2在spring.factories中添加自动装载的类
3其他项目引用既OK
1.新建一个starter的Maven项目A,pom文件修改
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.start</groupId>
<artifactId>spring-boot-starter-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--支持读取配置文件属性-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--需要改jar 才会自动加载enableautoconfiguration--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.新建resource文件,META-INF/spring.factories文件
多个可这样写:(\表示换行可读取到属性)(,多个属性分割)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.start.demo.service.MyHelloConfiguration,\
com.example.start.demo.service.MyHelloConfiguration
3.新建MyHelloConfiguration配置类
@Configuration
public class MyHelloConfiguration { //当不存在HelloService时加载
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService test() { HelloService helloService = new HelloService();
helloService.setMsg("world");
return helloService;
}
}
public class HelloService { private String msg; public void setMsg(String msg) {
this
.msg = msg;
} public String sayHello() {
return "hello" + msg;
}
}
4.在IDEA中Maven下执行install将jar包发布到本地仓库
5在项目B中引入项目A新建的spring-boot-starter-demo.jar
<dependency>
<groupId>com.example.start</groupId>
<artifactId>spring-boot-starter-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
6项目B引用测试结果:
项目B调用项目A的service
启动项目B设置application.properties文件debug=true,启动输出结果:
测试运行结果:
最新文章
- SLES 11 SP3防火墙设置
- Office文件的Open Xml 格式
- Txx考试(codevs 2894)
- EasyMock(官方资料整理)
- LeetCode OJ 222. Count Complete Tree Nodes
- SQL Server 2008登录问题(错误 233和18456)解决方法
- 『自我の感悟』alt or title?
- Powershell批量安装SNMP服务
- 认证鉴权与API权限控制在微服务架构中的设计与实现(四)
- iframe引入网页
- linux shell 脚本攻略学习3
- PCL滤波介绍(2)
- HDOJ1004
- css 实现圆形头像
- Git合并分支或者冲突
- JavaConfig 使用Java代码进行显示配置
- 68. Text Justification一行单词 两端对齐
- Django的模板与母版
- 模拟赛 yjqa
- 一起來玩鳥 Starling Framework(6)Juggler、Tween、以及DelayCall
热门文章
- TensorFlow(七):tensorboard网络执行
- @babel/preset-env useBuiltIns 说明
- Python测试框架对比
- Tkinter 之主窗口参数
- 问题分析——Maven打包后发版,静态资源找不到
- File contains parsing errors: file:///etc/yum.repos.d/docker-ce.repo	[line 84]: docker-ce-nightly-source]
- 关于tomcat-users.xml添加用户被重复加载的问题mark
- struts2之Action与JSP相互数据传递
- js的Map实例
- python 对xls写入信息