How to filter components in auto scanning?
In the previous example
you have seen how to enable auto component scanning. This page shows an example to include and exclude components based on your requirements.
You can filter your components during enabling auto component scanning level, it should be configured at xml based configuration
level. Here is an example for including auto scan components:
package com.java2novice.dao;
import org.springframework.stereotype.Component;
@Component
public class InventoryDao {
public void printInventoryDaoStatus(){
System.out.println("Inside InventoryDao & status method");
}
}
|
package com.java2novice.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.java2novice.dao.InventoryDao;
@Service("j2nInventoryService")
public class J2nInventoryService {
@Autowired
private InventoryDao inventoryDao;
public void testInventoryServce(){
System.out.println("Inside service class, testInventoryServce()");
inventoryDao.printInventoryDaoStatus();
}
}
|
Here is the xml based configuration, which includes a filter to include components, the filter is specified using "include-filter"
tag. Note that below filtering components based on regular expression, and annotation based. Below xml configuration including all Components
annotated with @Component annotation and any class containing "Service" String.
<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:component-scan base-package="com.java2novice">
<context:include-filter type="regex"
expression="com.java2novice.service.*Service.*" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.service.J2nInventoryService;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
J2nInventoryService inventoryService
= (J2nInventoryService) context.getBean("j2nInventoryService");
inventoryService.testInventoryServce();
}
}
|
Output: |
Inside service class, testInventoryServce()
Inside InventoryDao & status method
|
Now we will see example for how to exclude few components from auto scanning. In the below xml based configuration file, we are
excluding few components by using "exclude-filter" tag type.
<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:component-scan base-package="com.java2novice">
<context:exclude-filter type="regex"
expression="com.java2novice.service.*Service.*" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
</beans>
|
Output: |
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'j2nInventoryService' is defined
|
|