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.



ADVERTISEMENT
ADVERTISEMENT