XML Application Configuration

Let’s have a look at how to configure a Spring application using XML. We will remove annotations from our application altogether and use XML to define beans and dependencies.

For the code example shown in this lesson, we have created a sub-package called lesson14 inside the package io.datajek.spring.basics.movierecommendersystem. The package contains MovieRecommenderSystemApplication.java, Filter.java, ContentBasedFilter.java, CollaborativeFilter.java, and RecommenderImplementation.java files copied from the previous lesson.

We will remove all annotations from the code. This includes the @Configuration and @ComponentScan annotations from the MovieRecommenderSystemApplication class and @Component, @Qualifier, and @Autowired annotations from RecommenderImplementation, ContentBasedFilter, and CollaborativeFilter classes.

XML configuration file

The first step is creating an XML file that contains the bean definitions. Spring will read this file and know which beans to create and manage. We will create an XML file in src/main/resources and call it appContext.xml.

First, we need to provide some metadata for validating the tags which will be used in this file. The metadata defines the schema location of the tags, as shown:

Get hands-on with 1400+ tech skills courses.