Skip to content

Spring Inversion Of Control Java Code#

Development Process With Java Source Code#

  • 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)
  • In which, for the first XML configuration file and Java Annotations we have to maintain the XML Spring configuration file, so our projects will not be a pure Java Code. However, in the third way Java Source Code, Spring Framework provides for us the ability to write Spring Container configuration by Java Code and no need to maintain the XML Spring configuration file anymore.

  • So for Configuring Spring Container with Java Source Code, we should follows steps below:

    • Create a Java class and annotate as @Configuration.
    • Add component scanning support: @ComponentScan (Optional).
    • Read Spring Java configuration class.
    • Retrieve bean from Spring Container.

Create a Java class and annotate as @Configuration#

  • So in the step one, we just simply create a java class and we annotate it with @Configuration annotation.
ApplicationConfig.java
1
2
3
4
5
6
7
8
package com.spring.core.spring.inversion.of.control.java.source.code.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfig {

}
  • So this class is name ApplicationConfig and it contains empty config there. Later this class will be used to add some bean configurations.

Add component scanning support (Optional)#

  • Now, we will continue to add the @ComponentScan annotation, this annotation will tell Spring: let's scan and register beans with the Spring Container. This annotation is like the tag <context:component-scan> that we used before in XML configuration file.
  • In the @ComponentScan, we have to provide the values for the property basePackages which are the packages that we want the Spring Framework to scan.
    • Ex: @ComponentScan(basePackages = {"com.spring.core.spring.inversion.of.control.java.source.code"})
ApplicationConfig.java
1
2
3
4
5
6
7
8
9
package com.spring.core.spring.inversion.of.control.java.source.code.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.spring.core.spring.inversion.of.control.java.source.code"})
public class ApplicationConfig {
}
  • In case, we don't want to use the @ComponentScan annotation, so we have configure Spring Beans manually in this ApplicationConfig.java.
ApplicationConfig.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.spring.core.spring.dependency.injection.java.source.code.config;  

import com.spring.core.spring.dependency.injection.java.source.code.*;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  

@Configuration  
public class ApplicationConfig {  

    @Bean  
    public ExaminationService englishExaminationService() {  
        return new EnglishExaminationService();  
    }

}
  • As you can see, we will create the method which return the java class that we want to create the bean for it. Then we will add the annotation @Bean on it and by default the method name will be the bean id.
  • You can view more in this section Spring Bean Configuration With Java Code.

Read Spring Java configuration class#

  • Now, at this step, we just simply read the configuration java class that we created in the step above. So in our main class, we will create Spring Container by creating new AnnotationConfigApplicationContext with the ApplicationConfig class. Then the Spring Context will be available for us.
SpringApplication.java
1
2
//Create a spring container  
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

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:
SpringApplication.class
1
2
3
4
5
//Create a spring container  
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);  

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

Example With Java Source Code#

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>

....

Create a Java class and annotate as @Configuration#

  • Now, let's create the ApplicationConfig class in our project and add the annotation @Configuration as below.
ApplicationConfig.java
1
2
3
4
5
6
7
package com.spring.core.spring.inversion.of.control.java.source.code.config;  

import org.springframework.context.annotation.Configuration;  

@Configuration  
public class ApplicationConfig {  
}

Add component scanning support (Optional)#

  • Next, we just simply add the annotation @ComponentScan and set the basePackages value to the packages that we want the Spring Framework will scan.
ApplicationConfig.java
1
2
3
4
5
6
7
8
9
package com.spring.core.spring.inversion.of.control.java.source.code.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.spring.core.spring.inversion.of.control.java.source.code"})
public class ApplicationConfig {
}

Read Spring Java configuration class#

  • Now, let's use AnnotationConfigApplicationContext for creating the applicationContext which is known as Spring Container as below:
SpringApplication.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.spring.core.spring.inversion.of.control.java.source.code;

import com.spring.core.spring.inversion.of.control.java.source.code.config.ApplicationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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

        //Create a spring container
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

    }
}
  • In which the AnnotationConfigApplicationContext will be created with ApplicationConfig that we created above.

Retrieve Beans From Container#

  • Now, 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
19
20
21
22
package com.spring.core.spring.inversion.of.control.java.source.code;

import com.spring.core.spring.inversion.of.control.java.source.code.config.ApplicationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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

        //Create a spring container
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

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

        //Use bean
        System.out.println(englishCoach.getDailyHomeWork());

        //Close context
        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#