Learn How to use Spring Filter to match components

0
4084
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.

https://maven.apache.org/download.cgi
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

package com.eduonix.spring.filter;

public class SPFilterDao {

	@Override
	public String toString() {
		return "I am Spring filter DAO";
	}
}

Listing 2: –This is a simple service file

package com.eduonix.spring.filter;

import org.springframework.beans.factory.annotation.Autowired;
import com.eduonix.spring.filter.SPFilterDao;


public class SPFilterService {
	
	@Autowired
	SPFilterDao filterDao;

	@Override
	public String toString() {
		return "SPFilterService [filterDao=" + filterDao + "]";
	}


}

Listing 3: – This is the XML configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="com.eduonix.*" >

		<context:include-filter type="regex"
                       expression="com.eduonix.spring.filter.*Dao.*" />                       
	
		<context:include-filter type="regex"
                       expression="com.eduonix.spring.filter.*Service.*" />                       
        
	</context:component-scan>

</beans>

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

package com.eduonix.spring.filter;

import org.springframework.context.ApplicationContext;
import com.eduonix.spring.filter.SPFilterService;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SPFilterMain {
	
	 public static void main( String[] args )
	    {
		 ApplicationContext context = new FileSystemXmlApplicationContext(
	                "WebContent/WEB-INF/SPFilterConfig.xml");

		 	SPFilterService spfltrsrvc = (SPFilterService)context.getBean("SPFilterService");
	    	System.out.println(spfltrsrvc);

	    }

}

Now, run the main application and it will show you the following output.
Filtermainjava
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

package com.eduonix.spring.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eduonix.spring.filter.SPFilterDao;

@Service
public class SPFilterService {
	
	@Autowired
	SPFilterDao filterDao;

	@Override
	public String toString() {
		return "SPFilterService [filterDao=" + filterDao + "]";
	}


}

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

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="com.eduonix.*" >

		<context:include-filter type="regex"
                       expression="com.eduonix.spring.filter.*Dao.*" />                       
                       
        <context:exclude-filter type="annotation"
					   expression="org.springframework.stereotype.Service" />                      
        
	</context:component-scan>

</beans>

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

package com.eduonix.spring.filter;

import org.springframework.context.ApplicationContext;
import com.eduonix.spring.filter.SPFilterService;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SPFilterMain {
	
	 public static void main( String[] args )
	    {
ApplicationContext context = new FileSystemXmlApplicationContext(
	                "WebContent/WEB-INF/SPFilterConfig.xml");
		 
System.out.println(" In the main file, not looking for the Service file");

//SPFilterService spfltrsrvc = (SPFilterService)context.getBean("SPFilterService");
	    		//System.out.println(spfltrsrvc);

	    }

}

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here