Annotation-based configuration and component scanning Flashcards

(35 cards)

1
Q

What does @ComponentScan do?

A

It scans packages for classes annotated with stereotype annotations and registers them as Spring beans

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

By default, which package does @ComponentScan scan?

A

The package of the class where @ComponentScan is declared and all its subpackages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is @SpringBootApplication usually enough for scanning?

A

It includes:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you exclude components from scanning?

A

@ComponentScan(
excludeFilters = @Filter(
type = FilterType.REGEX,
pattern = “*.Repository”
)
)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if a class is excluded from component scanning?

A

Spring will not register it as a bean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of @Component?

A

Generic stereotype for Spring-managed components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between @Component, @Service, and @Controller?

A

Functionally identical for bean registration, but semantically indicate architectural roles

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What special behavior does @Repository add?

A

Automatic exception translation into Spring’s DataAccessException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When should you prefer stereotype annotations over @Bean methods?

A

When you control the class and want simple, concise configuration close to the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a key architecture difference between stereotype annotations and @Bean methods?

A
  • Stereotype:
    • configuration near class
    • class depends on Spring
    • Simple setup
  • @Bean:
    • configuration separate
    • class be Spring-agnostic
    • More flexible initialization
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if you define a bean with @Bean and also define the same bean via @ComponentScan?

A

@Bean takes precedence - it overrides the scanned bean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is a bean named when created via component scanning?

A

Default name = class name with first letter lowercase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is a bean named when created via @Bean?

A

Default name = method name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if the scanned bean and the @Bean method have different names?

A

They are considered 2 separate beans -> potential ambiguity error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does Spring resolve parameters of a @Bean method?
“””
@Bean
public OrderService orderService( OrderMapper mapper, OrderRepository repo)
“””

A

Spring resolves them by type from the application context

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens if multiple beans of the same type exist when resolving a @Bean method parameter?

A

Spring throws NoUniqueBeanDefinitionException unless:
- One is @Primary
- A @Qualifier is used

17
Q

Why does calling another @Bean method inside a configuration class not create multiple instances?
“””
@Bean
public OrderService orderService() {
return new OrderService(orderRepository());
}
“””

A

Because @Configuration classes are proxied using CGLIB, the proxy intercepts method calls and returns the existing singleton from the context

18
Q

What would happen if the class were not annotated with @Configuration?

A

Each method call would create a new instance - no proxy interceptionW

19
Q

When is @Autowired not required on a constructor?

A

When the class has exactly one constructor (Spring automatically uses it)

20
Q

When must you use @Autowired on a constructor?

A

When there are multiple constructors and you must specify which one to use?

21
Q

Can @Autowired be used on fields?

A

Yes, but constructor injection is recommended

22
Q

Why is constructor injection preferred over field injection?

A
  • Immutability
  • Easier testing
  • Clear required dependencies
  • no reflection hacks
23
Q

What does @Autowired(required = false) do?

A

Spring will inject the dependency if available;
otherwise it leaves it null

24
Q

What is a better alternative to “required = false”?

A

Use Optional<T> in constructor parameters</T>

25
If an interface has 2 implementations, how does Spring resolve injection?
First by type, IF multiple: - @Primary - @Qualifier
26
What does @Primary do?
Marks the default bean for injection when multiple candidates exist
27
What does @Fallback do?
Used only if no other bean of that type is available
28
What does @Qualifier("beanName") do?
Selects a specific bean by name when multiple exist
29
What is Spring's injection resolution order?
1) Match by type 2) Narrow by qualifier 3) If multiple -> check @Primary 4) If still ambiguous -> Error
30
Why should configuration classes be separated by layer?
- Maintainability - Clear architecture - Modularization - Easier testing
31
What problem occurs if configuration becomes too centralized?
- hard to maintain - poor separation of concerns - reduced modularity
32
Why can using stereotype annotations make your domain classes depend on Spring?
Because you add Spring-specific annotations to the class itself
33
Why is @Bean better for third-party classes?
You cannot modify the source code to add annotations
34
What happens if 2 beans of the same type and name are registered?
Bean definition overriding rules apply, may cause startup failure depending on configuration
35
Why is injection by concrete type sometimes discouraged?
It breaks abstraction and reduces flexibility - prefer interface based injection