Questions regarding using `@ComponentScan` to detect Spring managed components
Questions tagged [component-scan]
129 questions
6
votes
2 answers
Add packages to component-scan
I have a project with a Spring container. Let's say this project is called my-common-library and uses the namespace my.common. It scans all components in this namespace, as specified in common-context.xml which looks like following
…

vasily
- 2,850
- 1
- 24
- 40
6
votes
1 answer
Spring: Why is @PreDestroy not called at end of each test class?
I have an integration test class annotated as follows
@WebAppConfiguration
@ContextConfiguration(classes = {AppConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheConsumerTest {
}
Here's my…

Popcorn
- 5,188
- 12
- 54
- 87
5
votes
2 answers
Spring custom @Enable annotation meta-annotated with @ComponentScan
I'm trying to write my own @Enable annotation for Spring framework, which should be used as follows:
package com.example.package.app;
@SpringBootApplication
@com.example.annotations.EnableCustom("com.example.package.custom")
public class…

knittl
- 246,190
- 53
- 318
- 364
5
votes
2 answers
Adding configuration class to SpringBootTest breaks component scan
I am trying to disable real Mongo connection and replace it with Fongo mock in tests.
Here is my test class:
@SpringBootTest
@RunWith(SpringRunner.class)
public class ControllerTest {
@Autowired
private…

Alesto
- 639
- 4
- 13
- 29
4
votes
1 answer
Why shall I not component-scan my auto-configuration?
In Spring Boot's documentation it is clearly stated that auto-configurations must be specified via the spring.factories file:
Auto-configurations must be loaded that way only. Make sure that they are defined in a specific package space and that…

Sebastian Sprenger
- 75
- 7
4
votes
2 answers
Spring boot @ComponentScan vs @Import
I and my friend were discussed about @ComponentScan and @Import. Which one is better?
We have 2 different ideas.
@ComponentScan: Easy to use, import all beans from the component
scan.
@Import: You need to know what component you want to use, no…

Hoan Nguyen
- 41
- 1
- 3
4
votes
2 answers
How do I start a Spring Boot Web Application without using ComponentScan
I am trying to avoid component scanning to reduce start up time in our module tests, and in our web app in general.
When I replace @SpringBootApplication with @SpringBootConfiguration @EnableAutoConfiguration, I get the following error:
Unable to…

neu242
- 15,796
- 20
- 79
- 114
4
votes
1 answer
Scope of and in hierarchical contexts
I have read:
Multiple component-scan
What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
@RequestMapping annotation not working if is in application context instead of dispatcher…

Jim Garrison
- 85,615
- 20
- 155
- 190
4
votes
3 answers
@ComponentScan not working in test with spring-boot-starter-test
I am attempting to test my @Service and @Repository classes in my project with spring-boot-starter-test and @Autowired is not working for the classes I'm testing.
Unit test:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes…

Geyser14
- 1,385
- 3
- 14
- 32
3
votes
1 answer
Spring Filter components in auto scanning
I have a Spring MVC app. ( an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the…

David Àlvarez Tudela
- 401
- 1
- 3
- 10
3
votes
1 answer
How is @ConfigurationProperties-annotated classes detected automatically with @SpringBootApplication Annotation
I am learning Spring Boot and have a question with one example in the reference documentation.
Following section of the documentation mentions
6. Using the @SpringBootApplication Annotation
A single @SpringBootApplication annotation can be used to…

R.G
- 6,436
- 3
- 19
- 28
3
votes
1 answer
@ComponentScan from 2 packages
I have 2 projects, the main spring boot project and the second spring project, which contains @Contollers, @Services and so on.
I try to use some of this services in main project. I saw similar questions like here, here, here and many others, but it…

Slava
- 133
- 2
- 8
3
votes
1 answer
Spring @ComponentScan annotation doesn't work
I have spring boot application sample with package layout:
main:
-com.foo
Application.java
-com.foo.services
ItemService.java
ItemRepository.java
-com.foo.config
Configuration.java
test:
-com.foo.services
…

magulla
- 499
- 1
- 9
- 22
3
votes
1 answer
Spring Boot: NoUniqueBeanDefinitionException between test and main
I have a SpringBoot main/Application.java class
@SpringBootApplication
@ComponentScan(value = "com.nfl.dm.shield", excludeFilters =
{
@ComponentScan.Filter(value = MemoryRepository.class, type = FilterType.ASSIGNABLE_TYPE)
…

nflearl
- 131
- 7
3
votes
2 answers
How to configure controller in spring without component scanning in xml?
I have to design a very large scale project for a bank using spring mvc. I already choose to go with the XML configuration. My concern is to limit the start up time of the server. There will be approximately 2000 controllers.
I already use component…

Hasib Tarafder
- 5,773
- 3
- 30
- 44