XML Configuration with Java Annotations
Explore how to combine XML configuration and Java annotations to manage Spring beans effectively. Understand how to use the component-scan tag to detect @Component annotated beans and enable autowiring with @Autowired. Learn the differences between context:component-scan and context:annotation-config tags and see how to streamline configuration in large projects using annotation-based dependency injection.
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 ...