Spring Inversion Of Control XML#
Development Process With XML#
-
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)
-
So for configuring Spring Container using XML configuration file, we should follows these step below:
- Configure Spring Beans.
- Create a Spring Container.
- Retrieve beans from Spring Container for using.
Configure The Spring Bean XML#
- The Spring Bean will be configured in an XML configuration file and in this file we will configure the tag bean with
id
andclass
. - Let's see the example below with the XML configuration file
applicationContext.xml
. For the tag bean, theid
is like an alias, andclass
is the fully qualified class name of implementation java class
applicationContext.xml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Create A Spring Container#
Spring Container
is generally known asApplicationContext
and we have some implementation of it as below:-
Specialized implementations of
ApplicationContext
:- ClassPathXmlApplicationContext
- AnnotationConfigApplicationContext
- GenericWebApplicationContext
- other...
-
For
ClassPathXmlApplicationContext
: we can create Spring Container by creating newClassPathXmlApplicationContext
with the name of config file (applicationContext.xml) as below.
1 2 |
|
Retrieve Beans From Container#
- Now, to get the bean from the container we will use the method
getBean(<beanId>, <interface class>)
. In which thebeanId
is the id of bean that we defined inapplicationContext.xml
andinterface class
is the interface of implemented class that we also defined inapplicationContext.xml
.
1 2 3 4 5 |
|
- Why do we specify the interface in getBean()?
- When we pass the interface to the method, behind the scenes
Spring
will cast the object for us. - However, there are some slight differences than normal casting.
- From the Spring docs: Behaves the same as getBean(String), but provides a measure of type safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).
- When we pass the interface to the method, behind the scenes
Example Code With XML#
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 |
|
Configure The Spring Bean#
- Let's create an interface and implementation class as below.
Coach.java | |
---|---|
1 2 3 4 5 6 7 |
|
EnglishCoach.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
- Then in the
resources
let's create anapplicationContext.xml
for configuring Spring beans.
applicationContext.xml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Create A Spring Container#
- Now, let's use
ClassPathXmlApplicationContext
for creating theapplicationContext
which is known asSpring Container
as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- In which the
ClassPathXmlApplicationContext
will be created withapplicationContext.xml
file.
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 23 |
|
Testing#
- Finally, we just simply run the main class then we can see the content in the spring bean as below.
1 2 3 |
|