kopykitab · an iso 9001:2008 company 113, golden house, daryaganj, new delhi - 110002, india...

16

Upload: others

Post on 31-Jan-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

  • (An Imprint of Laxmi Publications Pvt. Ltd.)An ISO 9001:2008 Company

    BENGALURU CHENNAI COCHIN GUWAHATI HYDERABADJALANDHAR KOLKATA LUCKNOW MUMBAI RANCHI NEW DELHI

    BOSTON (USA) ACCRA (GHANA) NAIROBI (KENYA)

    UNIVERSITY SCIENCE PRESS

  • SPRING FRAMEWORK WITH JAVA—A PRACTICAL APPROACH

    © by Laxmi Publications (P) Ltd. All rights reserved including those of translation into other languages. In accordance with the Copyright (Amendment) Act, 2012, no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise. Any such act or scanning, uploading, and or electronic sharing of any part of this book without the permission of the publisher constitutes unlawful piracy and theft of the copyright holder’s intellectual property. If you would like to use material from the book (other than for review purposes), prior written permission must be obtained from the publishers.

    Printed and bound in India Typeset at Kalyani Computer Services, Delhi

    First Edition : 2016ISBN 978-93-84872-61-8

    Limits of Liability/Disclaimer of Warranty: The publisher and the author make no representation or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties. The advice, strategies, and activities contained herein may not be suitable for every situation. In performing activities adult supervision must be sought. Likewise, common sense and care are essential to the conduct of any and all activities, whether described in this book or otherwise. Neither the publisher nor the author shall be liable or assumes any responsibility for any injuries or damages arising here from. The fact that an organization or Website if referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers must be aware that the Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

    All trademarks, logos or any other mark such as Vibgyor, USP, Amanda, Golden Bells, Firewall Media, Mercury, Trinity, Laxmi appearing in this work are trademarks and intellectual property owned by or licensed to Laxmi Publications, its subsidiaries or affiliates. Notwithstanding this disclaimer, all other names and marks mentioned in this work are the trade names, trademarks or service marks of their respective owners.

    Published in india by

    UNIVERSITY SCIENCE PRESS(An Imprint of Laxmi Publications Pvt. Ltd.)

    An ISO 9001:2008 Company113, GOLDEN HOUSE, DARYAGANJ, NEW DELHI - 110002, INDIA Telephone : 91-11-4353 2500, 4353 2501 Fax : 91-11-2325 2572, 4353 2528 C—www.laxmipublications.com [email protected] Printed at:

    & Bengaluru 080-26 75 69 30

    & Chennai 044-24 34 47 26, 24 35 95 07

    & Cochin 0484-237 70 04, 405 13 03

    & Guwahati 0361-254 36 69, 251 38 81

    & Hyderabad 040-27 55 53 83, 27 55 53 93

    & Jalandhar 0181-222 12 72

    & Kolkata 033-22 27 43 84

    & Lucknow 0522-220 99 16

    & Mumbai 022-24 91 54 15, 24 92 78 69

    & Ranchi 0651-220 44 64

    Bran

    ches

  • (ix)

  • 1

    C H A P T E R

    SPRING CORE CONCEPTS

    1

    The Spring Framework (simply Spring) is a layered open-source application framework

    for Java platform; simplifying Java Enterprise Edition application development by its

    excellent features such as Inversion-of-Control (IoC), Aspect Oriented Programming, and

    Transaction Management.

    1.1 SETTING UP THE ENVIRONMENT

    You can download the latest version of Spring Framework from Spring Source

    Community webpage (http://www.springsource.org/download). The Spring Framework

    version 3.2.1 (spring-framework-3.2.1.RELEASE-dist.zip) is used in the demonstration code

    of this book. And Eclipse IDE (version Juno) with Windows platform is used for code

    development. You can download the latest Eclipse IDE from its official download page

    (http://www.eclipse.org/downloads).

    Create a new Java Project named “SpringDemo” in Eclipse IDE. Choose File →New → Other → Java Project → Type “SpringDemo” as Project Name → Acceptthe defaults → Finish.

    Create a new directory named “lib” under “SpringDemo”. Select “SpringDemo” →Right Click → New → Folder → Type “lib” as Folder name → Finish.

    Include Spring library jar files in CLASSPATH. Copy all the Spring library jars

    (from spring-framework-3.2.1.RELEASE\libs) to this “lib” directory. Select the project-

    name “SpringDemo” → Right click → Properties → Java Build Path (left side) →Libraries Tab → Add JARs → Select All Jar files under the lib directory of ourproject and press OK → OK. Follow the same procedure to include dependent libraryfiles according to your application’s need into CLASSPATH.

    Create a Package named “com.springdemo”. Select “src” directory of the project →Right Click → New → Package → Type “com.springdemo” as package name →Finish.

    Now the IDE is ready and test Spring applications.

  • 2 Spring Framework with Java—A Practical Approach

    1.2 FIRST APPLICATION: DISPLAYING YOUR NAME

    Store the following program under “com.springdemo” package.

    //File Name: Employee.javapackage com.springdemo;

    public class Employee { private String name;

    public void setName(String name) { this.name = name; }

    public String getName() { return name; }}

    Store the following program under “com.springdemo” package.

    //File Name: EmployeeApp.javapackage com.springdemo;import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class EmployeeApp {

    public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Employee employee = (Employee) context.getBean("employee"); System.out.println("My name is" + employee.getName()); }}

    Store the following “beans.xml” file in “src” directory of the project.

  • Spring Core Concepts 3

    Run the EmployeeApp as Java Application. Select “EmployeeApp.java” → RightClick → Run As → Java Application.

    The output is shown below.

    My name is Mohamed Ibrahim

    1.3 INVERSE OF CONTROL (IoC), DEPENDENCY INJECTION

    The Inverse of Control (IoC), also known as Dependency Injection (DI) is a technique

    where objects define their dependencies. An object defines at runtime what the objects

    that it will work with them are. The Spring container injects those dependencies (objects)

    when it creates an object (bean).

    The following example demonstrates how to apply IoC.

    // File Name: DisplayRecord.java

    package com.springdemo;

    public interface DisplayRecord {

    public void display();

    }

    // File Name: Student.java

    package com.springdemo;

    public class Student implements DisplayRecord {

    @Override

    public void display() {

    System.out.println("STUDENT RECORD");

    System.out.println("Roll Number : 1001");

    System.out.println("Name : Mohamed Ibrahim");

    System.out.println("Result : Pass]");

    }

    }

    // File Name: Employee.java

    package com.springdemo;

    public class Employee implements DisplayRecord {

    @Override

  • 4 Spring Framework with Java—A Practical Approach

    public void display() {

    System.out.println("EMPLOYEE RECORD");

    System.out.println("Employee Number : 1001");

    System.out.println("Employee Name : Mohamed Ibrahim");

    System.out.println("Salary : 10,000.00");

    }

    }

    // File Name: AccessRecord.java

    package com.springdemo;

    public class AccessRecord {

    DisplayRecord displayRecord;

    public void setDisplayRecord(DisplayRecord displayRecord) {

    this.displayRecord = displayRecord;

    }

    public void outputDetails() {

    displayRecord.display();

    }

    }

    // File Name: IoCDemo.java

    package com.springdemo;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class IoCDemo {

    public static void main(String args[]) {

    ApplicationContext context =

    new ClassPathXmlApplicationContext("beans.xml");

    AccessRecord accessRecord =

    (AccessRecord) context.getBean("accessRecord");

    accessRecord.outputDetails();

    }

    }

  • Spring Framework With Java-APractical Approach

    Publisher : Laxmi Publications ISBN : 9789384872618Author : B. MohamedIbrahim

    Type the URL : http://www.kopykitab.com/product/11794

    Get this eBook

    40%OFF

    https://www.kopykitab.com/Spring-Framework-With-Java-A-Practical-Approach-by-B-Mohamed-Ibrahim

    Spring Framework With Java-A Practical Approach