Using Struts 2 and Spring Frameworks Together

Embed Size (px)

Citation preview

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    1/24

    Using Struts 2 and Spring

    Frameworks TogetherBruce Phillips

    University of Kansas

    March 2009

    Presentation and code examples available at

    http://www.brucephillips.name/spring

    http://www.brucephillips.name/springhttp://www.brucephillips.name/spring
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    2/24

    References

    Spring Framework -http://www.springsource.org/documentation

    Spring Community Forums -http://forum.springsource.org/

    Introduction to the Spring Framework 2.5 -

    http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25

    Spring in Action, 2nd Edition, Manning Publishing,

    August 2007 Pro Spring 2.5, Apress Publishing, August 2008

    Struts 2 Framework - http://struts.apache.org

    2

    http://www.springsource.org/documentationhttp://forum.springsource.org/http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25http://www.amazon.com/Spring-Action-Craig-Walls/dp/1933988134/ref=pd_bbs_1?ie=UTF8&s=books&qid=1235658285&sr=8-1http://www.amazon.com/Pro-Spring-2-5-Jan-Machacek/dp/1590599217/ref=sr_1_1?ie=UTF8&s=books&qid=1235658325&sr=1-1http://struts.apache.org/http://struts.apache.org/http://www.amazon.com/Pro-Spring-2-5-Jan-Machacek/dp/1590599217/ref=sr_1_1?ie=UTF8&s=books&qid=1235658325&sr=1-1http://www.amazon.com/Spring-Action-Craig-Walls/dp/1933988134/ref=pd_bbs_1?ie=UTF8&s=books&qid=1235658285&sr=8-1http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25http://forum.springsource.org/http://www.springsource.org/documentation
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    3/24

    References

    Using Struts 2 and Spring Frameworks Together, BrucePhillips Blog -http://www.brucephillips.name/blog/index.cfm/2008/10/17/Usi

    ng-Struts-2-and-Spring-Frameworks-Together Introduction to the Java Spring Framework,

    http://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Framework

    Struts 2 In Practice, Manning Publishing,http://www.manning.com/wannemacher/

    Apache Struts 2 Documentation, Spring Plugin -http://struts.apache.org/2.x/docs/spring-plugin.html

    Struts User Forum - http://www.nabble.com/Struts---User-f206.html

    Maven - http://maven.apache.org/

    Maven - http://books.sonatype.com/maven-book/index.html

    3

    http://www.brucephillips.name/blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Togetherhttp://www.brucephillips.name/blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Togetherhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.manning.com/wannemacher/http://struts.apache.org/2.x/docs/spring-plugin.htmlhttp://www.nabble.com/Struts---User-f206.htmlhttp://www.nabble.com/Struts---User-f206.htmlhttp://maven.apache.org/http://books.sonatype.com/maven-book/index.htmlhttp://books.sonatype.com/maven-book/index.htmlhttp://maven.apache.org/http://www.nabble.com/Struts---User-f206.htmlhttp://www.nabble.com/Struts---User-f206.htmlhttp://struts.apache.org/2.x/docs/spring-plugin.htmlhttp://www.manning.com/wannemacher/http://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Togetherhttp://www.brucephillips.name/blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Together
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    4/24

    Spring and Struts 2 Frameworks

    Introduction to the Java SpringFramework

    http://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Framework

    Introduction to The Struts 2 Java WebApplication Framework

    http://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Framework

    4

    http://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2008/10/7/Introduction-toThe-Struts-2-Java-Web-Application-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Frameworkhttp://www.brucephillips.name/blog/index.cfm/2009/1/29/Introduction-to-the-Java-Spring-Framework
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    5/24

    Advantages of Using

    Spring and Struts 2 Together Leverage the strengths of both

    frameworks Spring

    Dependency management

    Aspect-Oriented Programming (AOP)

    Spring JDBC

    Struts 2 Simplifies implementing the model, view,

    controller (MVC) pattern in a web application

    5

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    6/24

    Example Applications

    Struts2_Spring_Example

    Simple Struts 2 application that uses Spring tomanage dependencies

    ContactsWebApp

    More complicated Struts 2 application thatuses Spring to manage dependencies,implement cross-cutting (AOP) concerns, and

    interact with a database repository Code and installation instructions at

    http://www.brucephillips.name/spring

    6

    http://www.brucephillips.name/springhttp://www.brucephillips.name/spring
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    7/24

    CreateContact

    ContactService

    CreateEmail

    EmailService

    CreatePhone

    PhoneService

    ContactDataAccess

    Classes Depend On Other ClassesDevelopers must manage these class dependencies (coupling)

    ContactsWebApp

    sampling of class

    dependencies

    Struts ActionSupport Classes

    EmailDataAccessPhoneDataAccess

    Service Classes

    Persistence Classes

    7

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    8/24

    Struts Without Spring

    Struts web applications tend to have quite a fewcontroller (ActionSupport) classes

    These controller classes call upon other classes todo the work

    Without Spring must use concrete objectinstantiation ContactService contactService = new

    ContactServiceImpl();

    The same code will be repeated in all the other StrutsActionSupport classes that use a ContactService object

    Each class is responsible for managing itsdependencies More difficult to configure, maintain, and change

    8

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    9/24

    Struts With Spring

    Use Spring to manage dependencies

    Dependencies are specified and managed inone place

    Much easier to change dependencies

    Most dependencies can be auto-wired

    Use the other benefits Spring provides

    Aspect Oriented Programming (AOP)

    Spring JDBC

    Transaction Management

    9

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    10/24

    Jars Required

    See Struts2_Spring_Example application

    jars are under Referenced Libraries spring.jar is needed to use Spring capabilities

    struts2-spring-plugin-2.1.6.jar is needed so

    that Struts 2 can use Spring

    10

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    11/24

    How To Integrate Spring Into Struts

    Changes to web.xml

    Add ContextLoaderListener andRequestContextListener to web.xml

    Add applicationContext.xml

    Under WEB-INF

    Spring configuration and bean definitions

    11

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    12/24

    Use Spring to Manage

    Dependencies There are two different ways to have

    Spring manage the dependencies in aStruts application

    Configure your actions as Spring beans

    applicationContext.xml

    Configure your actions in struts.xml

    applicationContext_alternate.xml In both cases use Spring to manage

    dependencies between your ActionSupport

    classes and other classes

    12

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    13/24

    Struts2_Spring_Example

    Register class is a Struts ActionSupport class

    Register class depends upon a PersonService

    class

    Option 1 use Spring to create and managethe Register objects

    See applicationContext.xml

    Note the id value for the Register bean

    Register class requires a PersonService object and

    Spring will manage this dependency In struts.xml create the action node but for the class

    value use the id value specified inapplicationContext.xml

    13

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    14/24

    Struts2_Spring_Example

    Option 2 See struts_alternate.xml

    Create action node as usual by specifying the completeclass for the class attribute

    See applicatonContext_alternate.xml Just create those beans for classes that are NOT Struts

    ActionSupport classes

    Spring will manage any dependencies for theActionSupport classes

    Register class depends on PersonService class Before a Register object is provided to the application,

    Spring will pass a PersonService object to classRegisters setPersonService method

    14

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    15/24

    Struts 2 and Spring

    Configuration Options How both option 1 and option 2 manage

    dependencies can be configured to bypassthe defaults

    See chapter 3 Struts 2 In Practice

    See Struts 2 Spring plugin documentation at:

    http://struts.apache.org/2.x/docs/spring-plugin.html

    15

    http://struts.apache.org/2.x/docs/spring-plugin.htmlhttp://struts.apache.org/2.x/docs/spring-plugin.html
  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    16/24

    ContactsWebApp

    Very simple contacts manager

    Web application that uses Struts 2 andSpring

    Database is embedded Derby

    Uses Struts to manage ActionSupportclasses (option 2) and Spring to:

    Manage dependencies

    Implement cross-cutting concerns such as logging,performance timing

    Interact with database repository (Spring JDBC)

    16

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    17/24

    FindContact

    ContactSearchService

    ContactDataAccess

    ContactsWebApp FindContact and its dependencies

    ActionSupport

    Class

    Service

    Layer

    Persistence Layer

    PersonDataAccessEmailDataAccessPhoneDataAccess

    DataSource

    17

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    18/24

    ContactsWebApp struts.xml and

    applicationContext.xml struts.xml

    Action nodes specify complete class value

    For example FindContact

    Note class FindContact needs aContactSearchService object and has a publicsetContactSearchService method

    applicationContext.xml

    Spring configuration Create Spring beans for all non-

    ActionSupport classes that are used by the

    ActionSupport class

    18

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    19/24

    Struts 2 Spring Interaction

    applicationContext.xml Spring will provide any objects needed by

    the ActionSupport classes automatically

    Autowire by name For example FindContacts property

    contactSearchService will get a value because

    Spring will call the setContactSearchServicemethod and pass it a contactSearchService object

    Spring is also managing dependencies for theother classes For example see contactSearchService,

    contactDataAccess and personDataAccess beans

    Be sure to specify scope=request

    19

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    20/24

    Spring AOP

    Can use Spring AOP just as we did in the

    non-Struts 2 Contact application Same configuration in the Struts 2

    projects applicationContext.xml as we

    had in the Contact (non-web) projects

    applicationContext_DB_Repository.xml

    See package edu.ku.si.contact.advice

    20

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    21/24

    AOP Example - Exception Logging

    Need to specify where to apply theadvice

    See ExceptionLogging class PointCutdefinition

    Dont need to apply advice to theActionSupport classes because they wont bethe originator of any Exceptions

    Rename c:/derby/contactsDB toc:/derby/tcontactsDB

    Run ContactsWebApp

    21

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    22/24

    Spring JDBC

    Can use Spring JDBC as we did in theContact (non-Struts 2) application

    See package edu.ku.si.contact.data

    The data access classes extend Spring

    JDBCs SimpleJdbcDaoSupport See PersonDataAccess

    Much simpler to query database and process

    results

    Note Spring JDBC will manage opening andclosing connections, handling exceptions, etc.

    22

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    23/24

    Data Source Management

    The data access classes need a Data Sourceobject

    See applicationContext.xml

    Note no scope is specified for the dataSourcebean

    By extending SimpleJdbcDaoSupport thedata access classes inherit the setDataSource

    method which Spring uses to inject the datasource object

    To use connection pooling see notes in

    applicationContext.xml23

  • 8/8/2019 Using Struts 2 and Spring Frameworks Together

    24/24

    Why Use Spring With Struts 2?

    Significantly reduces coupling between

    classes Central management of dependencies

    Applications are easier to maintain and

    change

    Leverage Spring features that Struts 2

    doesnt provide Aspect-oriented programming

    Spring JDBC24