Spring Configuration and Component Scan
Learn about a few interview questions regarding Spring configuration and component scan.
We'll cover the following
- What is a configuration file?
- How are beans configured in the Spring container?
- What is the difference between XML and annotation configuration?
- How is annotation based configuration enabled in Spring?
- Can there be multiple configuration files in a project?
- What is a component scan?
- How is a component scan done in Spring Boot?
- What is the difference between context: annotation-config and context:component-scan tags?
What is a configuration file?
A configuration file is an XML file that describes the different classes in the application and how they are linked to one another. XML configuration files can get lengthy and difficult to manage in large projects. Annotation based approach is easier and cleaner.
How are beans configured in the Spring container?
Configuration metadata can be provide in the following ways:
- XML based configuration: In XML config files, beans are defined using the
<bean>
tag containing a number of different configuration options. For example, the following XML configuration defines two beans and specifies constructor injection to inject themyDependency
bean inmyBean
.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.