我在 Spring 中使用 Profiles 時遇到問題,基本上,我們使用它們來存根我們微服務的某些部分,該部分使用到 DB 的連接和到另一個 Web 服務的連接。
以前,我為 DB 和外部 webservice 使用了一個存根組態檔:
@Configuration
@EnableAutoConfiguration
@Profile("stub")
@ComponentScan(
basePackages = {"com.consumption.backend"},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.consumption.backend.*.persistence.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.consumption.backend.databackendapi.*")
})
public class StubConfiguration {
@Bean
public DataApi consumptionApi() { return new DataStubApi(); }
@Bean
public RefDayDao refDayDao() { return new RefDayInMemoryDao(); }
@Bean
public RefTypeHourDao refTypeHourDao() { return new RefTypeHourInMemoryDao(); }
}
這作業正常,但是我想將此存根分成兩個存根,一個用于資料庫,另一個用于外部網路服務,以確保我們的測驗具有更大的靈活性。
DAO 的存根:
@Configuration
@EnableAutoConfiguration
@Profile("stubconfv3")
@ComponentScan(
basePackages = {"com.consumption.backend"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.consumption.backend.*.persistence.*")
)
public class StubConfV3Configuration {
@Bean
public RefDayDao refDayDao() { return new RefDayInMemoryDao(); }
@Bean
public RefTypeHourDao refTypeHourDao() { return new RefTypeHourInMemoryDao(); }
}
外部網路服務的存根:
@Configuration
@EnableAutoConfiguration
@Profile("stubdatabackend")
@ComponentScan(
basePackages = {"com.consumption.backend"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.consumption.backend.databackendapi.*")
)
public class StubDataBackendConfiguration {
@Bean
public DataApi consumptionApi() { return new DataStubApi(); }
}
DAO 的存根似乎作業正常,但是我似乎對外部 web 服務 API 有問題,沒有正確排除實作:
- 如果我使用
stub組態檔啟動我的應用程式,一切正常 - 如果我啟動我的應用程式
stubconfv3并stubdatabackend遇到注入問題,因為找到了兩個類:
Parameter 0 of constructor in com.consumption.backend.service.DataService required a single bean, but 2 were found:
dataBackendApi: defined in file [C:\code\consumption-backend\databackend-api\target\classes\com\consumption\backend\databackendapi\DataBackendApi.class]
consumptionApi: defined by method 'consumptionApi' in class path resource [com/consumption/backend/application/configuration/StubDataBackendConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Either the exclusion is not working or there is some tricky thing as it seems to find the .class file in target instead of finding it at runtime in a class loader
Most likely, I do some stupid mistake and am not seeing it ...
編輯:我注意到如果我在中輸入stubconfv3profile 檔案StubConfV3Configuration,則不會再出現注入問題,所以我想你不能@ComponentScan在兩個不同的類中組合excludeFilters ......第一個類將對除她排除的之外的所有內容進行組件掃描,第二個也會做同樣的事情,所以第二個類排除的包仍然會被第一類掃描。
uj5u.com熱心網友回復:
您可以嘗試使用注釋您的 DataBackendApi
@Profile("!stubdatabackend")
在不使用該組態檔時將其包含在內,否則將其排除
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345455.html
上一篇:從服務類呼叫方法時,SpringBoot@AutowiredNullPointerException
下一篇:將SpringBoot依賴項從1.5.3.RELEASE更新到2.2.7.RELEASE會破壞SpringApp
