In Spring context XML file, Filter has a sub-element 'context:include-filter' having two attributes as 'type' and 'expression' attributes that indicate to Spring container that 'type' is of regular expression whose value is been provided by expression attribute. By this concept Spring gets to know that which classes are to be scanned and treated as bean.
1. Filter component include
Use the below example to see how we use Spring filtering to scan and register components name which matched defined regex, even the class is not annotated with @Component.
DAO layer
Define the DAO class as below:
EmployeeDAO.java
package com.example.employee.dao;
public class EmployeeDAO
{
@Override
public String toString() {
return "Hello , We are in EmployeeDAO";
}
}
Service layer
Define Service class as below:
EmployeeService.java
package com.example.employee.services;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.employee.dao.EmployeeDAO;
public class EmployeeService
{
@Autowired
EmployeeDAO employeeDAO;
@Override
public String toString() {
return "EmployeeService [employeeDAO=" + employeeDAO + "]";
}
}
Spring filtering.
Now define the below code in app-context.xml
<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.example" >
<context:include-filter type="regex"
expression="com.example.employee.dao.*DAO.*" />
<context:include-filter type="regex"
expression="com.example.employee.services.*Service.*" />
</context:component-scan>
</beans>
Now define the below class to run the code:
App.java
package com.example.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.employee.services.EmployeeService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"app-context.xml"});
EmployeeService emp = (EmployeeService)context.getBean("employeeService");
System.out.println(emp);
}
}
Output
EmployeeService [employeeDAO=Hello , This is EmployeeDAO]
In this XML filtering, all filess name contains DAO or Service (DAO., Services.) word will be detected and registered in Spring container.
2. Filter component exclude
You can also exclude specified components, to avoid Spring to detect and register it in Spring container. For this define the below code in your app-context.xml
Exclude those files name contains DAO word.
<?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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.example" >
<context:exclude-filter type="regex"
expression="com.example.employee.dao.*DAO.*" />
</context:component-scan>
</beans>
Hope this will help you:)
0 Comment(s)