Learn How to use Spring Filter to match components

Spring Filter

Spring Filter

Spring Filter is one of the basic functionality in Spring framework. Filters are used for various purposes. The basic functionality of Spring filters are to control bean registration by the Spring container. The filters are mainly configured in the Spring configuration file. And the beans are either auto-scanned or detected by writing code. In our article, we will explore the different filter types and implement them by writing code examples.

Environment setup:-
Setting up the environment for Spring based application development involves the following three major steps. Apart from the following, you can also install build tools for building your application. Maven is one of the most popular automatic build tool.
• Setting up Java Development Kit
Download the JDK from the Oracle site and then, install and configure it. Then the PATH and JAVA_HOME environment variables must be set.
• Setting of Eclipse IDE
Eclipse can be downloaded from the official website. Once downloaded, unpack the binaries and then set the PATH as well.
• Setting up Spring libraries
The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.

If you want to use the Maven build tool, the following steps can be followed. But it is not a mandatory requirement for running the following applications.
First, download the Maven binaries from the link mentioned below. Unzip the binaries in a folder and add bin folder to the path variable.

Now, follow the steps to integrate the maven plug-in for eclipse.

1. Open eclipse IDE
2. Go to Help —>Install new software
3. Search for the maven plug-in releases
4. Select the release available
5. Accept the license and install it
6. Re-start the IDE to work with the plug-in properly
Maven can be used as a plug-in or as a separate installation for building the projects.

Sample application: –
In this section, we will check how different types of filters can be implemented in a Spring based application. First, we will check the include filter, which identifies the components to be registered by the container.

Listing 1: – This is a simple DAO file

Listing 2: –This is a simple service file

Listing 3: – This is the XML configuration file

Listing 4: – This is the main file to test the filters

Now, run the main application and it will show you the following output.


In our second example, we will check how the exclude filter can be implemented. This type of filters are used to exclude the components from being registered in the Spring container. The container checks the configuration file and excludes the component beans being marked by the exclude filter.

In our example, we will exclude the beans marked by the annotation @Service. We need to change the following two files to implement this exclude filter.

Listing 5: – This is the service file marked with @Service

Listing 6: – This is the modified configuration file with exclude filter

Let us keep the other two files as it is (as written for the previous example). Now, run the main file and it will show the following output on the console.

Showing exclude filter output

The output is showing as an exception that the bean is not defined. It is the correct output, as the bean has been excluded in the configuration file by implementing the ‘exclude’ filter. So it has not been registered by the Spring container and hence not found during the search in the main file. The following code snippet in the main file is throwing the error.

Listing 7: – This is the modified main file

Now, if we comment out the above section in the main file and run it once again, it will show the following output only, without any exception. The exception is not thrown as we are not more searching for the excluded component in the main program.

Showing exclude filter output without exception

Conclusion: – In this article, we have discussed various types of filters used in Spring applications. The most important filters are exclude and include filter. These are used to control the bean registration by the Spring container. We have also implemented two applications to check the coding part and how the output is processed. Hope this examples will help you to write Spring filters effectively.


Please enter your comment!
Please enter your name here