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
    • 356
    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

    1. package com.evon;
    2.  
    3. public class Person {
    4.  
    5. String name;
    6. String address;
    7. int age;
    8.  
    9. public String getName() {
    10. return name;
    11. }
    12.  
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16.  
    17. public String getAddress() {
    18. return address;
    19. }
    20.  
    21. public void setAddress(String address) {
    22. this.address = address;
    23. }
    24.  
    25. public int getAge() {
    26. return age;
    27. }
    28.  
    29. public void setAge(int age) {
    30. this.age = age;
    31. }
    32.  
    33. }

    Customer.java

    1. package com.evon;
    2. import java.util.List;
    3.  
    4. public class Customer {
    5.  
    6. private List<Object> list;
    7.  
    8.  
    9. public List<Object> getList() {
    10. return list;
    11. }
    12.  
    13. public void setList(List<Object> list) {
    14. this.list = list;
    15. }
    16. }

    spring.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans
    4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    5.  
    6.  
    7. <bean id="person" class="com.evon.Person">
    8. <property name="name" value="Manish" />
    9. <property name="address" value="New Delhi" />
    10. <property name="age" value="25" />
    11. </bean>
    12.  
    13. <bean id="customer" class="com.evon.Customer">
    14.  
    15. <!-- java.util.List -->
    16. <property name="list">
    17. <list>
    18. <value>1</value>
    19. <ref bean="person" />
    20. </list>
    21. </property>
    22.  
    23.  
    24.  
    25. </bean>
    26.  
    27. </beans>

    App.java

    1. package com.evon;
    2.  
    3. import java.util.Iterator;
    4. import java.util.List;
    5.  
    6. import org.springframework.context.ApplicationContext;
    7. import org.springframework.context.support.ClassPathXmlApplicationContext;
    8.  
    9. public class App {
    10. public static void main(String[] args) {
    11. ApplicationContext context = new ClassPathXmlApplicationContext(
    12. "spring.xml");
    13.  
    14. Customer cust = (Customer) context.getBean("customer");
    15. System.out.println(cust.getList());
    16.  
    17. }
    18. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: