Top Banner
G l a r i m y Training on Spring Framework Krishna Mohan Koyya Technology Consultant & Corporate Trainer [email protected] | www.glarimy.com 091-9731 4231 66 [Visit the portal for latest version of this presentation] © 2009, Glarimy. All rights reserved. http://www.glarimy.com G l a r i m y
39

G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

Oct 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

G l a r i m y Training on

Spring FrameworkKrishna Mohan Koyya

Technology Consultant & Corporate [email protected] | www.glarimy.com

091-9731 4231 66 [Visit the portal for latest version of this presentation]

© 2009, Glarimy. All rights reserved. http://www.glarimy.com

G l a r i m y

Page 2: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Call me Krishna– Originally from Tadepalligudem in

Andhra Pradesh– Have been in Bengaluru for the last

10 years• Held various positions in the IT

Industry– Worked on development of huge

systems • Extensive work in the areas of

– Object Orientation– Distributed Systems– Network Management Systems– Java Technologies

Krishna Mohan Koyya

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 3: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Graduation– B.E. in Electronics & Communication Engineering

• SRKR Engineering College, Bhimavaram, affiliated to Andhra University, Visakhapatnam

• 1989-1993• Post Graduation

– M.Tech in Computer Science & Technology• College of Engineering, Andhra University, Visakhapatnam• 1995-1997

• Career (13 years)– Consultant at Hewlett-Packard – Project Leader at Wipro Technologies– Grade 10 Engineer at Cisco Systems– CEO at Sudhari IT Solutions India Pvt Limited– Associate Professor at Sasi Institute of Tech & Engg– Corporate Trainer & Technology Consultant

Academics & Career

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 4: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

Corporate Trainings

© 2009, Glarimy. All rights reserved.

G l a r i m y

BangaloreChennaiMysore

HyderabadPune

Page 5: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Spring Framework– Spring 2.5.6– Spring WebFlow 2.0.8– Spring Security 2.0.4

• Java, IDE & Build Tools– JDK 1.6– Apache Ant 1.7– Eclipse 3.4

• Webserver– Apache Tomcat 5.0

• Database– MySQL 5

• Messaging System– Apache ActiveMQ

Software Requirements

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 6: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Install Java– Define JAVA_HOME pointing to jdk1.6.0_14– Update PATH to include JAVA_HOME/bin

• Install Ant– Update PATH to include C:\Program Files\apache-ant-1.7.1/bin

• Install Tomcat– Define TOMCAT_HOME pointing to apache-tomcat-6.0.20

• Create a folder ‘projects’• Install Eclipse

– Run eclipse.exe in its bin folder– Choose ‘projects’ as the workspace– Choose J2EE Perspective– Add ‘Apache Tomcat v6.0 Server’ to the server list– Start tomcat server and check homepage at http://localhost:8080

• Install Active MQ– Run activemq.bat under its bin folder– Check homepage at http://localhost:6161

Environment Setup

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 7: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Install MySQL– Set root password to ‘admin’ and eave all

default values in the install wizard– Run ‘net start MySQL’ from command line– Run Start->All Programs->MySQL->MySQL

Server 5.0->MySQL Command Line Client– Run the following SQL queries to create the

database• create database glarimy;• use glarimy;• create table directory (name varchar(32) primary

key, city varchar(32) not null, age city);

Environment Setup

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 8: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Create a folder ‘spring’ and unzip the following there– spring-framework-2.5.6.zip– spring-webflow-2.0.8.RELEASE.zip– spring-security-2.0.4.zip– mysql-connector-java-3.1.13-bin.zip

• Define SPRING_HOME pointing to spring-framework-2.5.6

• Copy the following to TOMCAT_HOME/server/lib– commons-logging.jar– spring.jar– standard.jar– jstl.jar– spring-webmvc.jar– mysql-connector-java-3.1.13-bin

Environment Setup

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 9: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Open Source• Created by Rod Johnson• Framework

– Consists of several modules– For Enterprise Application Development

• Container– For light weight dependency injection– Aspect Oriented

Spring

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 10: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Core– Basis for the spring framework– Provides BeanFactory for Dependency Injection

• Application Context– Extends BeanFactory– The Spring Framework with lifecycle events, validation and integration

• AOP– Basis for developing Aspects

• DAO– JDBC based database connectivity– Supports transaction management and handling of DB exceptions

• ORM– Provides hooks for integrating ORM solutions

• JCA– Connectivity to EIS, Legacy systems and etc.,

Spring Modules

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 11: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• JMX– Exposes application beans as JMX Beans

• JMS– Publishes and consumes JMS messages to/from

Queues and Topics• Remoting

– Exposes Java Beans as Remote Objects• MVC

– Model View Controller implementation for Web Applications

• Portlet MVC– Based on Spring MVC to support portal development

Spring Modules

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 12: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Application in Control– Beans depends on other beans– Beans discover the depending beans– Tight Coupling

• Inversion of Control– Beans depend on other beans– Beans are injected with the depending beans– Loose Coupling

• Injection Methods– Constructor Injection– Property Injection– Interface Injection– Method Injection

Dependency Injection

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 13: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• XML configuration file• XMLBean Factory

– Classpath Resource• Retrieves configuration file from classpath

– FileSystem Resource• Retrieves configuration file from file system

– InputStream Resource• Retrieves configuration file from the stream

– ServletContext Resource• Retrieves configuration file from the servlet context

– UrlResource Resource• Retrieves configuration file from the URL

Bean Factory

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 14: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• At the beginning– Instantiate– Populate Properties– SetBeanName

• Only for BeanNameAware implementations– SetBeanFactory

• Only for BeanFactoryAware implementations– Pre-Initialization BeanPostProcessors– AfterPropertiesSet

• Only for InitializingBean implementations– Custom init method– Post-initialization BeanPostProcessors

• At the end– Destroy

• Only for DisposableBean implementations– Custom destroy method

Bean Lifecycle

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 15: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• XML configuration file• ApplicationContext

– ClassPathXmlApplicationContext• Loads context from the XML file within the

classpath– FileSystemXmlApplicationContext

• Loads context from the XML file within the file system

– XmlWebApplicationContext• Loads context from the XML file within the web

application

Application Context

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 16: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• At the beginning– Instantiate– Populate Properties– SetBeanName

• Only for BeanNameAware implementations– SetBeanFactory

• Only for BeanFactoryAware implementations– SetApplicationContext

• Only for ApplicationContextAware implementations– Pre-Initialization BeanPostProcessors– AfterPropertiesSet

• Only for InitializingBean implementations– Custom init method– Post-initialization BeanPostProcessors

• At the end– Destroy

• Only for DisposableBean implementations– Custom destroy method

Bean Lifecycle

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 17: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Wiring simple values– Integers, float values, strings, boolean values

• Wiring other beans– References to other beans

• Wiring collections– Maps, Sets, Lists and Properties

• Wiring null values• Auto Wiring

– By Name– By Type– By Constructor– By Auto-detection

Bean Wiring

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 18: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Factory Methods• Controlling bean creation

– Configuring init-method• Any method of a bean• Default Init Method

– Applies to all beans– Implementing InitializingBean interface

• Method: afterPropertiesSet• Controlling bean tear-down

– Configuring destroy Method• Any method of a bean• Default Destroy Method

– Applies to all beans– Implementing DisposableBean interface

• Method: Destroy

Bean Configuration

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 19: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Standalone Application– Singleton

• Default• Bean can be created only one per container

– Prototype• Bean can be created any number of times

• Web Applications– Request

• Bean is created for each HTTP Request– Session

• Bean is created only once for each session• Portals

– Global Session• Bean is created only one for the global HTTP Session

Bean Configuration: Scope

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 20: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Parent Bean– Holds common properties and values– Associated with a Class

• Can be created– Can be inherited by any other child beans

• Abstract Parent Bean– Holds common properties and values– Not associated with any class

• Can not be created– Can be inherited by any other child beans

• Child Beans– Can inherit properties from other parent beans– Can override the values of inherited properties

Bean Configuration: Inheritance

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 21: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Complements OOP– Avoids code-clutter– Clear-cut development roles– Easy decoration

• Aspect or Advisor or Interceptor– Deals with cross cutting concerns

• Secondary requirements span across classes– Combination of point-cut and advice

• Advice: Additional code to be applied to the module• Point-cut: Point where cross-cutting concern applies

• Weaving– Applying advices to the targets and creating proxies– Weaving Methods

• Compile Time weaving• Load Time Weaving• Run Time Weaving

Aspect Oriented Programming

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 22: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Run Time Weaving– Supports only method point-cuts– Client interacts with the Target through Proxy ProxyFactoryBean

• ProxyFactoryBean is assigned with an ID (Ex: pid)• The Target is assigned with an ID (Ex: tid)• The ProxyFactoryBean is wired with the Target• Client loads the bean with pid assuming it is the Target

• Advices are Java classes only– No special language or compiler is needed– Choice of implementing various interfaces

• Before - MethodBeforeAdvice• After-Returning – AfterReturningAdvice• Throwing – ThrowsAdvice• Around – MethodInterceptor

• Defining Point-cuts– Regular Expressions– AspectJ Pointcuts

Spring AOP

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 23: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Auto Proxying– Only ApplicationContext supports it – Proxy gets created automatically

• Target is assigned with an ID (Ex: tid)• Clients load the bean with the same ID

• Two Methods of Autoproxying– DefaultAdvisorAutoProxyCreator

• Creates Proxy for the Targets that matches any advisors– BeanNameAutoProxyCreator

• Creates Proxy for the named Targets• POJOs as Aspects

– <aop:aspectj-autoproxy />– Using AspectJ Annotations

• @Aspect and @Pointcut• @Before, @AfterReturning, @AfterThrowing, @Around

– Using XML configuration• <aop:aspect>, <aop:pointcut>• <aop:before>, <aop:after-returning>, <aop:after-throwing>, <aop:around>

Spring AOP

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 24: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Uses JDBC– Supports connection pools– Supports JNDI lookup

• Supports ORM and other API– Supports Hibernate, Toplink, IBatis, JPA and etc

• Supports Transactions– Supports JTA

• Injection of data sources– Loose coupling between application & persistence– Follows DAO pattern

• Templates– Obviates boilerplate code

• DAO Support Classes– Further simplifies the persistence

• Unchecked Exceptions– Meaningful exceptions

• Handful of utlities– RowMapper and ParameterizedRowMapper

Persistence in Spring

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 25: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• CannotAcquireLockException• CannotSerializeTransactionException• CleanupFailureDataAccessException• ConcurrencyFailureException• DataAccessException• DataAccessResourceFailureException• DataIntegrityViolationException• DataRetrievalFailureException• DeadlockLoserDataAccessException• EmptyResultDataAccessException• IncorrectResultSizeDataAccessException• IncorrectUpdateSemanticsDataAccessException• InvalidDataAccessApiUsageException• InvalidDataAccessResourceUsageException• OptimisticLockingFailureException• PermissionDeniedDataAccessException• PessimisticLockingFailureException• TypeMismatchDataAccessException• UncategorizedDataAccessException

Unchecked Exceptions

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 26: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• JDBC Templates– jdbc.core.JdbcTemplate

• JDBC connections– jdbc.core.namedparam.NamedParameterJdbcTemplate

• JDBC connections with support for named parameters– jdbc.core.simple.SimpleJdbcTemplate

• JDBC connections, simplified with Java 5 constructs• ORM and other Templates

– orm.hibernate.HibernateTemplate • Hibernate 2.x sessions

– orm.hibernate3.HibernateTemplate • Hibernate 3.x sessions

– orm.jpa.JpaTemplate • Java Persistence API entity managers

– orm.toplink.TopLinkTemplate • Oracle’s TopLink

– orm.jdo.JdoTemplate • Java Data Object implementations

Templates

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 27: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• JDBC Support Classes– jdbc.core.support.JdbcDaoSupport

• JDBC connections– jdbc.core.namedparam.NamedParameterJdbcDaoSupport

• JDBC connections with support for named parameters– jdbc.core.simple.SimpleJdbcDaoSupport

• JDBC connections, simplified with Java 5 constructs• ORM Support Classes

– jca.cci.support.CciDaoSupport • JCA CCI connections

– orm.hibernate.support.HibernateDao• Support Hibernate 2.x sessions

– orm.hibernate3.support.HibernateDao• Support Hibernate 3.x sessions

– orm.jdo.support.JdoDaoSupport • Java Data Object implementations

– orm.jpa.support.JpaDaoSupport • Java Persistence API entity Managers

DAO Support Classes

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 28: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• For JDBC and iBATIS– jdbc.datasource.DataSourceTransactionManager

• For distributed transactions– transaction.jta.JtaTransactionManager

• For Hibernate – orm.hibernate.HibernateTransactionManager– orm.hibernate3.HibernateTransactionManager

• For JDO for persistence.– orm.jdo.JdoTransactionManager

• For JPA.– orm.jpa.JpaTransactionManager

• For Toplink– orm.toplink.TopLinkTransactionManager

Transaction Managers

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 29: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Full control on transactions• Granular level• Templates

– TransactionTemplate• Execute()

• Callbacks– TransactionCallBack interface

• doInTransaction()– TransactionStatus

Programmatic Transactions

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 30: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Transactions as Aspects• Transaction Annotations• Transaction Attributes

– Propagation– Isolation– Read-Only– TimeOut– RollbackRules

Declarative Transactions

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 31: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Propagation– PROPAGATION_MANDATORY – PROPAGATION_NESTED – PROPAGATION_REQUIRED– PROPAGATION_NEVER– PROPAGATION_NOT_SUPPORTED– PROPAGATION_REQUIRED– PROPAGATION_REQUIRES_NEW– PROPAGATION_SUPPORTS

• Isolation Levels– ISOLATION_DEFAULT – ISOLATION_READ_UNCOMMITTED– ISOLATION_READ_COMMITTED– ISOLATION_REPEATABLE_READ– ISOLATION_SERIALIZABLE

Declarative Transactions

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 32: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• MVC Framework for Web Applications• Dispatcher Servlet at the center

– Front Controller and Dispatcher– Application Context from XML files

• Controllers– Interfaces the webapp with the biz logic

• Views– User Interface Elements

• View Resolvers– Connects Models with the Views

• Model– Data meant for the Views

Spring MVC

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 33: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Browser sends HTTP Request– Web Server checks the URL pattern– Passes it to the Dispatcher Servlet, if matches

• Dispatcher maps the controller– Dispatcher looks into the context file– Identifies and maps to the configured controller– Passes the HTTP Request to the controller

• Controller processes the request– Controller returns ModelAndView

• Dispatcher Resolves the View– Takes help of the ViewResolver– Finds the View

• View Gets Rendered

Spring MVC: Collaboration

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 34: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Mapping Controllers– BeanNameUrlHandlerMapping Maps

• Based on the controller’s bean name– SimpleUrlHandlerMapping

• Based on property collection in the application context.

– ControllerClassNameHandlerMapping• Based on the class name of the Controller

– CommonsPathMapHandlerMapping• Based on the Commons attributes

• Multiple Mapping Strategies

Spring MVC: Collaboration

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 35: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• View– ParameterizableViewController– UrlFilenameViewController

• Simple– Controller– AbstractController

• Throwaway– ThrowawayController

• Multiaction– MultiActionController

• Command – BaseCommandController– AbstractCommandController

• Form– AbstractFormController– SimpleFormController

• Wizard – AbstractWizardFormController

Spring MVC: Controllers

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 36: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Validator Interface– Validate

• Does the validation– Supports

• Checks if it can validate the object of this class

• Validation Framework– Integrates with Commons Validator

• Configuration in the Context File

Spring MVC: Validation

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 37: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• Different View Resolvers– InternalResourceViewResolver– BeanNameViewResolver– ResourceBundleViewResolver– XmlViewResolver

• Multiple View Resolvers in application• Order

Spring MVC: View Resolvers

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 38: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

• This is just a presentation and no way replaces the class room training, discussion and illustrations.

• Best out of this presentation can be obtained by working on the accompanied sample mini projects, simultaneously.

• For any questions, mail me to [email protected]

Final Note about the Preso

© 2009, Glarimy. All rights reserved.

G l a r i m y

Page 39: G l a r i m y Training on Spring Framework · – Basis for the spring framework – Provides BeanFactory for Dependency Injection • Application Context – Extends BeanFactory

© 2008, Glarimy. All rights reserved.

G l a r i m yG l a r i m y

© 2009, Glarimy. All rights reserved.

For Technical [email protected]

FAQ, Tutorials, Presentations, Samples, Quiz

http://www.glarimy.com

For Appointments091-9731 4231 66

Thank Youwww.glarimy.com