Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Use ClassPathXmlApplicationContext

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 576
    Comment on it

    ClassPathXmlApplicationContext :- This can load the beans configuration file and To create and initialize all the beans inside the configuration file.you need not to provide the full path of the XML file but you have to set CLASSPATH properly because it can look bean configuration XML file in CLASSPATH.

    Student.java

    package com.manish;
    
    public class Student 
    {
    
        private String name;
    
    
        public Student()
        {
            System.out.println("Student Bean");
        }
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
    
    }
    

    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/>
    
    
    <bean id="student" class="com.manish.Student">
    
        <property name="name" value="manish"></property>
    
    </bean>
    
    </beans>
    

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.manish.Student;
    
    public class App {
    
    
        public static void main(String[] args) {
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
            Student stu=(Student)ctx.getBean("student");
    
            System.out.println("Name:="+stu.getName());
    
    
    
    
        }
    
    }
    

 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: