Java annotations are special labels or markers that are added to Java classes that provide meta-data about the class. Annotations can be processed at compile or at runtime and we will see both examples in this article.
Here we have a book label with pages, price, code, and so on. All this information is the meta-data about the book.
You may have already come across some annotations like @Override that we use when implementing a given interface. Using it, we tell the compiler that we are overriding a method of the parent class. This happens at the compilation time, the compiler checks our class and makes sure that we really are overriding the method or else we will get a compilation error.
1
2
3
4
5
6
7
public class TrackTeacher implements Teacher {
@Override
public String getHomework() {
return "solve the previous year questions";
}
...
}
Another method is XML configuration, which can be very verbose for large projects. Suppose we have 100 beans with multiple dependencies; creating each and every one would be a mess. Instead, you can actually configure your Spring beans with annotations. Annotations allow us to minimize XML configuration. We basically have boxes, which we then annotate and specify the destination. We add an annotation to a class and then Spring can use that for configuring the system. It scans the classes for special annotations and then registers the beans in the spring container.
Steps to configure inversion of control using Java annotations:
Enable component scanning in Spring config file
Add the @Component annotation to your Java classes
Retrieve bean from Spring container
For better understanding, let’s create a Java project and code through our concept.
Spring container is generally known as ApplicationContext. I am attaching a default code for applicationContext file to get started; go ahead and paste this file into your main src folder. In this XML file, we enable component scanning using context:component-scan.
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.nikhil.anno_demo"/> </beans>
You should provide your package name. In our case it is com.nikhil.anno_demo.
To test this, create an interface called Teacher, with a method called getDailyHomework and an implementation class called ChemistryTeacher. Make sure to add a component annotation to your class with an ID like below.
Teacher.java
1
2
3
4
5
6
package com.nikhil.anno_demo;
public interface Teacher {
public String getDailyHomework();
}
ChemistryTeacher.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.nikhil.anno_demo;
import org.springframework.stereotype.Component;
@Component("theChemTeacher")
public class ChemistryTeacher implements Teacher {
@Override
public String getDailyHomework() {
return "Revise thermodyamics";
}
}
Behind the scenes, Spring automatically registers this bean with bean ID “theChemTeacher”, so later we can retrieve this bean using the bean ID.
Now to retrieve. First make a new class with the main method, then go ahead and do the following steps:
read Spring config file
get the bean from the Spring container
call a method
close context
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.nikhil.anno_demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationDemoApp {
public static void main(String[] args) {
// read spring config file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// get the bean from the spring container
Teacher chem = context.getBean("theChemTeacher", Teacher.class);
// call method
System.out.println(chem.getDailyHomework());
// close context
context.close();
}
}
Now go ahead and run your application.
Console log:
Here we are defining our own bean id in the component attribute, but we can let Spring generate a default bean id. The default bean id is basically the class name with the first letter lowercase. In our case, it will be “chemistryTeacher” for ChemistryTeacher.java.
1
2
@Component
public class ChemistryTeacher implements Teacher
Retrieving bean:Teacher chem = context.getBean("chemistryeacher",Teacher.class);