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

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 332
    Comment on it

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

    Customer.java

    package com.evon;
    import java.util.Properties;
    
    public class Customer {
    
    
        private Properties prop;
    
    
        public Properties getProp() {
            return prop;
        }
    
        public void setProp(Properties prop) {
            this.prop = prop;
        }
    
    }
    

    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="customer" class="com.evon.Customer">
    
            <!-- java.util.Properties -->
             <property name="prop">
                <props>
                    <prop key="email">manish@evontech.com</prop>
                    <prop key="desig">Developer</prop>
                </props>
            </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.getProp());
        }
    }
    

 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: