Stereotype annotations
By Autowire Concept we can get the Object of the other class without configuring in xml.
Ex: Employee Class we have Dept Object. We can access Dept Object without configuring property in xml, But we have to declare all the beans or components in XML bean configuration file.
So that spring container can detect and register your beans or components.
Ex: Employee Class we have Dept Object. We can access Dept Object without configuring property in xml, But we have to declare all the beans or components in XML bean configuration file.
So that spring container can detect and register your beans or components.
Example:
StudentDAO.java
package dao;
public class StudentDAO {
public void getData(){
System.out.println("Hello, Satya Kaveti");
}
}
StudentService.java
package service;
import org.springframework.beans.factory.annotation.Autowired;
import dao.StudentDAO;
public class StudentService {
@Autowired -----------(1)
StudentDAO studentDAO;
public void getData(){
studentDAO.getData();
}
public StudentDAO getStudentDAO() {
return studentDAO;
}
}
spring.xml
<beans>
<context:annotation-config/>
<bean id="dao" class="dao.StudentDAO" autowire-candidate="true">--- (2)</bean>
<bean id="s" class="service.StudentService"> ----- (3)
</bean>
</beans>
StudentMain.java
import service.StudentService;
public class StudentMain {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("res/spring.xml");
StudentService ob = (StudentService)context.getBean("s");
ob.getData();
}
}
In above (1) we used annotations to remove manual object dependency Injection
But in (2) & (3) we are still configuring bean classes
By using Stereotype annotations we can remove those lines also
Stereotype annotations Working
· Actually, spring is able to auto scan
· Detect and instantiate your beans from project package, not even write single line in XML file.
· For this we use Stereotype annotations
· org.springframework.stereotype is base packge
We have following Annotation Types Summary
1. @Component Indicates that an annotated class is a "component".
2. @Controller Indicates that an annotated class is a "Controller" (e.g. a web controller).
3. @Repository Indicates that an annotated class is a "Repository" (or "DAO").
4. @Service Indicates that an annotated class is a "Service" (e.g. a business service facade).
Spring automatically scans and identifies all these classes
that are annotated with “ @Component, @Service, @Repository, @Controller”
and registers Bean Definition with ApplicationContext