anarchy88.com

INTRODUCTION TO SPRING

February 16th, 2010 by aldi

Spring is a lightweight framework based on the Dependency Injection (DI) pattern. It is lightweight because Spring uses POJO, which makes it very powerful and very easy to extend or implement.

Spring’s primary use is as a framework for enabling the use of Inversion of Control (IOC). It is also a powerful Java Enterprise application framework that provides a large number of features and services. Spring is initially a bit difficult to understand, but once mastered, you will see a great improvement in your productivity. For enterprise applications, Spring is an excellent tool because it is a complete, wellwritten, well-documented, scalable, and open source—with a lot of community support.

With Spring, you are able to inject all your classes directly into the container using an XML file. This means that the Spring container instantiates all classes and injects into others as defined by XML. You no longer have any dependency lookup problems.

Think of Spring as a container that assembles all your classes together and any part can easily be swapped out. Each part is just a plain Java object.

Without DI, you would have to create a layer that assembles everything, and probably you would also have to change the code for any environment because of the code embedded relationship between the different classes.

public class UserService (){

private UserDao userDao;

public UserService(){

userDao = new UserDao();

}

}

With DI, you solve this problem because you have to create a parameterized version of the same code, allowing it to accept injected classes

public class UserService (){

private UserDao userDao;

public UserService(UserDao userDao){

userDao = userDao;

}

}

But Spring is not just an IOC framework (though everything is based on the IOC framework). Spring provides services and resources for database connection pools, database transaction, security, messaging, caching, and much more. To manage the security of all your application with groups, roles, and permissions, for example, you only have to inject some classes into the Spring security framework.

Spring supports all major data access technologies such as JDBC, Hibernate, JPA, and Ibatis. For example, to query a database you don’t have to create the code to manage the connection, process the result set, and release the connection itself. All these processes are handled by Spring.

The typical architecture of a Spring application comprises a presentation layer that uses a service layer to communicate to the data access layer, which can deal with database, mail servers, or any other data containers, as shown in Figure 1-1.

Untitled5Figure 1-1. Architecture of a Spring application

The presentation layer contains all views and components that are concerned with the presentation of the application to the user. Typically, the view is implemented with JSP or a similar technology; in this book we’ll use Flex.

The service layer represents the business logic of the application. All operations pass through the service layer. You can have different service layers for different services, such as UserServices, MailServices, and so on. In the upcoming chapters, you will learn how to expose the Java Spring service layer to Flex/AS, and to allow the presentation layer to use it.

The data access layer provides all methods to save, send, and retrieve data. Usually the data access layer works with the database, mail servers, .xml, or the file system.

As mentioned previously, Spring is based on a IoC container where all objects(beans) are instantiated. To help you understand this concept better, I’m going to use a basic IOC Flex container I wrote to inject different .xml files to different service layers. My application needed to provide different content to the view, such as video, images, and text, and everything is based on different XML files. At the time, I had to create the application as quickly as possible, and I had other applications to create, so the more code I could reuse and the faster I could deliver the project, the better I would be.

My goal was to create a container where I could inject the .xml files and relate them to the different content factories of the application. I copied precisely the Spring .xml syntax to make it readable by the Spring developer. Below is the application .xml file to show you how I injected the .xml files to the different content factories.

<beans>

<bean claz=”com.publicis.iocframework.core.XMLLoader”????

path=”articlesList.XML” />

XMLLoader is a core class of my IOC framework that loads XML from a given path.

<bean

claz=”com.publicis.articles.business.ArticlesServiceImpl”>

<constructorarg value=”articlesDao” ref=”articlesDao” />

</bean>

ArticlesServiceImpl is the application service layer, and you have to pass into the default

constructor the object type articlesDao.

<bean claz=”com.publicis.articles.dao.XML.ArticleDaoXML”>

<constructorarg value=”XMLArticlesList” />

</bean>

ArticlesDaoXML is the data access layer and you have to pass into the constructor an object XML type.

As you can see, I am able to inject classes and manage their dependencies directly from an XML file without recompiling and changing my code. Moreover, I can reuse the XmlLoader core class to load into the IOC container any XML file and reuse it for a different application. For example, I could pass the XmlArticlesList object to another object simply by adding a new XML tag like this:

<bean claz=”com.publicis.articles.dao.XML.mediaDaoXML”>

<constructorarg value=”XMLArticlesList” />

Dependency injection frameworks exist for a number of platforms and languages, such as AS, Flex, Java, C++, PHP, ColdFusion, Microsoft, .NET, Ruby, Python and Perl. Below is a table with the most popular IOC frameworks for AS, Flex, and Java.

Untitled4

2 Responses

  1. HERBERT


    Medicamentspot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. No prescription drugs. Order drugs online

    Buy:VPXL.Soma.Viagra.Levitra.Viagra Super Active+.Cialis Soft Tabs.Cialis Professional.Propecia.Viagra Soft Tabs.Viagra Professional.Super Active ED Pack.Cialis.Maxaman.Tramadol.Zithromax.Viagra Super Force.Cialis Super Active+….

  2. WAYNE


    CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price drugs. Order pills online

    Buy:Seroquel.Lipothin.Female Pink Viagra.Nymphomax.Advair.SleepWell.Acomplia.Prozac.Lipitor.Zocor.Ventolin.Zetia.Female Cialis.Benicar.Wellbutrin SR.Buspar.Cozaar.Amoxicillin.Aricept.Lasix….

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.