You are previewing Learning Spring Application Development.
O'Reilly logo
Learning Spring Application Development

Book Description

Develop dynamic, feature-rich, and robust Spring-based applications using the Spring Framework

In Detail

With this practical guide, you will learn best practices through real-world projects and follow a simple, practical approach to developing high performance and enterprise-grade Java applications with Spring.

Starting with the architecture of the Spring Framework and setting up the key components of the Spring application development environment, you will learn the configuration of Spring Container and how to manage Spring beans using XML and annotations.

Following this, you will explore how to implement the request handling layer using Spring annotated controllers. Other highlights include learning how to build the Java DAO implementation layer by leveraging the Data Access Object design pattern, securing your applications against malicious intruders, and exploring the Spring Mail Application Programming interface to send and receive e-mails.

What You Will Learn

  • Understand the architecture of the Spring Framework and how to set up the key components of the Spring application development environment

  • Configure Spring Container and manage Spring beans using XML and annotations

  • Practice Spring AOP concepts such as aspect, advice, pointcut, and introduction

  • Implement JDBC support and ORM support with Hibernate in the Spring Framework

  • Access data using the DAO design pattern and Spring

  • Leverage the best of Spring Web Controllers and form tag library to create a Spring MVC application

  • Secure your applications against malicious intruders using Spring Security

  • Implement the Spring Mail Application Programming interface to send and receive e-mails

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Learning Spring Application Development
      1. Table of Contents
      2. Learning Spring Application Development
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Introducing the Spring Framework
        1. Introducing Spring
          1. Features of Spring
          2. Other features of Spring
        2. Evolution of the Spring Framework
        3. Spring Framework Architecture
          1. Spring Core Container
          2. The AOP module
          3. Data access/integration
          4. The Web module
          5. The Test module
        4. Benefits of the Spring Framework
        5. Creating an application in Spring
          1. Obtaining Spring JAR files
          2. Understanding Spring packaging
          3. SpringSource Tool Suite
          4. The Spring application
            1. Creating a Spring project
            2. Adding required libraries
            3. Creating source files
            4. Creating the Spring bean configuration file
            5. Running the program
        6. Exercise
        7. Summary
      10. 2. Inversion of Control in Spring
        1. Understanding Inversion of Control
          1. What is a container
          2. Spring Container
            1. Beans
              1. XML-based bean configuration
            2. BeanFactory
              1. What is a factory pattern?
              2. Spring BeanFactory
              3. Implementation of BeanFactory
            3. ApplicationContext
              1. Implementation of ApplicationContext
        2. Dependency Injection
          1. Dependency Injection in Spring
            1. The Has-A relationship
            2. Constructor-based Dependency Injection
              1. The <constructor-arg> element
              2. Constructor Injection – injecting simple Java types
              3. Constructor Injection – resolving ambiguity
                1. No ambiguity
                  1. Solution – use index attribute
                2. Parameter ambiguity
                  1. Solution – use type attribute
            3. The setter-based Dependency Injection
              1. The <property> element
              2. Setter Injection – injecting a simple Java type
              3. Setter Injection – injecting collections
          2. Injecting inner beans
          3. Injecting null and empty string values in Spring
            1. Case 1 – injecting an empty string
            2. Case 2 – injecting a null value
        3. Bean definition inheritance
          1. Inheritance with abstract
        4. Autowiring in Spring
          1. Autowiring modes
            1. Autowiring using the no option
            2. Autowiring using the byname option
              1. Case 1 – if id=" employeeDao"
              2. Case 2 – if id=" employeeDaoBean"
            3. Autowiring using the byType option
            4. Autowiring using the constructor
        5. The bean's scope
          1. Singleton
          2. Prototype
          3. Request
          4. Session
          5. Global session
        6. The Spring bean life cycle
          1. Initialization
          2. Activation
          3. Destruction
          4. Initialization callbacks
            1. Implementing the org.springframework.beans.factory.InitializingBean interface
            2. Using init-method in the XML configuration
          5. Destruction callbacks
            1. Implementing the org.springframework.beans.factory.DisposableBean interface
            2. Using destroy-method in the XML configuration
        7. Exercise
        8. Summary
      11. 3. DAO and JDBC in Spring
        1. Overview of database
        2. The DAO design pattern
          1. The DAO layer
        3. JDBC without Spring
          1. Sample code
            1. ADD drivers specific to database into the project
            2. Directory structure of the application
              1. The Employee.java file
              2. The EmployeeDao.java file
              3. The EmployeeDaoImpl.java file
              4. The HrPayrollSystem.java file
        4. Spring JDBC packages
        5. JDBC with Spring
          1. DataSource
          2. DataSource in the DAO class
            1. Directory structure of the application
              1. The EmployeeDaoImpl.java file
              2. The HrPayrollSystem.java file
        6. What is JdbcTemplate
          1. Configuring the JdbcTemplate object as Spring bean
            1. The Spring.xml file
          2. Functionality exposed by the JdbcTemplate class
            1. Querying (select)
            2. Updating (Insert-Update-Delete)
            3. Other JdbcTemplate operations
          3. Directory structure of the application
            1. The Employee.java file
            2. The EmployeeDao.java file
            3. The EmployeeDaoImpl.java file
        7. JDBC batch operation in Spring
          1. Directory structure of the application
            1. The EmployeeDaoImpl.java file
            2. The HrPayrollBatchUpdate.java file
        8. Calling a stored procedure
          1. Using the SimpleJdbcCall class
            1. Calling a stored procedure
              1. The EmployeeDaoImpl.java file
        9. Exercise
        10. Summary
      12. 4. Hibernate with Spring
        1. Why Object/Relational Mapping?
        2. Introducing ORM, O/RM, and O/R mapping
        3. Introducing Hibernate
          1. Hibernate architecture
            1. Configuration
            2. SessionFactory
            3. Session
            4. Transaction
            5. Query
            6. Criteria
            7. The Persistent object
        4. Integrating Hibernate with the Spring Framework
          1. Sample data model for example code
          2. Integrating Hibernate
          3. Required JARs for the Spring-Hibernate project
          4. Configuring Hibernate SessionFactory in Spring
            1. XML Spring configuration for Hibernate
              1. hibernate.properties
          5. Annotated domain model class
          6. The Hibernate sessions
            1. The Session interface methods
          7. Persistence layer – implement DAOs
            1. The EmployeeDao interface
            2. The EmployeeDaoImpl class
          8. Service layer – implement services
            1. The EmployeeService interface
            2. The EmployeeServiceImpl class
          9. Directory structure of the application
          10. Running the application
            1. The DBUtils class
            2. The SpringHibernateMain class
            3. Output to console
            4. Populated data in the Employee table
        5. Hibernate Query Language
          1. The Query interface
          2. Database operation using HQL
            1. The FROM clause
            2. The AS clause
            3. The SELECT clause
            4. The WHERE clause
            5. The ORDER BY clause
            6. The GROUP BY clause
            7. Using the named parameter
            8. The UPDATE clause
            9. The DELETE clause
            10. Pagination using Query
        6. Hibernate Criteria Query Language
          1. The Criteria interface
            1. Restrictions with Criteria
              1. The eq method
              2. The gt method
              3. The lt method
              4. The like method
              5. The ilike method
              6. The between method
              7. The isNull method
              8. The isNotNull method
              9. The And or OR condition
                1. Restrictions.and
                2. Restrictions.or
              10. Pagination using Criteria
              11. Sorting the results
        7. Exercise
        8. Summary
      13. 5. Spring Web MVC Framework
        1. The MVC architecture and separation of concern
        2. Front Controller Design Pattern
        3. Understanding Spring MVC
          1. Features of the Spring MVC framework
          2. Flow of request handling in Spring MVC
        4. Developing a simple Spring MVC application
          1. Creating a new Maven project
          2. Adding Spring MVC dependencies to pom.xml
          3. Configuring the application
            1. The /WEB-INF/web.xml file
            2. The /WEB-INF/SpringDispatcher-servlet.xml file
          4. Creating the controller – EmployeeController
          5. Creating the view – hello.jsp
          6. Running the application
        5. DispatcherServlet in Spring MVC
          1. DispatcherServlet in deployment descriptor – web.xml
          2. Registering Spring MVC configuration file location
        6. Spring configuration – SpringDispatcher-servlet.xml
        7. Controllers in Spring MVC
          1. The @Controller annotation to define a controller
          2. The @RequestMapping annotation to map requests
            1. Mapping requests at the class level
            2. Mapping requests at the method level
            3. Properties information in @RequestMapping
            4. Method parameters of @RequestMapping
              1. @RequestParam
            5. Return values in @RequestMapping annotated methods
        8. ViewResolver in Spring MVC
          1. Configuring ViewResolver for JSP as view technology
        9. Model in Spring MVC
        10. Spring MVC with Hibernate integration
          1. Application architecture
          2. Sample data model for example code
          3. Project structure
          4. The pom.xml file
            1. The hibernate.properties file
          5. The SpringDispatcher-servlet.xml file
          6. Hibernate model class – entity class
          7. The DAO layer
            1. The EmployeeDao interface
            2. The EmployeeDao implementation
          8. The service layer
            1. The EmployeeService interface
            2. The EmployeeService implementation
          9. Spring MVC controller classes
          10. The View page
            1. The hello.jsp page
            2. The employee.jsp page
            3. The index.jsp page
          11. Running the application
        11. Exception handling using @ControllerAdvice
          1. The GenericException class
          2. The SpringException class
          3. The EmployeeController class
          4. The hello.jsp page
          5. The exception.jsp page
          6. Running the application
        12. Spring MVC internationalization (i18n)
          1. The properties file
          2. Spring configuration
            1. ReloadableResourceBundleMessageSource
            2. LocaleChangeInterceptor
            3. SessionLocaleResolver
          3. The hello.jsp page
          4. Running the application
        13. Handling form with the controller
          1. ModelAndView in Spring MVC
          2. Spring MVC Controller class
            1. @ModelAttribute in the controller class
            2. ModelMap in the controller class
          3. The View page
          4. The Spring MVC form
          5. Running the application
        14. Exercise
        15. Summary
      14. 6. Spring Security
        1. What is Spring Security?
          1. Major operations
        2. Servlet filters review
        3. Security use case
        4. Spring Security configuration
          1. Spring Security setup
            1. Adding JARs to the classpath
            2. Spring Security dependencies – pom.xml
          2. Namespace configuration
        5. Securing web application's URL access
          1. The first step – web.xml
          2. Separating security configurations
        6. Logging into web application
          1. HTTP basic authentication
          2. Form-based login service
          3. Logout service
          4. Anonymous login
          5. Remember Me support
        7. Users authentication
          1. Users authentication with in-memory definitions
          2. Users authentication against database
          3. Encrypting passwords
        8. Method-level security
        9. Let's get down to business
          1. Project structure
          2. Adding filters to web.xml
          3. Resolving your view
          4. Let's add a custom login
          5. Mapping your login requests
          6. Obtaining the employee list
          7. Let's see some credentials
          8. Time to log out
          9. Running the application
        10. Exercise
        11. Summary
      15. 7. Spring Testing
        1. Testing using JUnit 4
          1. JUnit 4 annotations
          2. Assert methods
          3. An example of JUnit 4
        2. Testing using TestNG
          1. TestNG annotations
          2. Example of TestNG
        3. Agile software testing
          1. Unit testing
            1. Unit testing for isolated classes
            2. Unit testing for dependent class using mock objects
            3. The Mockito framework
          2. Integration testing
        4. Create unit tests of the Spring MVC controller
        5. Spring MVC test framework
          1. Required dependencies
          2. Annotations in Spring testing
            1. The @ContextConfiguration annotation
            2. The @WebAppConfiguration annotation
          3. MockMvc
            1. Assertion
          4. @RunWith(SpringJUnit4ClassRunner.class)
        6. Exercise
        7. Summary
      16. 8. Integrating JavaMail and JMS with Spring
        1. E-mail support in Spring
          1. Introducing the JavaMail API
          2. Using the JavaMail API
          3. The Spring API for JavaMail
          4. Developing a Spring Mail Application
            1. Configuration file – Spring.xml
            2. Spring's e-mail sender
            3. The MailerTest class
        2. Spring Java Messaging Service
          1. What is a message and messaging?
          2. What is JMS?
          3. The JMS application
            1. JMS components
          4. MOM Service Provider
            1. Configuring ActiveMQ – message queue
          5. The Spring bean configuration (Spring.xml)
          6. MessageSender.java – Spring JMS Template
          7. App.java
          8. Start ActiveMQ
          9. Output
          10. Monitoring the broker
          11. Exception on running App.java
        3. Exercise
        4. Summary
      17. A. Solutions to Exercises
        1. Chapter 1, Introducing the Spring Framework
        2. Chapter 2, Inversion of Control in Spring
        3. Chapter 3, DAO and JDBC in Spring
        4. Chapter 4, Hibernate with Spring
        5. Chapter 5, Spring Web MVC Framework
        6. Chapter 6, Spring Security
        7. Chapter 7, Spring Testing
        8. Chapter 8, Integrating JavaMail and JMS with Spring
        9. Chapter 9, Inversion of Control in Spring – Using Annotation
        10. Chapter 10, Aspect-oriented Programming with Spring
      18. B. Setting up the Application Database – Apache Derby
      19. Index