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

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 294
    Comment on it

    Here the below code will show you how to inject the Set in Spring using the spring.xml file. By using that we will inject the Set 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.Set;
    3.  
    4. public class Customer {
    5.  
    6.  
    7. private Set<Object> set;
    8.  
    9.  
    10.  
    11. public Set<Object> getSet() {
    12. return set;
    13. }
    14.  
    15. public void setSet(Set<Object> set) {
    16. this.set = set;
    17. }
    18.  
    19.  
    20. }

    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. <bean id="person" class="com.evon.Person">
    7. <property name="name" value="Manish" />
    8. <property name="address" value="New Delhi" />
    9. <property name="age" value="25" />
    10. </bean>
    11.  
    12. <bean id="customer" class="com.evon.Customer">
    13.  
    14. <!-- java.util.Set -->
    15. <property name="set">
    16. <set>
    17. <value>1</value>
    18. <ref bean="person" />
    19. </set>
    20. </property>
    21.  
    22. </bean>
    23.  
    24. </beans>

    App.java

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

 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: