如何通过 spring.factories 将 SpringBootApp 迁移到库?

我们已经构建了一个 Spring 服务发现解决方案,该解决方案@Component通过@Service在根包级别通过@SpringBootApplication. 该项目遵循标准的 maven/spring 布局:


project

  src/main/java

    service

      - ServiceA.java

      - ServiceB.java

      ...

    model

      - modelA.java

      ...

    component

      - ComponentA.java

      - ComponentB.java


    BootApp.java

    pom.xml

我们现在想将它作为一个库在内部分发,这样做需要一个spring.factories文件(请参阅marathon 是如何做到的/请参阅spring autoconfig docs)。查看其他服务发现实现,例如marathonand eureka,看来我应该删除所有自动装配注释(即@Componentand @Service),而是手动将所有组件/bean 配置为一个或多个@Configuration类。该应用程序已经自动装配和引导超过十个类 - 我宁愿不必重构和手动连接它们。


我的问题:有什么方法可以简单地保留现有的自动装配/项目结构并spring.factories在类似于组件扫描的情况下将其拾取?就像是:


org.springframework.boot.autoconfigure.EnableAutoConfiguration=project.BootApp

或者


org.springframework.boot.autoconfigure.EnableAutoConfiguration=project.*

spring.factories关于如何在没有大量@Configuration课程的情况下支持重构,还有其他提示或最佳实践吗?是否可以分开差异并让我们的大部分内部对象自动装配,然后手动配置公共对象,如DiscoveryClientand ServiceRegistery(参见spring cloud SPI)并在中引用它们spring.factories?


鸿蒙传说
浏览 104回答 1
1回答

眼眸繁星

事实证明,可以以@ComponentScan非常相似的方式使用@SpringBootApplication@Configuration@ComponentScan("com.bnymellon.tsg.discovery.springcloud.autoconfig")public class BootApp {}关键区别在于@SpringBotApplicationcontains @EnableAutoConfiguration,它在这里表现不佳。PS - 我没有继承,spring-starter-parent所以spring.factories没有包含在构建的工件中。必须添加以下内容pom.xml<build>&nbsp; &nbsp; <resources>&nbsp; &nbsp; &nbsp; &nbsp; <resource>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <directory>src/main/resources</directory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <includes>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <include>**/*.factories</include>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </includes>&nbsp; &nbsp; &nbsp; &nbsp; </resource>&nbsp; &nbsp; </resources></build>我想在一天结束时,通过注释自动装配(例如@Service)是一种有效的方法,我在这个问题中认为它与 spring.factories 不兼容是不正确的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java