CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   JAVA   How to Configure Spring Framework with XML Configurations?

How to Configure Spring Framework with XML Configurations?

September 16, 2022 by SNK

Spring Framework With XML Configuration

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 :-

project_directory_structure

Let’s get started.

What are we going to do ?

  1. Defining a Bean.
  2. Injecting a Bean into constructor (Dependency Injection).
  3. Setter Injection.
  4. Injecting Literal values.
  5. 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.

applicationContext.xml
XHTML
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.

Animal
Java
1
2
3
4
interface Animal {
    public void animalSpeaks();
    public void animalEats();
}

Dog
Java
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

applicationContext.xml
XHTML
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.

Main.java
Java
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.

AnimalService
Java
1
2
3
4
5
package com.learning.spring;
 
public interface AnimalService {
    public void getAnimalType();
}

LandAnimalService
Java
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

applicationContext.xml
XHTML
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?

Java
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.

Java
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

Dog.class
Java
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

applicationContext.xml
XHTML
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

Main.java
Java
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 :-

applicationContext.xml
XHTML
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>

Dog.java
Java
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).

application.properties
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.

applicationContext.xml
XHTML
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.

SHARE ON
Buffer

Enjoyed this article?

Like us on

JAVA spring 5 xml configuration example spring bean xml configuration spring xml tutorial

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.