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
andJava Annotations
we have to maintain the XML Spring configuration file, so our projects will not be a pure Java Code. However, in the third wayJava 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
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 |
|
- 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 inXML configuration file
. - In the
@ComponentScan
, we have to provide the values for the propertybasePackages
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"})
- Ex:
ApplicationConfig.java | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
- In case, we don't want to use the
@ComponentScan
annotation, so we have configure Spring Beans manually in thisApplicationConfig.java
.
ApplicationConfig.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
- 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 theApplicationConfig
class. Then the Spring Context will be available for us.
SpringApplication.java | |
---|---|
1 2 |
|
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 |
|
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 |
|
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 |
|
Add component scanning support (Optional)#
- Next, we just simply add the annotation
@ComponentScan
and set thebasePackages
value to the packages that we want the Spring Framework will scan.
ApplicationConfig.java | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Read Spring Java configuration class#
- Now, let's use
AnnotationConfigApplicationContext
for creating theapplicationContext
which is known asSpring Container
as below:
SpringApplication.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
- In which the
AnnotationConfigApplicationContext
will be created withApplicationConfig
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 |
|
Testing#
- Finally, we just simply run the main class then we can see the content in the spring bean as below.
1 2 3 |
|
See Also#
- Spring Inversion Of Control
- Spring Inversion Of Control XML
- Spring Inversion Of Control Annotation
- Spring Bean Configuration With Java Code