Stereotype Annotations in Spring Framework

  • By Pooja Nandode-Bhavsar
  • February 28, 2023
  • JAVA
Stereotype Annotations in Spring Framework

Stereotype Annotations in Spring Framework

In spring, Stereotype Annotations in Spring Framework we need to define bean configuration in XML file means the name of the bean, the class name with which that bean is associated etc by using <bean> tag. But now we dont need to use <bean> tag in xml file we can directly specify in bean class(Emp) that we want to create the bean of this class in xml file, without using <bean> tag in xml and we can do this by using @component annotation in bean class. Means the <bean> tag in xml and @component in bean class both are same.  Using @Value annotation we can spevcify the value of the bean which we can specify in xml using <property> tag or p schema. Like @Component and @Value the Spring Framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context of the project . With @Component, @Repository, @Service, and @Controller @value annotations in place and automatic component scanning enabled, Spring will automatically import the beans into the container and inject to dependencies. These annotations are called Stereotype annotations

 

Looking to jumpstart your career in software development or take your coding skills to the next level? Look no further than Java Training in Pune!

Our comprehensive Java training program covers all aspects of the Java programming language, from the basics of syntax and data structures to advanced topics such as multithreading and network programming. With our expert instructors and hands-on projects, you’ll gain practical experience and develop a deep understanding of Java’s capabilities and applications.

 

@Component  Annotation

@Component is a class-level annotation means @Component annotation can be applied on the class. It is used to indicate a class as a Component. The class which is annotated with @Component can be managed by the Spring IOC container means the bean of that class can be automatically created by the Spring IOC container with the same name as a class name but the first letter of that bean should start with a small case letter. For example, if the class name is Student then the bean name is “student”.

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

Stereotype Annotations in Spring Framework

@Value 

If we are using @Component annotation on the class then we don’t need to specify that bean configuration in an xml file, which means we don’t need to use bean tag in xml file. Spring IOC container will automatically create the bean but we need to initialize the property of that bean using @Value annotation on each property at the place where we have declared those properties in the class.

Program to demonstrate the use of @Component and @Value Stereotype annotation in Spring.

1.Student.java

package com.springcore.stereotype;

 

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

import org.springframework.stereotype.Component;

@Component  

public class Student

 {  

@Value(“Pooja Bhavsar”)    //@Value annotation is used to specify the value of the property of bean here ////if we are not using @Value annotation here then we need to use setters in main() to set the values of bean properties

private String s_name;

  

     @Value(“Pune”)

     private String s_city;   //${12+3}

  

// @Value(“#{2*4-2}”)  

        @Value(“9”)

  private int age;

 

public String getS_name() {

return s_name;

}

 

public void setS_name(String s_name) {

this.s_name = s_name;

}

 

public String getS_city() {

return s_city;

}

 

public void setS_city(String s_city) {

this.s_city = s_city;

}

 

public int getAge() {

return age;

}

 

public void setAge(int age) {

this.age = age;

}

 

public Student() {

super();

// TODO Auto-generated constructor stub

}

 

public Student(String s_name, String s_city, int age) {

super();

this.s_name = s_name;

this.s_city = s_city;

this.age = age;

}

 

@Override

public String toString() {

return “Student [s_name=” + s_name + “, s_city=” + s_city + “, age=” + age + “]”;

}

}

 

2.config.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

 

<beans

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”

xmlns:p=”http://www.springframework.org/schema/p”

xmlns:context=”http://www.springframework.org/schema/context”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns=”http://www.springframework.org/schema/beans”>

 

 <context:component-scan base-package=”com.springcore.stereotype”/>

 

</beans>

 In the above xml file we are specifying to the Spring IOC Container that the classes which is annotated with @Component annotation has kept in given component scan package so that spring IOC container can thoroughly scan that package`

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

  1. Main file

import org.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public class Stereo_Main {

              public static void main(String[] args) 

              {

ApplicationContext context = new ClassPathXmlApplicationContext(“com/springcore/stereotype/stereo_config.xml”);

  Student s1 =(Student)context.getBean(“student”);  //here,student is bean name

  System.out.println(s1);

}

}

 

Stereotype Annotations in Spring Framework @Configuration and @Bean

In the project of Spring, generally we need to define bean configuration in an xml file using <bean> tag. But we can avoid or remove xml based configuration from the spring and at that place we can use annotation  based java configuration. Means instead of writing configuration in xml file we can write that configuration in java file just we need to annotate that class with @Configuration annotation so that spring IOC container can understand that spring bean configuration has defined in that class which is marked as @Configuration annotation.

Looking to master the art of programming in the most versatile and widely-used language in the industry? Look no further than our Java Course in Pune!

Our comprehensive course covers all aspects of the Java programming language, from basic syntax and data structures to advanced topics such as multithreading and network programming. With our expert instructors and hands-on projects, you’ll gain practical experience and develop a deep understanding of Java’s capabilities and applications

@Bean

The method which is annotated with @Bean annotation indicates that is generating bean of the spring bean class. If we are using @Bean annotation then there is no need to use @Componenet annotation above the bean class. The spring IOc container will automatically create the bean of the class and give the name to that bean which is exactly same as a method name which is annotated with @Bean annotation.

 

Program on the use of @Configuration and @Bean annotation

  • Student8.java

import org.springframework.stereotype.Component;

 

//@Component   //If we are not using @Component annotation then we need to use @Bean annotation in java Configuration file(WithoutXml.java)

public class Student8 {

 

  int rno ;

  String name;

  

public int getRno() {

return rno;

}

public void setRno(int rno) {

this.rno = rno;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Student8(int rno, String name) {

super();

this.rno = rno;

this.name = name;

}

public Student8() {

super();

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return “Student8 rno=” + rno + “, name=” + name;

}

}

2.WithoutXml .java

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.stereotype.Component;

 

@Configuration      //the class which is annotated with @Configuration  annotation…can consists of bean configuration(just like .xml configuration file)….

@ComponentScan(basePackages = “com.springcore.removingXML”)

public class WithoutXml 

{

  //First bean

  @Bean      //@Bean Indicates that a method generates a bean to be managed by the Spring container.

  public Student8 getStudBean() //here, getStudBean() is bean id means the name of bean

  {

   

  Student8 stud=new Student8(45,”riya”);

   

  return stud ;

  }

   

  //Second bean

  @Bean

  public Student8 getStudBean123()  //by default the name of this 2nd bean is getStudBean123()..

  {

  Student8 stud=new Student8(23,”yash”);

   

  return stud ;

  }

//Third bean 

  @Bean(name= {“s1″,”ss”,”st”,”stu”})  //now, here the name of this 3rd bean is NOT getStud()…bcz,in bean annotation e have used array…so the bean name can be  one of this “s1″,”ss”,”st”,”stu”

  public Student8 getStud()

  {

  Student8 stud=new Student8(12,”yashshri”);

   

  return stud ;

  }

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

3.Removing_XmlMain1.java

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Removing_XmlMain1 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

//In below statement we are using AnnotationConfigApplicationContext class instead of ClassPathXmlApplicationContext class bcz, here we not using xml file for bean configuration                                

//Here,we are using annotation based java confiuration…and to give the path of that java cnfiguration file we need to use AnnotationConfigApplicationContext

ApplicationContext context = new AnnotationConfigApplicationContext(WithoutXml.class);

      

 

Student8 s8 =(Student8)context.getBean(“getStudBean”);

Student8 s9=(Student8)context.getBean(“getStudBean123”);

Student8 s10=(Student8)context.getBean(“getStud”);

// Student8 s8 =(Student8)context.getBean(“stud”);

// s8.setRno(74);

//s8.setName(“ram”);

System.out.println(“getStudBean =”+s8);

System.out.println(“getStudBean123 =”+s9);

System.out.println(“third bean =”+s10);

}

 

}

 

Author:-

Pooja Nandode-Bhavsar

Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*