• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Component annotation in spring

July 7, 2015 By j2eereference Leave a Comment

Component annotation in spring:

In previous posts, we have seen how to do wiring of beans inside Spring using annotations. Most of the annotations had a common requirement that the bean which gets injected into, needs to be configured in the xml. In this post we explain about annotation that declares a class as a bean so that we don’t have to even add <bean></bean> tag in the xml. We can have the configurations inside the class itself using annotations. In order to do this, we have to tell Spring what classes are going to be bean and what classes are not. We use @Component annotation to tell Spring that, a particular class is going to be a bean.

Hdfc.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.j2eereference.spring;
 
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
 
@Component
public class Hdfc implements Bank{
private Branch branch;
 
public Branch getBranch() {
return branch;
}
@Resource(name="branchB")
public void setBranch(Branch branch) {
this.branch = branch;
}
 
@Override
public void getBranchInfo()
{
System.out.println("HDFC Branch code : "+branch.getCode()+" and Address : "+branch.getAddress() );
}
}

Defining a class with @Component is equivalent to configuring the bean inside xml. @Component creates the bean with name of the class in lower class.

spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">  
<bean id="branchA" class="com.j2eereference.spring.Branch" >
<property name="code" value="1234"></property>
<property name="address" value="BranchA Address"></property>
</bean>
<bean id="branchB" class="com.j2eereference.spring.Branch" >
<property name="code" value="5678"></property>
<property name="address" value="BranchB Address"></property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.j2eereference.spring"></context:component-scan>
</beans>

Now look at the Branch related beans in xml. We have defined Branch with 2 different names. That is we have two instances of the Branch bean with different names  – branchA and branchB. What happens if we define a bean using @Component annotation and not in the xml, we cannot have more than one instance of the bean. This is obvious because a class with one name can create only instance, we cannot have a different name for it. If we want another instance, we have to create another class with different name and make it a bean using @Component. This is one of the disadvantages of using annotations and advantage of using xml configuration.  This is what happens if we add @Component annotation inside Branch. Hence lets not use it inside Branch class, we will retain the xml configuration for Branch to have different instances for Branch.

Also, spring xml needs to know that we have beans inside the code that it needs to look for. It has to not only look for the beans defined in the xml, it also has to look for the beans defined inside the code using annotations. We need to ask Spring to scan through all the classes and then identify what annotations mark certain classes as beans. In order to do this, we add <context:component-scan> tag. This tag tells Spring that it needs to scan for components, the components being beans marked as @Component. We have to enter the property ‘base-package’. This package tells Spring to which package it has to scan to look for the @Component annotation. We want to it to scan only a particular package because it is more efficient than scanning everything. So we restrict the search to a certain package. It has some other advantages as well which we will see later.

In our main method, we don’t have to do any change. When Spring finds @Component, it registers the class as a bean with same name as the the class (first letter lower-cased).

BankApp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.j2eereference.spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class BankApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Bank bank = (Bank)context.getBean("hdfc");
bank.getBranchInfo();
}
}

So we are set to run this.
Output:
HDFC Branch code : 5678 and Address : BranchB Address

The component-scan tag searches for other annotations as well apart from @Component. These annotations are called as stereotypes. The concept of stereotypes is, when we write enterprise applications in Spring, we would have some standard beans that perform some standard roles. We would have a data object, a service class, a view, a controller etc. All these are stereotypical roles that Spring beans perform in any enterprise application. We have Spring annotations that let us define a bean to perform one of those roles.

Suppose Hdfc.java to be service layer class. Instead of @Component, we could have @Service annotation. Even if we replace now @Component with @Service, it is going to work the same way and produce the same result. But @Service gives additional information to Spring that it is a service bean.

If Hdfc.java were a data object, we could use @Repository which works the same way and tells spring that it is a data object.

@Controller tells spring that it is a Controller class.

The functionality of these stereotyped annotations is no different from @Component. But @Component is something generic and tells spring that it is a bean. The stereotyped annotations give an additional  information about what role the bean performs.

Advantage of using stereotyped annotations:
It adds a level of of documentation. We look at the bean and know what role it performs.

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Spring JSR-250 Annotations

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.