Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to filter components in Spring Framework using include-filter and exclude-filter tag ?.

Program to demonstrate how to filter components in Spring Framework using include-filter and exclude-filter tag

Generally, whenever we annotate classes with @Component, @Repository, @Service and @Controller annotation they get automatically scan by 'context:component-scan' and registered as Spring beans. The 'context:component-scan' has a base-package attribute which takes package name. It uses this package name and scans all the beans in the package and its sub-packages. The bean which has annotation as @Component, @Repository, @Service and @Controller etc ... its treated as Spring bean automatically. In order to exclude or include few of the beans from this package, we use sub-elements of 'context:component-scan' tag such as : include-filter or exclude-filter. Lets see each of them below separately :

a. Include Filter

1. Create a class by name Side.java having @Component annotation and place it in the package 'com.hubberspot.spring.components'. This package will have our all the java files and this package entry goes into Spring frameworks configuration xml in the 'context:component-scan' tag. By providing this entry we are telling Spring container to automatically scan this package and configure the classes as components/bean

package com.hubberspot.spring.components;

import org.springframework.stereotype.Component;


// We use @Component Annotation to make a class
// being automatically treated as a Spring bean
// without making any entry for it in Spring
// Configuration xml file 

@Component
public class Side {

 private int length;
 private int pointX;
 private int pointY;


 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }

 public int getPointX() {
  return pointX;
 }

 public void setPointX(int pointX) {
  this.pointX = pointX;
 }


 public int getPointY() {
  return pointY;
 }

 public void setPointY(int pointY) {
  this.pointY = pointY;
 }



}




2. Create a class Square.java having @Component annotation and being @Autowired by Side bean defined at step 1 -

package com.hubberspot.spring.components;

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

//We use @Component Annotation to make a class
//being automatically treated as a Spring bean
//without making any entry for it in Spring
//Configuration xml file 

@Component
public class Square {

 // Square has Side as its one 
 // of the property which is 
 // being autowired to it by placing 
 // @Autowired annotation over it


 @Autowired
 private Side side;


 public Side getSide() {
  return side;
 }


 public void setSide(Side side) {
  this.side = side;
 }



}




3. Create a Spring Configuration file having 'context:component-scan' tag with base-package="com.hubberspot.spring.components". This attribute provides Spring the information about the beans that needs to be scan in the package. This tag also has a sub-element 'context:include-filter' having two attributes as 'type' and 'expression' attributes which tells Spring container that type is of regular expression whose value is been provided by expression attribute. Now by this strategy Spring gets to know that which classes are to be scanned and treated as bean



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.

<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- In order to turn on Auto Scanning in Spring we need to provide a tag 
        by name <context:component-scan> see the tag below .... -->

    <context:component-scan base-package="com.hubberspot.spring.components">

        <!-- It will include all the classes in the package com.hubberspot.spring.components 
            that ends with 'ide' such as Side.java -->

        <context:include-filter type="regex"
            expression="com.hubberspot.spring.components.*ide.*" />

        <!-- It will include all the classes in the package com.hubberspot.spring.components 
            that ends with 'quare' such as Square.java -->

        <context:include-filter type="regex"
            expression="com.hubberspot.spring.components.*quare.*" />

    </context:component-scan>

</beans>



4. Run the Test class

package com.hubberspot.spring.components;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Square object. Without implementing new keyword we 
  // have injected object of Square just by reading an xml 
  // configuration file.
  Square square = (Square)context.getBean("square");

  System.out.println("The length of Square is : " + 

square.getSide().getLength());
  System.out.println("The x co-ordinate of Square is : " + 

square.getSide().getPointX());
  System.out.println("The y co-ordinate of Square is : " + 

square.getSide().getPointY());

 }

}



Output of the program :














b. Exclude Filter

1. Same Side.java

package com.hubberspot.spring.components;

import org.springframework.stereotype.Component;


// We use @Component Annotation to make a class
// being automatically treated as a Spring bean
// without making any entry for it in Spring
// Configuration xml file 

@Component
public class Side {

 private int length;
 private int pointX;
 private int pointY;


 public int getLength() {
  return length;
 }

 public void setLength(int length) {
  this.length = length;
 }

 public int getPointX() {
  return pointX;
 }

 public void setPointX(int pointX) {
  this.pointX = pointX;
 }


 public int getPointY() {
  return pointY;
 }

 public void setPointY(int pointY) {
  this.pointY = pointY;
 }



}




2. Square.java

package com.hubberspot.spring.components;

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

//We use @Component Annotation to make a class
//being automatically treated as a Spring bean
//without making any entry for it in Spring
//Configuration xml file 

@Component
public class Square {

 // Square has Side as its one 
 // of the property which is 
 // being autowired to it by placing 
 // @Autowired annotation over it


 @Autowired(required=false)
 private Side side;


 public Side getSide() {
  return side;
 }


 public void setSide(Side side) {
  this.side = side;
 }



} 


3. Spring Configuration xml file with exclude-filter



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.

<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- In order to turn on Auto Scanning in Spring we need to provide a tag 
        by name <context:component-scan> see the tag below .... -->

    <context:component-scan base-package="com.hubberspot.spring.components">

        <!-- It will exclude all the classes in the package com.hubberspot.spring.components 
            that ends with 'ide' such as Side.java -->

        <context:exclude-filter type="regex"
            expression="com.hubberspot.spring.components.*ide.*" />


    </context:component-scan>

</beans>



4. Run the Test class

package com.hubberspot.spring.components;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Square object. Without implementing new keyword we 
  // have injected object of Square just by reading an xml 
  // configuration file.
  Square square = (Square)context.getBean("square");

  if(square.getSide()==null){
   System.out.println("Side bean is injected as null ");
  }
  else{
   System.out.println("The length of Square is : " + 
     square.getSide().getLength());
   System.out.println("The x co-ordinate of Square is : " +
     square.getSide().getPointX());
   System.out.println("The y co-ordinate of Square is : " +
     square.getSide().getPointY());
  }
 }

}




Output of the program :




 
© 2021 Learn Java by Examples Template by Hubberspot