Spring Constructor Injection Using Map With XML Configurations

Share:
Spring Constructor Injection Using Map With
XML Configurations

Hello Readers, in the previous tutorial we have seen spring dependency injection with constructor using List with dependent object. 

Now in this tutorial we are going to see spring constructor injection using Map<String, String> with String as key and value pair.

Here Address class contains the following attribute fullName, role, board and favoriteThings which is the Map<String, String> with constructors.

Address.java
package com.TheTechMatin.Entity;

import java.util.List;
import java.util.Map;

public class Players {

     private String fullName;
     private String role;
     private String board;
     private Map<String, String> favoriteThings;
     public Players() {

     }

     public Players(String fullName, String role, String board, Map<String, String> favoriteThings) {
          this.fullName = fullName;
          this.role = role;
          this.board = board;
          this.favoriteThings = favoriteThings;
     }

     @Override
     public String toString() {
          return "Players [fullName=" + fullName + ", role=" + role + ", board=" + board + ", map=" + favoriteThings + "]";
     }

     /*it's not good programming practice to have method in entity class*/
     /*it's just for example*/
     public void displayInfo(){
          System.out.println("Full Name : "+fullName+" Role :"+role+" Board :"+board);
          for(Map.Entry<String, String> mapEntry: favoriteThings.entrySet()) {
              System.out.println(mapEntry.getKey()+" : "+mapEntry.getValue());
          }
     }
}

applicationContext.xml
This is the bean configuration file with <bean> element with id attribute which is used to define id of bean followed by class attribute.

The <constructor-arg> which is the sub element of <bean> used to inject values to the beans properties specified for class attribute by its value attribute.

It has also the child element <map> which is used to inject entry (key and value pair) to a matching bean property (In our case private Map<String, String> favoriteThings;).

See below example file.
<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="player" class="com.TheTechMatin.Entity.Players">
<constructor-arg value="Sir Ravindra Jadeja"></constructor-arg>
<constructor-arg value="All Rounder"></constructor-arg>
<constructor-arg value="BCCI"></constructor-arg>
          <!-- Favorite Things -->
<constructor-arg>
 <map>
<entry key="Favorite Actor" value="Vinod Khanna"></entry>
<entry key="Favorite Cricketer" value="Saba Kareem"></entry>
<entry key="Favorite Subject" value="Mathematics"></entry>
 </map>
</constructor-arg>
     </bean>
</beans>

TestApp.java
This file contains main method to perform the following steps :
1.Load the configuration file.
2.Get the bean form context.
3.Call the method.
4.close the context.

package com.TheTechMatin.Entity;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

     public static void main(String[] args) {
         
          //load the configuration file
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         
          //just printing the message
          System.out.println("Hello Welcome To Sping Constructor Injection!");
         
          //get the bean
          Players player = context.getBean(Players.class);
           
          //call method to display info
          player.displayInfo();
         
          //close the context
          context.close();

     }
}

On the execution of TestApp.java class the following output will be displayed on console.

Out/Put :
Hello Welcome To Sping Constructor Injection!
Full Name : Sir Ravindra Jadeja Role :All Rounder Board :BCCI
Favorite Actor : Vinod Khanna
Favorite Cricketer : Saba Kareem
Favorite Subject : Mathematics
Tested On Eclipse 2019-06      

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database