In this tutorial you will learn how to filter the component in spring framework.
In this tutorial you will learn how to filter the component in spring framework.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"
|
StudentService [studentDAO= inside StudentDAO] |