Skip to content

Spring Inversion Of Control Annotation#

Development Process With Annotations#

  • As we known in Spring Inversion Of Control, there are 3 ways that we can use to configure the Spring Container in Spring Framework:

    • XML configuration file (legacy, but most legacy apps still use this)
    • Java Annotations (modern)
    • Java Source Code (modern)
  • Why do we should use Spring configuration with Annotations?

    • XML configuration can be verbose.
    • Configure your Spring beans with Annotations.
    • Annotations minimizes the XML configuration.
  • So for Configuring Spring Container with Java Annotations, we should follows steps below:

    • Enable component scanning in Spring config file.
    • Add the @Component Annotation to our Java classes.
    • Retrieve bean from Spring Container.

Enable Component Scanning In Spring Config File#

  • When we add an annotation to a class, then Spring will actually scan our java classes for those annotations. When it finds a class that has a special Spring Annotation on it, it will automatically register that bean with the Spring Container.
  • So instead of doing everything long hand via XML config file, Spring will just scan and register beans with the Spring Container. These are two main points in scanning for component classes in Spring Framework.
  • Now, let's use the tag <context:component-scan> and property base-package with the value is the package that we want Spring Framework will 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.spring.core.spring.inversion.of.control.annotation"/>

</beans>

Add @Component Annotation Into Java Classes#

  • We need to add the @Component annotation into our java classes, so with this annotation, we tell to the Spring that this class should be registered when it starts scanning.
  • When we use the @Component annotation we can provide it any bean id name that we want to use. See example below.
EnglishCoach.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.spring.core.spring.inversion.of.control.annotation;  

import org.springframework.stereotype.Component;  

@Component("englishCoach")  
public class EnglishCoach implements Coach {  

    @Override  
    public String getDailyHomeWork() {  
        return "Spend 1 hour to practise Speaking Skill!";  
    }  

}

By default, if we don't provide any bean id name, then the Spring Framework will set the bean id automatically by class name with the first lower case character. Ex: EnglishCoach class -> the bean id will be englishCoach.

Retrieve Beans From Container#

  • In this step, we will do the same what we did before in retrieving beans from container. See the example below:
1
2
3
4
5
// create a spring container
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//retrieve bean from spring container
EnglishCoach englishCoach = context.getBean("englishCoach", Coach.class);

Example With Annotation#

Dependencies#

  • To begin with the example, we will need to use the dependency spring-context for creating the Spring Container.
pom.xml
1
2
3
4
5
6
7
8
9
....  

<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>5.3.24</version>  
</dependency>

....

Enable Component Scanning In Spring Config File#

  • Now, let's create the applicationContext.xml file in our resources folder of our project and add configuration as below.
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.spring.core.spring.inversion.of.control.annotation"/>

</beans>
  • As you can see, we will use the tag <context:component-scan> to tell Spring Framework that we want Spring Framework scans our package and the property base-package is used to tell Spring Framework that where it should do the scanning and register the beans.

Add @Component Annotation Into Java Classes#

  • Let's create an interface and implementation class as below.
Coach.java
1
2
3
4
5
6
7
package com.spring.core.spring.inversion.of.control.annotation;  

public interface Coach {  

    public String getDailyHomeWork();  

}
EnglishCoach.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.spring.core.spring.inversion.of.control.annotation;

import org.springframework.stereotype.Component;

@Component("englishCoach")
public class EnglishCoach implements Coach {

    @Override
    public String getDailyHomeWork() {
        return "Spend 1 hour to practise Speaking Skill!";
    }

}
  • As you can see, in the implement class EnglishCoach. We will use the annotation @Component with value is englishCoach which will tell Spring Framework that, this class is a bean with the bean id is englishCoach and Spring Framework should register it during the scanning.

Retrieve Beans From Container#

  • Next, we can use the bean in the Spring Container by using getBean() method.
SpringApplication.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.spring.core.spring.inversion.of.control.annotation;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {
    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //retrieve bean from the spring container
        Coach englishCoach = context.getBean("englishCoach", Coach.class);

        //use the bean
        System.out.println(englishCoach.getDailyHomeWork());

        context.close();
    }
}

Testing#

  • Finally, we just simply run the main class then we can see the content in the spring bean as below.
1
2
3
Spend 1 hour to practise Speaking Skill!

Process finished with exit code 0

See Also#

References#