Week 1 Review — Spring Core Mental Model
Goal
This review checks if I really understand Week 1.
Week 1 topics:
- What problem Spring solves
- IoC
- Dependency Injection
- Beans
ApplicationContextBeanFactoryBeanDefinition@Componentvs@Bean- Stereotype annotations
- Component scanning
- Constructor injection
@Primary@Qualifier- Common bean-not-found errors
1. Week 1 Big Picture
Spring helps manage object creation and dependencies.
Without Spring:
OrderRepository repository = new OrderRepository();
OrderService service = new OrderService(repository);
OrderController controller = new OrderController(service);
With Spring:
@Repository
public class OrderRepository {
}
@Service
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
@RestController
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
}
Spring creates the objects and connects them.
2. Core Memory Sentences
Memorize these:
IoC means Spring controls object creation and dependency management.
Dependency Injection means dependencies are provided from outside.
IoC is the idea. Dependency Injection is the technique.
A Spring bean is an object managed by the Spring IoC container.
ApplicationContext is the main Spring container.
BeanFactory is the basic Spring IoC container.
ApplicationContext extends BeanFactory and adds more features.
BeanDefinition is the recipe. Bean is the actual object.
Component scanning finds annotated classes and registers them as beans.
@SpringBootApplication includes @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@Component is generic.
@Service is for business logic.
@Repository is for persistence and can support exception translation.
@RestController equals @Controller plus @ResponseBody.
@Component is used on classes.
@Bean is used on methods.
Constructor injection is best for required dependencies.
@Autowired is optional on a single constructor.
@Primary means default bean.
@Qualifier means exact bean.
Spring singleton means one bean instance per Spring container.
Objects created with new are not automatically Spring-managed.
3. Concept Map
Spring Framework
│
├── IoC Container
│ ├── BeanFactory
│ └── ApplicationContext
│
├── Beans
│ ├── BeanDefinition
│ ├── Bean lifecycle
│ ├── Bean scope
│ └── Dependency Injection
│
├── Component Scanning
│ ├── @Component
│ ├── @Service
│ ├── @Repository
│ ├── @Controller
│ ├── @RestController
│ └── @Configuration
│
└── Bean Registration
├── Stereotype annotations
└── @Bean methods
4. Most Important Exam Traps
Trap 1 — IoC vs DI
Wrong:
IoC and DI are exactly the same.
Correct:
IoC is the principle. DI is one way to implement IoC.
Trap 2 — new vs Spring Bean
Wrong:
OrderService service = new OrderService();
This is not automatically Spring-managed.
Correct:
OrderService service = context.getBean(OrderService.class);
This comes from Spring and is managed by Spring.
Trap 3 — @Service Alone Is Not Enough
Wrong:
If a class has @Service, it is always a Spring bean.
Correct:
@Service marks the class as a candidate. It becomes a bean only if Spring scans its package.
Trap 4 — Component Scanning Does Not Scan Everything
Spring Boot scans from the package of the main class.
Example:
package com.example.app;
@SpringBootApplication
public class MyApplication {
}
Spring scans:
com.example.app
com.example.app.*
It does not automatically scan:
com.example.service
Trap 5 — @SpringBootApplication
Important:
@SpringBootApplication
includes:
@Configuration
@EnableAutoConfiguration
@ComponentScan
Trap 6 — @Component vs @Bean
@Component:
@Component
public class EmailService {
}
Used on a class.
@Bean:
@Bean
public Clock clock() {
return Clock.systemUTC();
}
Used on a method.
Trap 7 — BeanDefinition vs Bean
BeanDefinition = metadata / recipe
Bean = actual object
Trap 8 — Multiple Beans
If Spring finds two beans for one dependency, it fails unless ambiguity is solved.
Solutions:
@Primary
@Qualifier
List<T>
Map<String, T>
more specific type
Trap 9 — Constructor Injection
In modern Spring:
public OrderService(OrderRepository orderRepository) {
}
does not need @Autowired if it is the only constructor.
Trap 10 — Spring Singleton
Spring singleton means:
one instance per Spring container
It does not mean classic Java Singleton pattern.
Practice Questions and Answers
Question 1
What problem does Spring solve?
Answer:
Spring solves object creation, dependency wiring, configuration, lifecycle management, and infrastructure concerns such as transactions, security, and AOP.
Question 2
What is IoC?
Answer:
IoC means Inversion of Control. It means the control of object creation and dependency management is moved from application code to the Spring container.
Question 3
What is Dependency Injection?
Answer:
Dependency Injection means a class receives its dependencies from outside instead of creating them itself.
Question 4
What is the difference between IoC and Dependency Injection?
Answer:
IoC is the general principle where control is moved to the framework or container. Dependency Injection is a technique where dependencies are provided from outside. In Spring, DI is the main way IoC is implemented.
Question 5
What is a Spring bean?
Answer:
A Spring bean is an object created and managed by the Spring IoC container.
Question 6
What is ApplicationContext?
Answer:
ApplicationContext is the main Spring container. It creates, wires, configures, and manages beans. It also supports events, resources, profiles, environment access, and lifecycle management.
Question 7
What is BeanFactory?
Answer:
BeanFactory is the basic Spring IoC container. It provides basic bean creation, dependency injection, and bean lookup.
Question 8
What is the difference between ApplicationContext and BeanFactory?
Answer:
ApplicationContext extends BeanFactory and adds more features such as event publishing, internationalization, resource loading, environment access, and profile support.
Question 9
What is a BeanDefinition?
Answer:
A BeanDefinition is metadata that tells Spring how to create and manage a bean.
Question 10
What is the difference between BeanDefinition and bean?
Answer:
A BeanDefinition is the recipe or metadata. A bean is the actual object created and managed by Spring.
Question 11
What is component scanning?
Answer:
Component scanning is the process where Spring scans configured packages to find classes annotated with stereotype annotations and registers them as beans.
Question 12
Where does Spring Boot start scanning by default?
Answer:
Spring Boot starts scanning from the package of the class annotated with @SpringBootApplication and its subpackages.
Question 13
Why should the main Spring Boot class be in the root package?
Answer:
The main class should be in the root package so Spring scans all application subpackages automatically.
Question 14
What does @SpringBootApplication include?
Answer:
@SpringBootApplication includes:
@Configuration
@EnableAutoConfiguration
@ComponentScan
Question 15
What is the difference between @Component and @Bean?
Answer:
@Component is used on a class and discovered by component scanning. @Bean is used on a method inside a configuration class, and the return value becomes a bean.
Question 16
What is the difference between @Service and @Repository?
Answer:
@Service marks business logic classes. @Repository marks persistence-layer classes and can support exception translation into Spring’s DataAccessException hierarchy.
Question 17
What does @RestController include?
Answer:
@RestController includes:
@Controller
@ResponseBody
Question 18
Why is constructor injection recommended?
Answer:
Constructor injection is recommended because it makes dependencies explicit, supports final fields and immutability, improves testability, and prevents objects from being created without required dependencies.
Question 19
What happens if multiple beans match one dependency?
Answer:
Spring fails because the dependency is ambiguous. I can solve it with @Primary, @Qualifier, a more specific type, or collection injection.
Question 20
What is the difference between @Primary and @Qualifier?
Answer:
@Primary marks a bean as the default choice. @Qualifier selects a specific bean explicitly. @Qualifier is more specific.
7. Mini Mock Exam — Week 1
Instructions
Try to answer without checking notes.
Recommended time:
25 minutes
Passing score:
80%
There are 25 questions.
Question 1
Which statement best describes IoC?
A. A design pattern for writing SQL queries
B. A principle where control of object creation is moved to the container
C. A way to manually create objects with new
D. A Spring Boot-only feature
My answer:
Question 2
Which statement about Dependency Injection is correct?
A. A class creates all dependencies itself B. Dependencies are provided from outside C. It only works with field injection D. It only works in Spring Boot
My answer:
Question 3
What is a Spring bean?
A. Any Java object
B. Any object created with new
C. An object managed by the Spring container
D. A database record
My answer:
Question 4
Which container is commonly used in modern Spring applications?
A. ApplicationContext B. ArrayList C. EntityManager only D. ServletContext only
My answer:
Question 5
What is BeanFactory?
A. A database factory B. The basic Spring IoC container C. A REST controller D. A Spring Security filter
My answer:
Question 6
Which is true?
A. ApplicationContext extends BeanFactory
B. BeanFactory extends ApplicationContext
C. They are unrelated
D. ApplicationContext is only for testing
My answer:
Question 7
What is a BeanDefinition?
A. The actual object in memory B. Metadata describing how to create a bean C. A REST endpoint D. A SQL table
My answer:
Question 8
Which one registers a bean through component scanning?
A. @Service
B. new
C. private
D. static
My answer:
Question 9
Which annotation is used on a method whose return value becomes a bean?
A. @Service
B. @Bean
C. @Repository
D. @PathVariable
My answer:
Question 10
When should I usually use @Bean?
A. For third-party classes or custom creation logic B. Only for controllers C. Only for repositories D. Never
My answer:
Question 11
What does @SpringBootApplication include?
A. @Configuration, @EnableAutoConfiguration, @ComponentScan
B. @Service, @Repository, @Entity
C. @RestController, @RequestMapping, @Bean
D. @Transactional, @Autowired, @Test
My answer:
Question 12
Where does Spring Boot start component scanning by default?
A. From the package of the class annotated with @SpringBootApplication
B. From all packages in the JVM
C. From the database schema
D. From the operating system
My answer:
Question 13
Does @Service always guarantee a class becomes a bean?
A. Yes, always B. No, it must be discovered by component scanning or registered C. Only if the class is private D. Only if the class has no constructor
My answer:
Question 14
What is the default bean name for this class?
@Service
public class PaymentService {
}
A. PaymentService
B. paymentService
C. servicePayment
D. payment-service
My answer:
Question 15
What is special about @Repository?
A. It can support persistence exception translation B. It creates REST endpoints C. It disables transactions D. It is not a Spring stereotype
My answer:
Question 16
What does @RestController include?
A. @Controller and @ResponseBody
B. @Service and @Bean
C. @Repository and @Transactional
D. @ComponentScan and @Configuration
My answer:
Question 17
Which injection type is recommended for required dependencies?
A. Field injection B. Constructor injection C. Random injection D. Static injection
My answer:
Question 18
Is @Autowired required on a single constructor in modern Spring?
A. Yes B. No C. Only for repositories D. Only for controllers
My answer:
Question 19
What happens if no matching bean exists for a required constructor dependency?
A. Spring injects null by default B. Spring usually fails to start C. Spring creates a random object D. Spring ignores the dependency
My answer:
Question 20
What happens if two beans match the same dependency and there is no @Primary or @Qualifier?
A. Spring injects both automatically into one variable B. Spring chooses randomly C. Spring usually fails because the dependency is ambiguous D. Spring ignores both
My answer:
Question 21
What does @Primary do?
A. Marks one bean as the default choice B. Deletes all other beans C. Creates a database primary key D. Makes a class static
My answer:
Question 22
What does @Qualifier do?
A. Selects a specific bean B. Enables component scanning C. Starts Spring Boot D. Creates a transaction
My answer:
Question 23
Which one is more specific?
A. @Primary
B. @Qualifier
C. They are always equal
D. Neither works with Spring
My answer:
Question 24
What does Spring inject into List<NotificationSender>?
A. Nothing
B. Only the primary bean
C. All beans of type NotificationSender
D. A random string list
My answer:
Question 25
What does Spring singleton mean?
A. One instance per JVM, always B. One instance per Spring container C. One instance per database table D. One instance per HTTP request
My answer:
8. Mini Mock Exam Answers
Answer Key
1. B
2. B
3. C
4. A
5. B
6. A
7. B
8. A
9. B
10. A
11. A
12. A
13. B
14. B
15. A
16. A
17. B
18. B
19. B
20. C
21. A
22. A
23. B
24. C
25. B
9. Score
Total questions: 25
Correct answers:
Wrong answers:
Score:
Score calculation:
correct answers / 25 * 100
Example:
20 / 25 * 100 = 80%
10. Mistake Review Template
For every wrong answer, write this:
## Mistake
Question number:
My wrong answer:
Correct answer:
Why I was wrong:
Correct concept:
Memory sentence:
Example:
## Mistake
Question number: 13
My wrong answer: A
Correct answer: B
Why I was wrong:
I thought @Service always creates a bean.
Correct concept:
@Service only marks a class as a candidate. The class must be discovered by component scanning.
Memory sentence:
Annotation alone is not enough. The package must be scanned.
11. Final Week 1 Oral Exam
Try to answer these out loud.
Question 1
Explain Spring IoC like I am a junior developer.
Question 2
Explain why Dependency Injection improves testing.
Question 3
Explain what happens when Spring Boot starts.
Question 4
Explain the difference between @Component and @Bean.
Question 5
Explain why @Service might not be found.
Question 6
Explain what happens if two beans implement the same interface.
Question 7
Explain the difference between @Primary and @Qualifier.
Question 8
Explain why constructor injection is recommended.
12. Good Oral Answers
Oral Answer 1 — IoC
Spring IoC means that Spring controls object creation and dependency management. Instead of creating services and repositories manually with new, I let the Spring container create and connect them. This makes the application easier to configure, test, and maintain. The objects managed by Spring are called beans.
Oral Answer 2 — Dependency Injection and Testing
Dependency Injection improves testing because a class does not create its dependencies directly. It receives them from outside. That means I can pass a real repository in production and a fake or mock repository in tests. Constructor injection makes this especially simple because dependencies are explicit constructor parameters.
Oral Answer 3 — Spring Boot Startup
When Spring Boot starts, the main method calls SpringApplication.run. Spring creates the ApplicationContext, reads configuration, scans packages, registers bean definitions, creates singleton beans, injects dependencies, runs lifecycle callbacks, starts the embedded web server if needed, and then the application is ready.
Oral Answer 4 — @Component vs @Bean
@Component is used on a class and Spring discovers it through component scanning. It is usually used for my own application classes. @Bean is used on a method inside a configuration class, and the return value becomes a Spring bean. It is useful for third-party classes or objects that need custom creation logic.
Oral Answer 5 — Why @Service Might Not Be Found
A class with @Service might not be found if it is outside the component scan path. In Spring Boot, scanning starts from the package of the class annotated with @SpringBootApplication. Other reasons include inactive profiles, false conditions, missing dependencies, wrong annotation imports, or test slices that do not load the service.
Oral Answer 6 — Two Beans Same Interface
If two beans implement the same interface and another bean depends on that interface, Spring does not know which implementation to inject. The dependency is ambiguous and the application usually fails to start. I can solve this with @Primary, @Qualifier, a more specific type, or by injecting all implementations as a list or map.
Oral Answer 7 — @Primary vs @Qualifier
@Primary marks one bean as the default choice when multiple candidates exist. @Qualifier selects a specific bean explicitly. @Primary is useful when one implementation should usually be used. @Qualifier is more specific and wins when I need an exact bean.
Oral Answer 8 — Constructor Injection
Constructor injection is recommended because it makes dependencies explicit. It allows fields to be final, supports immutability, improves testability, and prevents objects from being created without required dependencies. It also makes design problems like too many dependencies or circular dependencies visible early.
13. Week 1 Final Readiness Checklist
Before moving to Week 2, I should be able to check all of these:
[ ] I can explain what problem Spring solves.
[ ] I can explain IoC.
[ ] I can explain Dependency Injection.
[ ] I can explain the difference between IoC and DI.
[ ] I can explain what a Spring bean is.
[ ] I can explain what ApplicationContext does.
[ ] I can explain BeanFactory.
[ ] I can compare ApplicationContext and BeanFactory.
[ ] I can explain BeanDefinition.
[ ] I can compare BeanDefinition and bean.
[ ] I can explain component scanning.
[ ] I know where Spring Boot scans by default.
[ ] I know why the main class should be in the root package.
[ ] I know what @SpringBootApplication includes.
[ ] I can compare @Component and @Bean.
[ ] I can compare @Component, @Service, and @Repository.
[ ] I know what @RestController includes.
[ ] I can explain constructor injection.
[ ] I know why field injection is discouraged.
[ ] I know @Autowired is optional on a single constructor.
[ ] I can explain what happens if no bean exists.
[ ] I can explain what happens if multiple beans exist.
[ ] I can explain @Primary.
[ ] I can explain @Qualifier.
[ ] I can explain how to inject all beans of one type.
[ ] I know objects created with new are not Spring-managed.
14. Weak Topics to Review Before Week 2
Write weak topics here:
## My Weak Topics
1.
2.
3.
4.
5.
For each weak topic:
## Weak Topic
Topic:
Why it is confusing:
Correct explanation:
Code example:
Memory sentence:
15. Week 1 Final Summary
Week 1 gave me the foundation of Spring.
The most important idea:
Spring manages objects called beans inside the ApplicationContext.
Spring finds beans through:
component scanning
or registers them through:
@Bean methods
Spring connects beans through:
Dependency Injection
Spring solves ambiguity with:
@Primary and @Qualifier
And Spring Boot makes setup easier with:
@SpringBootApplication
which includes:
@Configuration
@EnableAutoConfiguration
@ComponentScan
If I understand this well, I am ready for Week 2.