Tuesday, November 18, 2014

Spring Context Hierarchy and Spring MVC Flow

What does applicationContext means?

ApplicationContext:
It is the Central Interface that provides configuration for an application.



ApplicationContext Implements the multiple Interfaces as:

1).ListableBeanFactory

2).ResourceLoader : Interface for loading resources , classpath/filesystem resources
ResourceLoaderAware interface need to be implemented by the object that needs to get notified of ResourceLoader

Resource res = applicationContext.getConfigResources(“ http://UrlOfResouce”);

for example,say class:
public class SampleService implements ResourceLoaderAware{

private Resource resourceLoader;

public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public Resource getResource(String location){
return resourceLoader.getResource(location);
}

}


In bean Configuration file:

<beans>
<bean id sampleService class=”com.sample.SampleService”/>
</beans>

In main class the ResourceLoader can be accessed as:
SampleService sample = (SampleService) applicationContext.getBean(“sampleService”);

Resource res = sample.getResource(“http://UrlOfResouce);
/*
Do your work here
*/

ResourceLoaderAware is implemented by the Object and ResourceLoader is DI in the bean using setter Injection.
ResourcePatternResolver , a strategy interface for resolving the location pattern into ResourceObjects

3). MessageSource

Strategy interface for resolving messages, with support for the parameterization and internationalization of such messages.

It has two implementations:
  1. ResourceBundleMessageSource built on top of java.util.ResourceBundle
  2. ReloadableResourceBundleMessageSource, able to reload message definations without JVM restart



MessageSource is being notified to any object if it implements MessageSourceAware inteface and messageSourec can be passed as bean reference since its defined as bean with name messageSource
in applicationContext

public class SampleService implements MessageSourceAware{

private MessageSource messageSource;

private void setMessageSource(MessageSource messageSource){

this.messageSource = messageSource;
}

String nameinEnglish = messgaeSource.getMessage(“text”,Locale.US);

In main Class:
SampleService sample = (SampleService) applicationContext.getBean(“sampleService”);

4).ApplicationEventPublisher:
Serves as super-interface for ApplicationContext and it encapsulates event publication functionality

ApplicationEventPublisherAware is interface to be implemented by the Object that wants the ApplicationEventPublisher to get notified. And ApplicationEvent to be implemented by all ApplicationEvent


ApplicationEvent extends EventObject which is the root class from which all event state objects shall be derived.
Aware is the marker interface .


public MyCustomEventListener implements ApplicationListener{

public void onApplicationEvent(ApplicationEvent event){
}

}

public class MyCustomEvent extends ApplicationEvent{


public MyCustomEvent(Object source){

super(source);
}
}


public class MyCustomEventPublish implements ApplicationEventPublisherAware{

public ApplicationEventPublisher publisher;

public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
                this.publisher = publisher;
        }

        public void publish() {
                this.publisher.publishEvent(new MyCustomEvent(this));
        }



}
Bean Factory:
Bean Factory, as it name Implies,IOC container in spring.
This provides a way to configure a bean dependency and definations.
Bean Factory actually a container which instantiates, configures, and
manages a number of beans.


Spring MVC Flow:

Central Dispatcher for Httprequest controllers which dispatches the request to the handlers for processing a web request , providing convinient mapping







No comments:

Post a Comment