Exclude Certain class from Spring Component-Scan


In this article, we will learn how to exclude a certain class Annotated with @Component or @Service or any other annotation. So let's start.

EmployeeService.java

package com.sample.employee.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.sample.employee.dao.EmployeeDAO; @Servicepublic class EmployeeService { @Autowired EmployeeDAO employeeDAO; @Override public String toString() { return "EmployeeService [EmployeeDAO=" + EmployeeDAO + "]"; } }

EmployeeDAO.java

package com.sample.employee.dao; import org.springframework.stereotype.Component; @Component public class EmployeeDAO { @Override public String toString() { return "This is EmployeeDAO"; } }

Now Lets try to exclude EmployeeDAO from Component-Scan in applicationContext.xml


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.sample"> <context:exclude-filter type="regex" expression="com.sample.employee.dao.*DAO.*" /> </context:component-scan> </beans>

In the above applicationContext, you can see we have added a single line , which basically means exclude a class from component-scan whose package is "com.sample.employee.dao" and class name includes "DAO"

        


Need Programming or any Technical Related Books, Courses or Help Chat with us

Comments



Deals of the Day
Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online


Search Tags

    Filter one class from Spring Componentscan