Wiley 978-0-470-10161-2 Datasheet

Browse online or download Datasheet for Software manuals Wiley 978-0-470-10161-2. Wiley Beginning Spring Framework 2 User Manual

  • Download
  • Add to my manuals
  • Print
  • Page
    / 28
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 0
1
Jump Start Spring 2
It is always an exciting time when you first start to use a new software framework. Spring 2, indeed,
is an exciting software framework in its own right. However, it is also a fairly large framework. In
order to apply it effectively in your daily work you must first get some fundamental understanding
of the following issues:
Why Spring exists
What problem it is trying to solve
How it works
What new techniques or concepts it embraces
How best to use it
This chapter attempts to get these points covered as quickly as possible and get you using the
Spring 2 framework on some code immediately. The following topics are covered in this chapter:
A brief history of the Spring framework and design rationales
A typical application of the Spring framework
Wiring Java components to create applications using Spring
Understanding Spring’s autowiring capabilities
Understanding the inversion of control and dependency injection
Understanding the available API modules of Spring 2
Having read this chapter, you will be equipped and ready to dive into specific areas of the Spring
framework that later chapters cover.
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 1
COPYRIGHTED MATERIAL
Page view 0
1 2 3 4 5 6 ... 27 28

Summary of Contents

Page 1 - COPYRIGHTED MATERIAL

1Jump Start Spring 2It is always an exciting time when you first start to use a new software framework. Spring 2, indeed,is an exciting software frame

Page 2 - Applying Spring

1. Change the directory to the source directory, and then compile the code using Maven 2:cd src\chapter1\springfirstmvn compileThis may take a little

Page 3

BeanFactory factory = (BeanFactory) context;CalculateSpring calc = (CalculateSpring) factory.getBean(“opsbean”);calc.execute(args);}public void execut

Page 4 - Decoupling at the Interface

Note the XML schema and namespaces used in the <beans> document element. These are standard forSpring 2.0 and the schema defines the tags allowe

Page 5

2. Creates an instance of OpMultiply and names the bean multiply3. Creates an instance of OpAdd and names the bean add4. Creates an instance of Calcul

Page 6

log4j.rootLogger=FATAL, firstlog4j.appender.first=org.apache.log4j.ConsoleAppenderlog4j.appender.first.layout=org.apache.log4j.PatternLayoutlog4j.appe

Page 7

3. Change the directory back to the /springfirst directory in which pom.xml is located.4. Now, run the application using Maven 2 with the following co

Page 8

Understanding Spring’s Inversion of Control (IoC) ContainerThe Spring container is often referred to as an inversion of control (IoC) container. In st

Page 9

2. Uses it to look up a resource bound at jdbc/WroxJDBCDS3. Casts the returned resource to a DataSourceWhen writing your software components to be wir

Page 10 - How It Works

the required resource and then injects it into the component, effectively inverting the control over theselection of the resource from the component t

Page 11

3. Now, modify beans.xml to use the OpMultiply implementation instead: edit the file to matchthe highlighted lines shown here:<?xml version=”1.0” e

Page 12

All About SpringSpring started its life as a body of sample code that Rod Johnson featured in his 2002 Wrox Press bookExpert One on One Java J2EE Desi

Page 13

When the CalculateSpring bean is wired in beans.xml, the setter dependency injection is used towire an implementation of OpMultiply, instead of the fo

Page 14 - Try It Out Autowire by Type

Figure 1-4Adding a Logging AspectIn this section, you can try out Spring AOP support by applying a logging aspect to the existing calcula-tion code.Th

Page 15

This code prints a logging message, similar to the following, whenever a method on the Operationinterface (from the CalculateSpring code) is called:AO

Page 16

public void setOps(Operation ops) {this.ops = ops; }public void setWriter(ResultWriter writer) {wtr = writer;}public static void main(String[] args)

Page 17

<bean id=”screen” class=”com.wrox.begspring.ScreenWriter” /><bean id=”multiply” class=”com.wrox.begspring.OpMultiply” /><bean id=”opsbe

Page 18 - Dependency Injection

The argument to the logMethodExecution() method is a join point. This is a context object that marksthe application point of the aspect. There are sev

Page 19

They decouple dependency specifications from business logic, and separate crosscutting concerns. Allthese things are desirable in software projects of

Page 20

Bearing in mind that Spring was originally invented to create an easier and more lightweight alterna-tive to the legacy J2EE, it is easy to see why su

Page 21 - Adding a Logging Aspect

Inversion of control (IoC) is used throughout Spring applications to decouple dependencies that typicallyblock reuse and increase complexity in J2EE e

Page 22

wired components can be Java objects that you have written for the application, or one of the many pre-fabricated components in the Spring API library

Page 23 - Wiring in AOP Proxies

mathematic operation, and from the writer’s destination, by means of Java interfaces. Two interfaces aredefined. The first, called Operation, encapsul

Page 24

void showResult(String result) ;}One implementation of ResultWriter, called ScreenWriter, writes to the console screen:package com.wrox.begspring;publ

Page 25

private Operation ops = new OpAdd();private ResultWriter wtr = new ScreenWriter();public static void main(String[] args) {CalculateScreen calc = new C

Page 26

Try It Out Creating a Modularized ApplicationYou can obtain the source code for this example from the Wrox download website (wrox.com). You canfind th

Page 27

Figure 1-1In Figure 1-1, there is still one problem: the code of CalculateScreen must be modified and recom-piled if you need to change the math opera

Page 28

Figure 1-2Try It Out Compiling Your First Spring ApplicationThe source code for this Spring-wired application can be found in the src\chapter1\springf

Comments to this Manuals

No comments