6248728 spring framework in 15mins

Upload: dramesh

Post on 08-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 6248728 Spring Framework in 15mins

    1/27

    05/24/08 07:05:35

    Article Home> Articles > Java > JSP Spring tutorial This Java Spring Tutorial covers the basics in order to start developing Java Spring examples and applications. Spring can be used with JSPs and plain Java. Java Spring is also used by some companies as an alternative to EJBs. This tutorial will help you learn more about Java Spring.

    1/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    2/27

    05/24/08 07:05:35 What is Spring? Spring is an open source (free) application development framework that makes Java (and primarily J2EE) programming easier. Rod

    Johnson explained his experience and ideas in writing a book (expert one-on-oneJ2EE Design and Development). This detailed issues with current J2EE architectu

    re but also provided examples of how to make flexible solutions. This Spring tutorial is designed for a Java developer to learn how to install, use and integrate Spring into their applications.

    2/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    3/27

    05/24/08 07:05:35 What are the main concepts for Spring? There are two central concepts that summarize what Spring does. One is dependency injection and the other is aspect oriented programming (AOP). For someone learning Spring,these two concepts bring about the most confusion.

    Dependency injection Dependency injection or Inversion of control is based on abest practice technique (design pattern). This technique allows a part of application to be changed without causing problems in other areas of the application.A design is inflexible unless it cannot be easily adapted

    Aspect Oriented Programming (AOP) AOP reduces duplication among classes Interceptors make it easy to add before and after methods in code snippets. Useful for caching,logging and security EHCache,Performance/Tracing interceptors Acegi for security

    3/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    4/27

    05/24/08 07:05:35 Why use Spring? Java has many benefits but it is facing increasing competition from advanced in .NET and PHP. One of the problems with current

    Java and J2EE development is that it requires too much non-business logic related code. Java and J2EE development is also progressing especially with open source development such as free libraries and tools. What this also means is that the knowledge and experience of writing quality applications is also increasing. Maintenance of applications is the largest phase of most projects and typically in J2EE applications there is a lot of code that deals with non-business logic. The Spring framework allows developers to focus on developing the main business logic of an application and be less concerned with remembering if the database connection has been correctly closed. Spring makes it possible to configure and compose complex applications from simpler components. In Spring,application objects are composed declaratively,typically in an XML file. Spring also provides much

    infrastructure functionality (transaction management,persistent framework integration,etc.),leaving the development of application logic to you.

    4/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    5/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    6/27

    05/24/08 07:05:35 As you can see in the figure,all Spring modules are built on top of the core container. The core container defines how your beans are created,configured and managed The core container,where you will find BeanFactory,the heart of any Spring-based application. BeanFactory is an implementation of the Factory pattern that applies IoC to separate your application configuration and dependency specifications from the actual application code Application context module,where supplies many enterprise services such as e-mail,JNDI access,EJB integration,remoting,and scheduling. It also supports for internationalization (I18N)messages,application life cycle events,and validation AOP module,supports for aspect-oriented programming. It enables us to interoperate between Spring and other AOP frameworks. JDBC and DAO module,the layer to manage database accesses

    O/R Mapping module,the layer provides hooks into several popular ORM frameworks,including Hibernate,JDO,iBATIS SQL Maps Web module,builds on the application context module,providing a context that is appropriate for web-base applications. MVC framework,Spring has its own a full-featured Model/View/Controller (MVC) forbuilding web application.

    6/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    7/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    8/27

    05/24/08 07:05:35

    Extract (Unzip) the Zip file into the standard directory. A similar directory structure as the one below will be created.

    8/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    9/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    10/27

    05/24/08 07:05:35 Where is Spring in our applications? The diagram below depictsstructure of Spring layer and the way an application use Spring to invoke otherbusiness services:

    In above,each business service is registered as a bean in the XML beans descriptor file. When Spring layer receives a invocation of a service from the application,it will look up the corresponding bean and dispatch the invocation to the mapped service.

    10/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    11/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    12/27

    05/24/08 07:05:35

    Example for passing parameter value into constructor: Service class: package com.visualbuilder.spring; public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public ExampleBean(AnotherBean b1,YetAnotherBean b2,int i) { this.beanOne = b1; this.beanTwo = b2; this.i = i; } }

    In the XML bean descriptor file: 1

    12/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    13/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    14/27

    05/24/08 07:05:35 Spring and Struts - Hello World example We build a Struts webapplication using the greeting service through Spring like using in Hello worldapplication above

    Design and Code This example is quite simple. It includes only an Action class,GreetingAction,to use Spring application context to look up the greeting serviceand request for service. The diagram below shows the sequential interactions between GreetingAction,Spring application context,and GreetingService

    a Struts plug-in that is aware of the Spring application context. Add the following code to your struts-config.xml to register the plug-in:

    ContextLoaderPlugIn loads a Spring application context (a WebApplicationContext,to be specific),using the context configuration files listed (comma separated)in

    its contextConfigLocation property. The spring-services.xml declares only beanservice with content as in following:

    14/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    15/27

    05/24/08 07:05:35

    When receive request,GreetingAction will use Spring application context to lookup GreetingService,the code below shows that:

    15/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    16/27

    05/24/08 07:05:35

    How to run Steps below describe the ways to deploy and run this web applicationon Tomcat 1. Register GreetingService as a common service on Tomcat by copying greetings.jar file into $CATALINA_HOME/commons/endorsed,figure below is for example:

    16/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    17/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    18/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    19/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    20/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    21/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    22/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    23/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    24/27

    05/24/08 07:05:35

    You can see two extend Spring classes: BookService and CategoryService were declared as beans: bookService and categoryService How to deploy and run Before doing that,must ensure your classpath environment variable point to Spring and Hibernate lib folder. The simplest way is to copy all jar files of Spring and Hibernate lib folders to the lib folder of the web application. The figure below showsthat the web lib folder contains all lib jar files of Spring and Hibernate:

    24/26

  • 8/6/2019 6248728 Spring Framework in 15mins

    25/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    26/27

  • 8/6/2019 6248728 Spring Framework in 15mins

    27/27