Spring Framework - Tutorialspoint

2y ago
208 Views
73 Downloads
923.11 KB
17 Pages
Last View : 2m ago
Last Download : 3m ago
Upload by : Maleah Dent
Transcription

Spring FrameworkAbout the TutorialSpring framework is an open source Java platform that provides comprehensiveinfrastructure support for developing robust Java applications very easily and very rapidly.Spring framework was initially written by Rod Johnson and was first released under theApache 2.0 license in June 2003.This tutorial has been written based on Spring Framework version 4.1.6 released in Mar2015.AudienceThis tutorial is designed for Java programmers with a need to understand the Springframework in detail along with its architecture and actual usage. This tutorial will bringyou at an intermediate level of expertise, from where you can take yourself to higher levelsof expertise.PrerequisitesBefore proceeding with this tutorial, you should have a good understanding of Javaprogramming language. A basic understanding of Eclipse IDE is also required because allthe examples have been compiled using Eclipse IDE.Questions and AnswersSpring Questions and Answers has been designed with a special intention of helpingstudents and professionals preparing for various Certification Exams and JobInterviews. This section provides a useful collection of sample Interview Questions andMultiple Choice Questions (MCQs) and their answers with appropriate explanations- Study Spring Questions and AnswersDisclaimer & Copyright Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com.i

Spring FrameworkTable of ContentsAbout the Tutorial . iAudience . iPrerequisites . iQuestions and Answers . iDisclaimer & Copyright . iTable of Contents . ii1.SPRING OVERVIEW . 1Benefits of Using the Spring Framework . 1Dependency Injection (DI) . 2Aspect Oriented Programming (AOP) . 22.SPRING ARCHITECTURE . 3Core Container . 3Data Access/Integration . 4Web . 4Miscellaneous. 43.SPRING – ENVIRONMENT SETUP. 6Step 1 – Set Up Java Development Kit (JDK) . 6Step 2 Install Apache Common Logging API . 6Step 3 – Set Up Eclipse IDE. 7Step 4 – Set Up Spring Framework Libraries . 84.SPRING – HELLO WORLD EXAMPLE . 10Step 1 - Create Java Project . 10Step 2 - Add Required Libraries . 11Step 3 - Create Source Files. 13ii

Spring FrameworkStep 4 - Create Bean Configuration File . 15Step 5 - Running the Program . 165.SPRING IOC CONTAINERS . 17Spring BeanFactory Container . 18Spring ApplicationContext Container . 206.SPRING – BEAN DEFINITION . 24Spring Configuration Metadata . 257.SPRING – BEAN SCOPES . 27The Singleton Scope . 27The Prototype Scope . 308.SPRING – BEAN LIFE CYCLE. 33Initialization Callbacks . 33Destruction Callbacks . 34Default Initialization & Destroy Methods . 379.SPRING – BEAN POST PROCESSORS. 3810.SPRING – BEAN DEFINITION INHERITANCE. 42Bean Definition Template . 4611.SPRING – DEPENDENCY INJECTION . 47Constructor-based Dependency Injection . 48Setter-based Dependency Injection . 5212.SPRING – INJECTING INNER BEANS . 5713.SPRING – INJECTING COLLECTION. 61Injecting Bean References . 65Injecting Null and Empty String Values . 67iii

Spring Framework14.SPRING – BEANS AUTOWIRING . 68Autowiring Modes . 68Autowiring ‘byName’ . 69Autowiring ‘byType’ . 72Autowiring by Constructor . 76Limitations with Autowiring . 7915.SPRING – ANNOTATION-BASED CONFIGURATION. 81@Required Annotation. 82@Autowired Annotation . 85@Qualifier Annotation . 92JSR-250 Annotations . 9616.SPRING – JAVA-BASED CONFIGURATION. 100@Configuration & @Bean Annotations . 100Injecting Bean Dependencies . 103The @Import Annotation . 106Lifecycle Callbacks . 107Specifying Bean Scope . 10817.SPRING – EVENT HANDLING. 109Listening to Context Events . 11018.SPRING CUSTOM EVENTS . 11419.SPRING AOP FRAMEWORK . 118AOP Terminologies . 118Types of Advice . 119Custom Aspects Implementation . 119XML Schema-based AOP with Spring . 119iv

Spring FrameworkXML Schema Based AOP Example . 123@AspectJ-based AOP with Spring . 129@AspectJ-based AOP Example . 13120.SPRING JDBC FRAMEWORK . 137JDBC Template Class . 137Configuring Data Source . 137Data Access Object (DAO) . 138Executing SQL Statements . 138Executing DDL Statements . 140Spring JDBC Framework Examples . 140Spring JDBC Example . 141SQL Stored Procedure in Spring . 14821.SPRING – TRANSACTION MANAGEMENT . 156Local vs. Global Transactions . 156Programmatic vs. Declarative . 157Programmatic Transaction Management. 157Declarative Transaction Management . 165Spring Transaction Abstractions . 17422.SPRING – WEB MVC FRAMEWORK . 179The DispatcherServlet . 179Required Configuration . 180Defining a Controller . 182Creating JSP Views . 183Spring Web MVC Framework Examples . 183Spring MVC Hello World Example . 184Spring MVC Form Handling Example. 187v

Spring FrameworkSpring Page Redirection Example . 194Spring Static Pages Example . 199Spring Exception Handling Example . 20423.SPRING – LOGGING WITH LOG4J. 213Jakarta Commons Logging (JCL) API . 216vi

1. Spring OverviewSpring FrameworkSpring is the most popular application development framework for enterprise Java. Millionsof developers around the world use Spring Framework to create high performing, easilytestable, and reusable code.Spring framework is an open source Java platform. It was initially written by Rod Johnsonand was first released under the Apache 2.0 license in June 2003.Spring is lightweight when it comes to size and transparency. The basic version of Springframework is around 2MB.The core features of the Spring Framework can be used in developing any Java application,but there are extensions for building web applications on top of the Java EE platform.Spring framework targets to make J2EE development easier to use and promotes goodprogramming practices by enabling a POJO-based programming model.Benefits of Using the Spring FrameworkFollowing is the list of few of the great benefits of using Spring Framework: Spring enables developers to develop enterprise-class applications using POJOs.The benefit of using only POJOs is that you do not need an EJB container productsuch as an application server but you have the option of using only a robust servletcontainer such as Tomcat or some commercial product. Spring is organized in a modular fashion. Even though the number of packages andclasses are substantial, you have to worry only about the ones you need and ignorethe rest. Spring does not reinvent the wheel, instead it truly makes use of some of theexisting technologies like several ORM frameworks, logging frameworks, JEE,Quartz and JDK timers, and other view technologies. Testing an application written with Spring is simple because environmentdependent code is moved into this framework. Furthermore, by using JavaBeanstyle POJOs, it becomes easier to use dependency injection for injecting test data. Spring's web framework is a well-designed web MVC framework, which provides agreat alternative to web frameworks such as Struts or other over-engineered orless popular web frameworks. Spring provides a convenient API to translate technology-specific exceptions(thrown by JDBC, Hibernate, or JDO, for example) into consistent, uncheckedexceptions. Lightweight IoC containers tend to be lightweight, especially when compared to EJBcontainers, for example. This is beneficial for developing and deploying applicationson computers with limited memory and CPU resources.1

Spring Framework Spring provides a consistent transaction management interface that can scale downto a local transaction (using a single database, for example) and scale up to globaltransactions (using JTA, for example).Dependency Injection (DI)The technology that Spring is most identified with is the Dependency Injection(DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept,and it can be expressed in many different ways. Dependency Injection is merely oneconcrete example of Inversion of Control.When writing a complex Java application, application classes should be as independent aspossible of other Java classes to increase the possibility to reuse these classes and to testthem independently of other classes while unit testing. Dependency Injection helps ingluing these classes together and at the same time keeping them independent.What is dependency injection exactly? Let's look at these two words separately. Here thedependency part translates into an association between two classes. For example, class Ais dependent of class B.

Spring Framework i About the Tutorial Spring framework is an open source Java platform that provides comprehensive infrastructure support for developing robust Java applications very easily and very rapidly. Spring framework was initially written by Rod Johnson and was

Related Documents:

tutorialspoint.com or google.com these are domain names. A domain name has two parts, TLD (Top Level Domain) and SLD (Second level domain), for example in tutorialspoint.com, tutorialspoint is second level domain of TLD .com, or you can say it's a subdomain of .com TLD. There are many top level domains available, like .com,

1) Explain the term ‘Spring Boot’. It is a Spring module that offers Rapid Application Development to Spring framework. Spring module is used to create an application based on Spring framework which requires to configure few Spring files. 2) Mention some advantages of Spring Boot Here are som

Benefits of the Spring Web MVC Framework The Spring Web MVC Framework is a robust,flexible,and well-designed framework for rapidly developing web applications using the MVC design pattern.The benefits achieved from using this Spring module are similar to those you get from the rest of the Spring Framework.Let's review a few of these.I will .

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

TASC Reading Test Practice Items Read the text. Then answer the questions. Excerpt from Main Street by Sinclair Lewis Main Street is a novel about a girl who grew up in the big city. She has married a physician who moves them to the small town in the Midwest in which he grew up. She is reluctant to move from the city she knows, but goes along with her husband thinking that perhaps she can .