Searching...
Friday, 14 June 2013

Spring Application Context implementations

09:47

Spring ApplicatonContext container

This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContextinterface.

The ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory. BeanFactory can still be used for light weight applications like mobile devices or applet based applications.

The most commonly used ApplicationContext implementations are:

FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.

ClassPathXmlApplicationContext This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.

WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.

Example

// Getting beans from XML based bean configuration
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-core.xml");
        // Student bean
        Student obj = (Student) context.getBean("studentBean");
        obj.setName("vinod");
        System.out.println(obj.getName());

       
        ApplicationContext context1 = new FileSystemXmlApplicationContext("/Users/vinodkariyathungalkumaran/git/spring/spring-jmstemplate/src/main/resources/spring-core.xml");
        Student obj1 = (Student) context1.getBean("studentBean");
 

 

0 comments: