亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從彈簧啟動應用程序中排除@Configuration

從彈簧啟動應用程序中排除@Configuration

達令說 2022-09-07 18:02:28
我有一堆模塊(比如3)。兩個是基于彈簧啟動的模塊,另一個是基于彈簧的模塊。說模塊 1 - 彈簧靴模塊 2 - 彈簧啟動模塊 3 - 通用模塊僅基于彈簧模塊 3 @Configuration定義的文件,該文件僅需要由模塊 2 而不是 1 選取。我嘗試了很多東西來排除配置文件。例如:-@SpringBootApplication@ComponentScan(basePackages = {"com.adobe"}, excludeFilters = {    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})public class Application {  private static final Logger logger = LoggerFactory.getLogger(Application.class);  public static void main(String[] args) throws Exception {    SpringApplication.run(Application.class, args);  }}但是@Configiration類仍然沒有被排除在外,Spring正試圖在我不想要的應用程序上下文中加載它。我的配置類@Configurationpublic class WorkerConfig {  @Bean  public WorkerExecutors workerExec() {    WorkerExecutors executors = new WorkerExecutors();    return executors;  }}我也確實在注釋@ComponentScan讀到 * <p>Note that the {@code <context:component-scan>} element has an * {@code annotation-config} attribute; however, this annotation does not. This is because * in almost all cases when using {@code @ComponentScan}, default annotation config * processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore, * when using {@link AnnotationConfigApplicationContext}, annotation config processors are * always registered, meaning that any attempt to disable them at the * {@code @ComponentScan} level would be ignored.所以看起來排除在組件掃描不起作用。除此之外,我還嘗試排除使用@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})public class Application {但彈簧靴投擲java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:    - com.adobe.repository.worker.lib.config.WorkerConfig    - com.adobe.acp.repository.worker.lib.core.WorkerExecutors    - com.adobe.acp.repository.worker.lib.core.Worker任何想法,我如何禁用組件掃描彈簧啟動模塊中的非彈簧啟動模塊,而不是放入不同的包。我不想放入不同的包裝。任何幫助是值得贊賞的!
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

你可以試試這個,它對我有用:


@SpringBootApplication

@ComponentScan(excludeFilters  = {@ComponentScan.Filter(

              type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})



查看完整回答
反對 回復 2022-09-07
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

Auto-config packages 位于 org.springframework.boot.autoconfigure 下。這就是你不能這樣做的原因:


@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})

春天做你吩咐做的事。您正在致電:


@ComponentScan(basePackages = {"com.adobe"}

, excludeFilters = {

    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})

因此,Spring 不會加載任何這些 Worker 類。這就是為什么Spring不會“執行”用@Configuration注釋的類。


也就是說,你試圖做的事情對我來說沒有意義。聽起來你有“模塊”(java類),但它們都是同一個彈簧上下文的一部分。如果你有一個Spring Context,那么你可以告訴Spring加載一些@Configuration類,而不是其他一些。然后,從您的“模塊”中,您可以注入所需的任何內容。模塊 1 將從模塊 3 中注入 Bean,但模塊 2 不會。就是這么簡單。


如果由于某種原因,您確實需要阻止模塊 2 從模塊 3 訪問 Bean,但仍然保持模塊 3 從模塊 1 中可見,那么我會將模塊 1 和模塊 2 分隔在兩個不同的 Spring Boot 應用程序中,模塊 3 成為通用代碼。但這種方法可能會破壞您當前的架構。


更新 周五 3月 29 2019


試試這個:


@SpringBootApplication

@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })

它對我有用。我有MyClass和MyClass2,MyClass被加載,MyClass2沒有。我嘗試使用Spring Boot 1.5.9.RELEASE和Spring Bom來獲取所有依賴項。


查看完整回答
反對 回復 2022-09-07
  • 2 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號