XML Configuration with Java Annotations
Learn how to use a combination of XML context and Java annotations.
We'll cover the following
In the last lesson, we removed all Java annotations from our application and used the appContext.xml
file to define beans and inject the dependency. However, if we want to detect beans defined by the @Component
annotation and inject the dependencies using @Autowired
annotation while using XML context, we can do that too.
In large projects, declaring a lot of beans using the <bean>
tag is cumbersome, so annotation-based dependency injection was introduced in Spring 2.52.52.5. This enabled automatic detection of beans having the @Component
annotation. The <context:component-scan>
tag is used to turn this feature on.
For the code example shown in this lesson, we use create a new package named lesson15
by copying lesson14
package from the previous lesson.
<context:component-scan>
tag
Right now, we have declared three beans in appContext.xml
. Suppose we want to declare the ContentBasedFilter
and CollaborativeFilter
beans using the @Component
annotation instead of defining them using the <bean>
tag in appContext.xml
.
Here’s the ContentBasedFilter
class:
Get hands-on with 1400+ tech skills courses.