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

    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.Set;
    
    public class Customer {
    
    
        private Set<Object> set;
    
    
    
        public Set<Object> getSet() {
            return set;
        }
    
        public void setSet(Set<Object> set) {
            this.set = set;
        }
    
    
    }
    

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

    App.java

    package com.evon;
    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.getSet());
            }
    }
    

 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: