Spring Configuration and Component Scan
Learn about a few interview questions regarding Spring configuration and component scan.
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
.
Press + to interact
<bean name="myDependency"class="io.datajek.spring.MyDependency"></bean><bean name="myBean"class="io.datajek.spring.MyBean"><constructor-arg ref="myDependency"/></bean>
-
Annotation based configuration: In this approach, annotations are used. The
@Component
as well as some other ...