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

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 326
    Comment on it

    Here the below code will show you how to inject the List in Spring using the spring.xml file. By using that we will inject the list 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.List;
    
    public class Customer {
    
        private List<Object> list;
    
    
        public List<Object> getList() {
            return list;
        }
    
        public void setList(List<Object> list) {
            this.list = list;
        }
    }
    

    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.List -->
            <property name="list">
                <list>
                    <value>1</value>
                    <ref bean="person" />
                </list>
            </property>
    
    
    
        </bean>
    
    </beans>
    

    App.java

    package com.evon;
    
    import java.util.Iterator;
    import java.util.List;
    
    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.getList());
    
        }
    }
    

 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: