Spring Core - Meetup

batterycopperInternet and Web Development

Nov 12, 2013 (3 years and 10 months ago)

96 views

Spring Talk


Dependency Injection and
IoC



Rohit


Spring Core Introduction


Rohit


Spring Database Connectivity


Lalit

Bhatt


Introduction to Spring Roo


Rohit




Speaker Introduction

Speaker Introduction

Inversion of Control and
Dependency Injection

Example
Scenario

Use case

Expression

Evaluator

(3*4)+sin(
cos
(30))

Input

12.15364048

Output

Use case

Expression

Evaluator

(3*4)+sin(
cos
(30))

Input

12.15364048

Output

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

………………………….

10000 input files

10000 output files

Use case

Problem Statement

-

Create a Test framework to test Expression
Evaluator with 10,000 expressions

What’s wrong with this code?

<R> execute(<D>)

ITestCase

e
xecute()

ITestExecutioner

<D>
getInput
(index);

Int

getCount
();

IInputAdapter

<R>
getExpected
(index);

Int

getCount
();

IExpectedDataAdapter

Init
();

collect();

publish()

IPublisher
<R>

log();

ILogger
<L>

<R> execute(<D>)

ITestCase

e
xecute()

ITestExecutioner

<D>
getInput
(index);

Int

getCount
();

IInputAdapter

<R>
getExpected
(index);

Int

getCount
();

IExpectedDataAdapter

Init
();

collect();

publish()

IPublisher
<R>

log();

ILogger
<L>

If we say new for each
arrow, we are
coupling components!

<R> execute(<D>)

ITestCase

v
oid
setLogger
(..)

v
oid
setTestCase
(..)

v
oid
setInputAdapter
(..)

v
oid
setOutputAdapter
(..)

v
oid
setResultPublisher
(…)

v
oid execute()

TestExecutioner

<D>
getInput
(index);

Int

getCount
();

IInputAdapter

<R>
getExpected
(index);

Int

getCount
();

IExpectedDataAdapter

Init
();

collect();

publish()

IPublisher
<
R>

log();

ILogger
<L>

Lets see this in Code

Spring Core

More Theory of Spring Core


Constructor Dependency Injection


Setter Dependency Injection


Auto Wiring


Bean Scope


Lazy
Init


Initiating Spring Framework

Constructor Injection

<!
--

IInputAdapter
,
IExpectedDataAdapter

Implementation
--
>

<
bean id="
testDataAdapter
"
class="
com.design.di.production.TestDataAdapter
">


<constructor
-
arg

type="
java.lang.String
"
value="
testdata.csv
"/>

<
/bean
>


Setter Injection

<!
--

Executioner Implementation
--
>

<
bean id="executioner"
class="
com.design.di.production.ActualExecutioner
">





<property name="
resultPublisher
">


<ref bean="
csvPublisher
"/>


</property>


….

<
/bean>



Auto Wiring

Requirement
-

All my beans require a logger.


Q


Do I explicitly put logger ref in all beans?


A


No, Use Auto Wiring

Autowire

Types

Type

Meaning

No

No Auto wiring

ByName

Bean

name attribute name matched

ByType

Maps property to Single object of type matched, if multiple objects
found fatal error

Constructor

Same as
ByType

but for

constructor arguments

Autodetect

Either
ByType

or Constructor

through introspection of bean class

Auto Wiring

<
bean id="
csvPublisher
"
class="
com.design.di.production.CSVPublisher
"
autowire
=“
autodetect

>


<property name="
filePrefix
" value="result">


</property>



<property name="logger">


<
ref

bean
="
consoleLogger
"/>


</
property
>


</
bean
>

Bean Scope

Type

Meaning

Singleton

As name suggests Singleton instance, good for stateless bean

Prototype

A new bean for each ref or
getBean
(),

good for
stateful

beans

request

Application for Spring Web apps, Request Scope

session

Application for Spring Web apps, Session

Scope


globalesession

Application for Spring Web apps, Session Scope,

Valid for Port let
Applications


Bean Scope

<
!
--

Logger Implementation
--
>

<
bean id="
consoleLogger
"
class="
com.design.di.production.ConsoleLogger

scope=“prototype”
>


<property name="level">


<value type="
com.design.di.ILogger$Level
">


Info


</
value
>


</
property
>


<
/
bean
>

Lazy
Init

Lazy
Init

<!
-

-

Created at first access
-

-
>

<
bean id="lazy
"

class
=
"
com.foo.ExpensiveToCreateBean”


lazy
-
init
="true"/
>


<!
-

-

Created an at startup
-

-
>

<bean name
="
not.lazy
"
class="
com.foo.AnotherBean
"/>

Initiating Spring Framework

Conclusion

Why use Spring

1.
Modular Application Development


Maximum reuse

2.
Configuration based Application
Development

3.
TDD possible using mock objects

4.
Integration testing by stubbing out
production modules

Introduction to Spring Roo

Scope of this talk


Understanding Rapid App Development


Demo Spring Roo


Excite you guys to take a session on Roo

Rapid Application Development

Configuration
Burden

Commands for
Convention

No Lock In

Extensible

Generated
Code Adapts
to new inputs

Code Demo Time

Typical Application Flow

Define Entities

CRUD DAOs

Web Controller

DAO Unit Test cases

Controller Test Cases

Security

Application Logic

Deploy

Integration Tests

Roo Commands

$>
roo.sh

$
roo
>
project
--
topLevelPackage

com.test.roo

$
roo
>
persistence
setup
--
provider HIBERNATE
--
database
HYPERSONIC_IN_MEMORY

$
roo
>
entity
--
class ~.
model.Employee


testAutomatically

$
roo
>
field
string
--
fieldName

firstName

--
notNull

$
roo
>
field
string
--
fieldName

lastName


notNull

$
roo
>
controller
all
--
package ~.
web

$
roo
>
json

all

$
roo
>
exit

$>
mvn

package
tomcat:run


http://localhost:8080/
roo

Q & A?

Contact us at

rohitsghatol@gmail.com