Home Tutorial Spring Spring3 Ioc Spring filter Bean

 
 

Spring filter Bean
Posted on: September 2, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how to filter the component in spring framework.

Spring Filter Bean

The auto scan property can be gain through @Component annotation. The same feature you can  get through filter the components in the xml configuration file. In this example you will see how to filter the components in Spring framework.

StudentDAO.java

package com.roseindia.student.dao;

import org.springframework.stereotype.Component;

@Component
public class StudentDAO {
	@Override
	public String toString() {
		return " Inside StudentDAO";
	}
}

StudentService .java

package com.roseindia.student.services;

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

import com.roseindia.student.dao.StudentDAO;

@Component
public class StudentService {
	@Autowired
	StudentDAO studentDAO;

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

AppMain .java

package com.roseindia.common;

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

import com.roseindia.student.services.StudentService;

public class AppMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		StudentService stud = (StudentService) context
				.getBean("studentService");
		System.out.println(stud);
	}
}

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.roseindia" >
 
  <context:include-filter type="regex" 
              expression="com.roseindia.student.dao.*DAO.*" />
 
  <context:include-filter type="regex" 
              expression="com.roseindia.student.services.*Service.*" />
 
 <context:exclude-filter type="annotation" 
                 expression="org.springframework.stereotype.Service" />  
</context:component-scan>
 
</beans>

When you run this application it will display message as shown below:


StudentService [studentDAO= inside StudentDAO]

Download this example code

Advertisement

Related Tags for Spring filter Bean:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 2, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com