Spring – Annotation-based Inversion of Control (IoC)

Spring – Annotation-based Inversion of Control (IoC)

We have previously discussed an example of Spring IoC (Inversion of Control). In the previous case, we have used the bean-configuration file to define the bean information. If we want to change the existing bean or set a new bean in the bean-config file, we need to write the complete details of that particular bean. Although we have set only one bean in the bean-config file, if there were several beans, it would create a lot of mess in the bean-config file.

To overcome the above disadvantage of XML-based configuration, we use annotation-based configuration.

We are going to use the @Component annotation in the bean and <context:component-scan>…<context:component-scan> in the bean-config file (applicationContext) to access the annotation-based configuration.

@Component- It is used to denote a class as Component, which means using the annotation-based configuration, the Spring container will auto-detect these classes for dependency injection.

The Component performs some specific operations. It is a parent stereotype annotation. The Spring framework provides three other specific annotations taht can be used to define a class as a Component.

Following are the sub annotations of @Component:

  • @Service – It is used to denote a Component as a Service and also handles the business logic.
  • @Repository – It is used to denote a Component as a Repository and also handles the data access logic.
  • @Controller – It is used to denote a Component as a Spring MVC Controller.

Following code shows how to use the @Component annotation:

@Component("mySim")
public class Jio_Sim implements Sim {
public String calling() { }
} 

Example of Annotation-based Inversion of Control (IoC)

Now, we are going to create an example of the annotation-based configuration. In this example, we have defined an interface Sim, that contains an unimplemented method. The class Jio_Sim implements the Sim interface and its unimplemented method.

Following are the steps to create an example of the annotation-based configuration:

Sim.java

public interface Sim {
public String calling();
} 

Jio_Sim.java

import org.springframework.stereotype.Component;
@Component("mySim")
public class Jio_Sim implements Sim {
public String calling() {
return "Annotattion based Configuration : Calling using Jio Sim  ";
}
} 

The <context:component-scan> property enables the auto-scanning feature in the Spring framework. It contains an attribute base-package, used to add the base package where all the files are stored.  The Spring container scans the provided base package and finds out the bean class annotated with @Component.

applicationContext.xml


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-3.0.xsd">

 

App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Sim s = ApplicationContext.getBean("mySim", Sim.class);
System.out.println(s.calling()); 
ApplicationContext.close();
}
} 

Output

applicationContext.xml

Default Component Names

In the above example, we have explicitly provided the Component name -@Component("mySim") in the Jio_Sim.java class.

By default, the Spring container will lower case the first character of the Component class name, as shown below:

Jio_Sim is the Component class name.

@Component
public class Jio_Sim implements Sim{ }
Sim s = ApplicationContext.getBean("jio_Sim", Sim.class); 

We can retrieve the Component by providing either an implicit or explicit Component name. In both cases, the output will be the same.


Related Topics

Spring AOP – @After Advice

Spring AOP – @After Advice We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice...

3 minutes read.

Spring AOP – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.

Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are...

7 minutes read.

Spring Tutorial

What is Spring Framework? Spring is one of the most popular enterprise application framework used for developing Java applications. “Spring is a lightweight open-source framework. It allows Java developer to build simple, reliable,...

6 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.

Spring MVC Example

Spring MVC Example We have previously discussed the Spring Web MVC framework. In this tutorial, we are going to create a simple example of Spring Web MVC. Following are the steps used to create an example...

2 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.

Spring AOP – @Before Advice Type

Spring AOP – @Before Advice Type As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology,...

2 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 minutes read.

Spring Autowiring –Field Injection

Spring Autowiring –Field Injection The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a...

2 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we...

2 minutes read.

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 minutes read.

Spring - Bean Scope

Spring - Bean Scope Bean Scope refers to a lifecycle of the bean. It allows us to have control over the creation of bean objects. It is a very powerful approach and provides...

6 minutes read.

Spring MVC Form Text Field

We have previously discussed the Spring MVC form tags. The form tag library provides a variety of form tags that are used to build web applications. One of them is a <form:input>...

8 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring – Configuration using @Configuration

Spring – Configuration using @Configuration We have previously discussed the Spring configuration using an XML configuration file. In that case, we have defined all the bean information in the bean-configuration file (applicationContext.xml). ...

3 minutes read.