如何限制Spring按包名筛选注入List中的Bean?
Spring按指定子包过滤注入List的实现方案
我在Spring中注入了List<ImplementMe>,对应的Bean还包含其他参数。所有ImplementMe的实现类原本处于同一包中,但该List所在的包可能不同,同时也存在不同包下的ImplementMe实现类。想了解是否能在@Bean方法中强制Spring仅注入指定子包下的Bean?我知道可以设置注入顺序,但未找到相关过滤方法,是否可以借助@Conditional注解来实现该需求?
简化后的现有实现
package com.example.demo import org.springframework.boot.CommandLineRunner import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.stereotype.Component interface ImplementMe @Component class One : ImplementMe @Component class Two : ImplementMe @Component class Three : ImplementMe class InjectInMe (val listOfImplementation : List<ImplementMe>) @Configuration class ConfigExample { @Bean fun example(listOfImplementation: List<ImplementMe> ) : InjectInMe { println(listOfImplementation) return InjectInMe(listOfImplementation) } } @SpringBootApplication class DemoApplication(private val applicationContext: ApplicationContext) { @Bean fun run() = CommandLineRunner { println(applicationContext.getBean(InjectInMe::class.java)) } } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }
使用ObjectProvider实现注入后过滤的示例
package com.example.demo import org.springframework.beans.factory.ObjectProvider import org.springframework.beans.factory.annotation.Qualifier import org.springframework.boot.CommandLineRunner import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.context.ApplicationContext import org.springframework.context.annotation.* import org.springframework.stereotype.Component interface ImplementMe @Component @Qualifier("pkg1") class One : ImplementMe @Component @Qualifier("pkg2") class Two : ImplementMe @Component @Qualifier("pkg2") class Three : ImplementMe class InjectInMe (val listOfImplementation : List<ImplementMe>) @Configuration class ConfigExample { @Bean @Qualifier("pkg2") fun example1(listOfImplementation: List<ImplementMe> ) : InjectInMe { return InjectInMe(listOfImplementation) } @Bean fun example2(listOfImplementation: ObjectProvider<ImplementMe> ) : InjectInMe { return InjectInMe(listOfImplementation.filter { // 此处模拟按包过滤 it.javaClass.name.startsWith("com.example.demo.T") }) } } @SpringBootApplication class DemoApplication(private val applicationContext: ApplicationContext) { @Bean fun run() = CommandLineRunner { with(applicationContext.getBean("example1") as InjectInMe) { println(listOfImplementation) } println() with(applicationContext.getBean("example2") as InjectInMe) { println(listOfImplementation) } } } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }
输出结果
[com.example.demo.One@63b3ee82, com.example.demo.Three@2620e717, com.example.demo.Two@7c8d5312] [com.example.demo.Three@2620e717, com.example.demo.Two@7c8d5312]
内容的提问来源于stack exchange,提问作者Romano Silva
相关产品推荐
相关产品推荐

