Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Import statement in spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 283
    Comment on it

    Import:- The import statement can be used to import the another bean configuration file in the existing bean configuration file.

    It is used to import more than one bean configuration file from different sources.

    The following example can be shown below:


    Teacher.java

        package com.chandan;
    
    public class Teacher {
        private String name;
        private int id;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    
    }
    

    app-context1.xml

        <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="teach" class="com.chandan.Teacher">
            <property name="id" value="100001"></property>
            <property name="name" value="Chandan Singh"></property>
        </bean>
    
    
    </beans>
    
    
    app-context.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
    
        <import resource="app-context1.xml" />
    
    </beans>
    

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.chandan.Teacher;
    
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
            Teacher teach=(Teacher)ctx.getBean("teach");
            System.out.println("Id:="+teach.getId());
            System.out.println("Name:="+teach.getName());
    
        }
    
    }
    

    here we can import the app-context1.xml file in app-context.xml and to get the bean information from app-context1.xml file.

 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: