思想:主要是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,启动输出结果:

测试运行结果:

最新文章

  1. SLES 11 SP3防火墙设置
  2. Office文件的Open Xml 格式
  3. Txx考试(codevs 2894)
  4. EasyMock(官方资料整理)
  5. LeetCode OJ 222. Count Complete Tree Nodes
  6. SQL Server 2008登录问题(错误 233和18456)解决方法
  7. 『自我の感悟』alt or title?
  8. Powershell批量安装SNMP服务
  9. 认证鉴权与API权限控制在微服务架构中的设计与实现(四)
  10. iframe引入网页
  11. linux shell 脚本攻略学习3
  12. PCL滤波介绍(2)
  13. HDOJ1004
  14. css 实现圆形头像
  15. Git合并分支或者冲突
  16. JavaConfig 使用Java代码进行显示配置
  17. 68. Text Justification一行单词 两端对齐
  18. Django的模板与母版
  19. 模拟赛 yjqa
  20. 一起來玩鳥 Starling Framework(6)Juggler、Tween、以及DelayCall

热门文章

  1. TensorFlow(七):tensorboard网络执行
  2. @babel/preset-env useBuiltIns 说明
  3. Python测试框架对比
  4. Tkinter 之主窗口参数
  5. 问题分析——Maven打包后发版,静态资源找不到
  6. File contains parsing errors: file:///etc/yum.repos.d/docker-ce.repo [line 84]: docker-ce-nightly-source]
  7. 关于tomcat-users.xml添加用户被重复加载的问题mark
  8. struts2之Action与JSP相互数据传递
  9. js的Map实例
  10. python 对xls写入信息