簡體   English   中英

在 Spring Boot 應用程序中使用 application.properties 進行組件掃描

[英]Component Scan using application.properties in spring boot app

我制作了一個包含組件和配置類的 java 庫。

當我在其他 spring boot 服務中使用該庫時,bean 沒有注冊,因為組件和配置類不在類路徑中。

我知道我們可以使用@ComponentScan,但我不想更改服務的代碼。 有沒有辦法使用 application.properties 將類添加到類路徑中? 或者我可以在庫中做些什么來注冊 bean?

您可以使用AutowireCapableBeanFactoryautowireBean()方法來做到這一點。 在這里閱讀更多: AutowireCapableBeanFactory

如果您使用的是 Spring Boot,則可以利用 Spring Autoconfiguration。

為此,您需要在庫的 .jar 文件中的META-INF/spring.factories中放置一個文件spring.factories 如果您使用 Gradle 或 Maven 作為構建工具和標准文件夾結構,則文件路徑為src/main/resources/META-INF/spring.factories

這是我寫的圖書館的一個例子:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.requirementsascode.spring.behavior.web.BehaviorConfiguration,\
org.requirementsascode.spring.behavior.web.SerializationConfiguration,\
org.requirementsascode.spring.behavior.web.BehaviorController

如您所見,在第一行 Spring 特定行之后,您列出了所有配置類。

例如,您可以 在此處了解有關自動配置的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM