Spring Framework Stereotype Annotations

 @Component: It is a class-level annotation. It is used to mark a Java class as a bean. A Java class annotated with @Component is found during the classpath. The Spring Framework pick it up and configure it in the application context as a Spring Bean.

A component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as a Component.

  1. @Service
  2. @Repository
  3. @Controller

1: @Service: We specify a class with @Service to indicate that they’re holding the business logic. Besides being used in the service layer, there isn’t any other special use for this annotation. The utility classes can be marked as Service classes.

2: @Repository: We specify a class with @Repository to indicate that they’re dealing with CRUD operations, usually, it’s used with DAO (Data Access Object) or Repository implementations that deal with database tables.

3: @Controller: We specify a class with @Controller to indicate that they’re front controllers and responsible to handle user requests and return the appropriate response. It is mostly used with REST Web Services.


Note: All these four annotations are available in package ‘org.springframework.stereotype’ and part of ‘spring-context jar’.




Example

Student.java

package stereotypeComponentAnnotation;


import java.util.List;


import org.springframework.beans.factory.annotation.Value;


import org.springframework.stereotype.Component;


@Component

public class Students {


@Value("shivam")

private String name;


@Value("11")

private int roll;


@Value("#{deg}")

private List<String>degree;



public List<String> getDegree() {

return degree;

}

public void setDegree(List<String> degree) {

this.degree = degree;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getRoll() {

return roll;

}

public void setRoll(int roll) {

this.roll = roll;

}

@Override

public String toString() {

return "Students [name=" + name + ", roll=" + roll + ", degree=" + degree + "]";

}



}



Config file :- Add this line 

<context:component-scan base-package="stereotypeComponentAnnotation" />

stereotypeComponentAnnotation = package name of your student class


.........................................................................


<?xml version="1.0" encoding="UTF-8"?>

<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"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="stereotypeComponentAnnotation" />

<util:list list-class="java.util.Vector" id="deg" >

<value>BA</value>

<value>MA</value>

</util:list>

</beans>




Test.java


package stereotypeComponentAnnotation;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub


ApplicationContext context=new ClassPathXmlApplicationContext("stereotypeComponentAnnotation/component.xml");

Students students=context.getBean("students",Students.class);

System.out.println(students);

}


}




//Student student (object name) By default it take object as a class name with 1st letter in small letters



// getBean("students",Students.class) // student is bean or object of Student class


GitHub Link :- https://github.com/shivamofficial/spring-Workspace

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence