Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Injecting Map in Spring

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    Here the below code will show you how to inject the Map in Spring using the spring.xml file. By using that we will inject the Map object in spring. -

    Person.java

    package com.evon;
    
    public class Person {
    
        String name;
        String address;
        int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }
    

    Customer.java

    package com.evon;
    import java.util.Map;
    
    public class Customer {
    
    
        private Map<Object, Object> map;
    
    
    
        public Map<Object, Object> getMap() {
            return map;
        }
    
        public void setMap(Map<Object, Object> map) {
            this.map = map;
        }
    
    }
    

    spring.xml

    <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-2.5.xsd">
    
        <bean id="person" class="com.evon.Person">
            <property name="name" value="Manish" />
            <property name="address" value="New Delhi" />
            <property name="age" value="25" />
        </bean>
    
        <bean id="customer" class="com.evon.Customer">
    
            <!-- java.util.Map -->
             <property name="map">
                <map>
                    <entry key="val1" value="1" />
                    <entry key="val2" value-ref="person" />
                </map>
            </property> 
    
    
    
        </bean>
    
    </beans>
    

    App.java

    package com.evon;

    import java.util.Iterator;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "spring.xml");
    
            Customer cust = (Customer) context.getBean("customer");
    
            System.out.println(cust.getMap());
    
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: