Rather than making some of my classes @Autowire
many small @Component
s, I wanted to create a single @Component
that would collect and just forward to the smaller @Component
s.
Coding itself isn't too difficult, but when it comes to testing, it becomes cumbersome because now I have to add it to the @ContextConfiguration(classes)
. Is there a way of doing it so I don't have to manage it individually?
Using @ComponentScan
adds the whole package which I may not want because it will trigger more @MockBean
to be created.
In my opinion what you are looking for is a combination of @Configuration + @Bean
. Instead of marking all your nested subcomponents with @Component you can initialize the complete chain via @Bean
and work with simple pojo-s instead of spring managed beans. At the end of course the end result will be a spring managed bean, but the initialization process, you can initialize it from anywhere with spring , or without spring.
With regards of ComponetScan. Usualy if I want to load only a certain section I create annotation with explicit patter of the conponenbt scan I want and then I use it accross the domain I want.
Example, lets say we have two sets of Mapper classes one is regarding Order the other is regarding inventory. I can create annotation @EnableOrderMappers
and annotation @EnableInventoryMappers
with appropriate component scann annotation on them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.