Today in this short tutorial we are going to learn how to configure spring framework with xml. We will have a neat example of spring 5 xml configuration file which we will be using to configure beans, inject dependency as well as getter setter level dependencies. We will also learn how to inject literal values in spring framework using xml configuration.
Please make sure that you have your spring project ready. If you want to see the project stucture how it looks, check the image below :-
Let’s get started.
What are we going to do ?
- Defining a Bean.
- Injecting a Bean into constructor (Dependency Injection).
- Setter Injection.
- Injecting Literal values.
- Injecting values from Properties File.
1. Defining a bean
To define a bean, first we have to create a java class and then create a new file inside resources folder as applicationContext.xml
.
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> ... </beans> |
This xml file will serve as a configuration for our spring project. Let’s write our implementation first, then we will register the bean in our xml.
1 2 3 4 |
interface Animal { public void animalSpeaks(); public void animalEats(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.learning.spring; public class Dog implements Animal { public Dog() { } @Override public void animalSpeaks() { System.out.println("Dog Barks"); } @Override public void animalEats() { System.out.println("Dog Eats Meat"); } } |
Once you have created the class we need to create a bean in our applicationContext.xml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="theDog" class="com.learning.spring.Dog" /> </beans> |
Finally, we will see how can we use it in our Main.java class.
1 2 3 4 5 6 7 8 9 10 11 |
package com.learning.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal = context.getBean("theDog", Animal.class); animal.animalEats(); } } |
2. Injecting Beans into Constructor [Dependency Injection]
Spring framework has made incredibly easy to inject the dependecy in our class. It does all the heavy lifting for us just by adding simple bit of code inside the applicationContext.xml
First let’s create a service which we will use to inject in our Dog class we created above and access the properties and methods inside it.
1 2 3 4 5 |
package com.learning.spring; public interface AnimalService { public void getAnimalType(); } |
1 2 3 4 5 6 7 8 |
package com.learning.spring; public class LandAnimalService implements AnimalService { @Override public void getAnimalType() { System.out.println("This animal lives on land"); } } |
Now, lets inject the dependency in our bean in our applicationContext.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="landAnimalService" class="com.learning.spring.LandAnimalService" /> <bean id="theDog" class="com.learning.spring.Dog"> <constructor-arg ref="landAnimalService" /> </bean> </beans> |
So, what exactly the spring is doing?
1 2 3 |
LandAnimalService landAnimalService = new LandAnimalService(); Dog thedog = new Dog(landAnimalService); // this part is handled by spring automatically so, that this code never gets repeated accross entire project. |
As you can see writing just this <constructor-arg ref="..."
inside your <bean>
tag will do the heavy lifting by spring and will inject the dependency and can be used anywhere across the entire project.
Now, we can access the methods of the service directly via our bean object.
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal = context.getBean("theDog", Animal.class); animal.getAnimalType(); } } |
3. Setter Injection
Similar to constructors, dependencies can also be injected by using setters. Let’s get started by creating a setter methods in our Dog.class
1 2 3 4 5 6 7 8 9 10 |
public class Dog implements Animal{ private AnimalService animalService; ... public void setAnimalService(AnimalService animalService) { this.animalService = animalService; } } |
Add it in our applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="landAnimalService" class="com.learning.spring.LandAnimalService" /> <bean id="theDog" class="com.learning.spring.Dog"> <property name="animalService" ref="LandAnimalService" /> </bean> </beans> |
Now call the setAnimalService in the Main.class
1 2 3 4 5 6 7 8 |
... public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal = context.getBean("theDog", Animal.class); animal.getAnimalType(); } } |
4. Injecting Literal Values
You can also inject literal values in your applicationContext.xml
file and retrieve it using the setter methods. Take a look below :-
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="theDog" class="com.learning.spring.Dog"> <property name="name" value="Bruno" /> <property name="habitat" ref="land" /> </bean> </beans> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.learning.spring; public class Dog implements Animal { ... public void setName(String name) { ... } public void setHabitat(String habitat) { ... } } |
4. Injecting Values from Properties File
We can also read the values from the file in spring without having to hard code in the applicationContext.xml
file. Let’s say, you need to store some environment specific values in a file and read it in order to use it in your code. You can do this so by creating a new file under /resources
folder names as application.properties
(you can give any name).
1 2 |
animal.name = "Tom" animal.habitat = "Land" |
Now we need to register newly created application.properties file into our applicationContext.xml
by using context-property-placeholder
tag and read it using ${animal.name} / ${animal.habitat}
. Take a look below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:application.properties" /> <bean id="theDog" class="com.learning.spring.Dog"> <property name="name" value="${animal.name}" /> <property name="habitat" ref="${animal.habitat}" /> </bean> </beans> |
So, these are some of the main things that you would be doing while configuring your project with xml configuration in spring.
Leave a Reply