..


Sponsored Links

Core Container - IoC containers

The IoC container is the part that takes care of instantiating Spring and configure the objects that are placed in it, which are called beans.

The beans are configured through metadata that can be XML files or Java annotations . By default, the metadata is read only by XML, to enable the use of annotations is no need to configure the ApplicationContext.

As mentioned previously part of IoC and DI is implemented through the BeanFactory and the ApplicationContext. As the ApplicationContext is a superset of the BeanFactory, it recommended, and from now on we will refer only all'ApplicationContext.
There are various types provided by Spring ApplicationContext, depending on the application must be developed. For example, for stand alone applications and we are ClassPathXmlApplicationContext FileSystemXmlApplicationContext, while for enterprise applications is the WebApplicationContext, which is instantiated through a serlvet listener.
All need the ApplicationContext configuration metadata and then we need to instantiate a first to write an xml file.
Let's take an example, again using as a reference managing a video store and see how to initialize the IoC class VideoManager.
First we create a file using the metadata that we call applicationContext.xml:






 <beans xmlns="http://www.springframework.org/schema/beans" ............>





  



 <! - Imports and other metadata ->



  



 <import resource="otherbeans.xml"/>

	



  



 <! - DEFINITION OF SERVICE ->

						

  



 <bean id="dvdService" class="it.mrwebmaster.DvdServiceImpl" scope="singleton" />





  



 <! - VIDEO MANAGER DEFINITION ->



  



 <bean id="videoManager" class="it.mrwebmaster.VideoManager" scope="prototype">



    



 <property name="dvdService" ref="dvdService" />



  



 </ Bean>



	





 </ Beans>



This example shows that it is possible to integrate multiple metadata files Definition as given in order to divide the beans. Currently it is important to understand the syntax of metadata (which will be explained later), but the potential of Spring.
Eliminate from the class constructor to initialize the VideoManager dvdService and create the getters and setters for the dvdService:






 public VideoManager () {



  



 super ();







 }









 <Dvd> getAvalaibleDvdList public List () {



  



 //......



  



 availabeDvdList return;







 }









 public void setDvdService (DvdService dvdService) {



  



 this.dvdService = dvdService;







 }









 public DvdService getDvdService () {



  



 dvdService return;







 }



Now let's see how to initialize the IoC:





 / **



 



 * Instanz the IoC container



 



 * /







 ApplicationContext ApplicationContext ClassPathXmlApplicationContext = new ("applicationContext.xml");









 / **



 



 * Retrieve the class VideoManager



 



 * /







 VideoManager VideoManager = (VideoManager) applicationContext.getBean ("VideoManager");









 / **



 



 * Print the list of DVDs available



 



 * /







 List <Dvd> avalaibleDvdList videoManager.getAvalaibleDvdList = ();









 for (DVD DVD: avalaibleDvdList) {



  



 System.out.println (dvd.getTitolo ());







 }



As we have seen, writing a few lines of XML code and we were able to fully configure our application.

The advantages of using Spring, which have been described in previous chapters stand out:

  • The beans in our application, and VideoManger DvdServiceImpl Nessus does not implement interface or abstract class, and dependencies dall'IoC containers are void.
  • If you decide to change the implementation dell'DvdService, or add others, just change the file applicationContext.xml, without touching the code of VideoManager, thanks to the use of interfaces.
  • Also thanks to the use of interfaces is easy to create tests for our application.

IoC Container

Again referring to our example we could use the FileSystemXmlApplicationContext this:

 



 ApplicationContext ApplicationContext FileSystemXmlApplicationContext = new ("/ path / applicationContext.xml");

 
Instead to use the WebApplicationContext in an enterprise application, you need to add in our web.xml (the complete example of an enterprise application that uses Spring illutrato will be the last chapter of this guide):





 <! - SPRING ->







 <context-param>



  



 <param-name> contextConfigLocation </ param-name>



  



 <param-value> classpath *: applicationContext.xml </ param-value>







 </ Context-param>









 <listener>



  



 <listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>







 </ Listener>



Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links