2. About me
open source committer, evangelist and adopter
Apache Camel
Spring Scala (Spring Source aka VMware aka Pivotal)
7 years of professional experience
Artegence aka Filmweb
Asseco
Pitney Bowes
Java Black Belt
freelance consulting
3. About me
Hired by Red Hat (JBoss)
to work with JBoss Fuse stack:
Apache ServiceMix
Apache ActiveMQ
JBoss HornetQ
Apache Camel
Apache CXF
http://jboss.org/jbossfuse
6. Spring guide for busy
developers
Defining dependency to be used in application:
@ of gr to
C ni u ai n
p bi c as Iv ie aC n iu ai n {
u lc l s no c Do of gr t o
@e nI vi ea i vi e a( {
Ba no cD o no cD o)
r tr n wI vi ea ( ;
e u n e n o cD o)
}
@e nI vi ee v c i v ie ev c( {
Ba no cS ri e no cS r ie )
r tr n wI vi ee v c( no cD o) ;
e u n e n o cS ri ei v ie a( )
}
}
7. Spring guide for busy
developers
Using the dependency in the application:
p bi c as Iv ie eo t ee ao {
u lc l s no c Rp rG nr t r
@n et
Ij c
pi ae Iv ie e vc i vi ee vc ;
rv t n oc Sr ie n oc S ri e
pb i Ls <n oc >i v ie Fr ot (n y a, i tm nh {
ul c it Iv i e no cs o Mn hi t er n o t)
I vi eu r qe y= I vi eu re .e rya )m nh m nh ;
n o cQ ey u r
n oc Q ei sy a(er . ot(o t)
r tr i vi ee vc . id no cs qe y ;
e u n n oc S ri ef nI v ie (u r)
}
}
8. 2. What is Spring Scala
official project from Spring Source (aka VMware, aka Pivotal)
portfolio
API for defining beans in Scala
wrappers for existing Spring API (aka Pimp My Spring)
10. Defining beans
c as MS aa of g et ns Fn t oa Cn iu ai n {
l s y cl Cn i xe d u ci nl of g rt o
vl do =b a( ( ya ()
a a
en )M Do )
ba (( ye vc ( a( )
en ) MS ri ed o) )
}
define bean of type MyDao and bean of type MyService
MyDao is wired into MyService
11. Defining beans
c as MS aa of g et ns Fn t oa Cn iu ai n {
l s y cl Cn i xe d u ci nl of g rt o
vl do =b a( {
a a
en )
M Do )
ya(
}
ba ( {
en )
M Sr ie do )
y e vc (a ()
}
}
Notice functions!
12. Defining beans - lifecycle
c a s M s a e r k r o f g r t o e t n s F n t o a C n i u a i n{
l s e sg Bo eC ni u ai n xe d uc in l of gr to
ba (a e= "c i eq rk r) {
en n m
a tv mB o e"
v lb oe = nw Bo e Sr ie
a rk r
e r kr ev c
b oe .d Cn e tr "c :/ o ah s: 11 "
r k ra do nc o( t p/ lc lo t6 6 6)
b oe
rkr
}i i {
nt
_ sa t)
. t r(
}d sr y{
eto
_ so (
. t p)
}
}
We avoid hard-coding method names. We invoke them instead.
13. Accessing beans
v lc ne t= Fn t oa Cn iA p ia in ot x[ y cl Cn i]
a otx
u ci nl o fg pl ct oC n et MS aa o fg
v ls ri e= cn e tM Sr ie
a evc
o tx [y e vc ]
14. (Globally) Accessing beans
Config
o jc M Sa ao fg et n sF nt oa Cn i ua in{
b et y cl Cn i x ed u c in lo fg rt o
vl do =b a( ( ya ()
a a
en )M Do )
vl sr ie =b a (( ye vc ( a( )
a e vc
en )M S ri ed o) )
}
+ application bootstrap
o jc M Ap et ns Ap {
b et y p xe d p
nw Fn to aC n iA pi ai n ot x( .
e u ci nl of gp l ct oC ne t)
r gs eC ni u ai n( yc l Cn i)
e i tr of gr to s MS aa of g
}
= global access
M S a a o f g s r i e ) m S r i e e h d)
y cl C ni .e vc (. y ev cM to(
Or even global access on steroids!
i pr M Sa ao fg _
m ot y cl Cn i .
..
.
s ri e) mS ri e eh d)
e vc ( .y ev cM to (
15. Pimping your existing
contexts
v lx lo tx = nw Ca s ah mA pi ai n ot x( ca s ah ba sx l )
a m C ne t
e l sP tX l pl ct oC ne t "l sp t: en . m"
v ls aa ot x =
a c l Cn et
A pi ai no t xC ne so s tR cA pi ai n ot x( mC ne t
p l ct oC ne to v ri n. oi hp l ct oC ne t xl ot x)
v ls ri e= sa a ot x[ ye v c]
a evc
c lC ne t MS ri e
For those who aren't easily frightened by implicit conversions:
i pr o gs rn fa eo k sa ac ne t A p ia in ot x Cn es os _
m ot r .p ig r mw r. cl . ot x. pl ct o Cn et ov ri n.
v lx lo tx = nw Ca s ah mA pi ai n ot x(ca s ah ba sx l )
a m C ne t
e l sP tX l pl ct oC ne t"l sp t: en . m"
v ls ri e= xl o tx [y ev c ]
a evc
m Cn et M Sr ie
16. Scala friendly API #1
JmsTemplate
v lc ne to Fc o y: Cn et o Fc oy = ..
a o n ci na tr
o nc in at r
.
v lt ml t =n wJ se p ae cn et oF c oy
a e p ae
e mT ml t( o nc in at r)
Function callbacks instead of anonymous inner functions.
t ml t. ed"u u" {s si n Ss in =
e pa e sn(q ee)
e so : es o >
ss in ce tT x Ms ae "e l Wr d )
es o .r ae et es g (H lo o l"
}
Pattern matching friendly return types.
t ml t. ee v( q ee )m th {
e pa e rc ie "u u" ac
cs S m( :T xM sa e = pi tn mg t et
ae o em e t es g) > r nl (. eT x)
cs N n = p it n" o tx m sa er ci e"
ae o e > r n l( N et e sg e e vd)
}
17. Scala friendly API #2
JdbcTemplate
v lt ml t =n wJ bT m lt (a ao re
a e p ae
e dc ep ae d tS uc )
Functions instead of callbacks
v lt ml t. ur A da(S LC l gn FO U E" {
a e p ae qe yn Mp " EE T oi R M SR )
(e ut e, rw u )=
rs l St o Nm >
" d %"fr a (o Nm r sl St gt t ig 1)
%: s.o mt rw u , e u te .e Sr n ()
}
Scala collections instead of java.util API
v lu es :M pS r n, nR f =t ml t. u rF ra (S L C * FO U E"
a sr
a[ ti gA y e]
ep ae qe yo M p" EE T
R M SR )
Options instead of nulls
v lq ey =" EE T lg nF O UE W EE i =?
a ur
SL C oi RM S R H R d
"
v ll gn =t ml t .u rF rb e tS rn ]q ey , 1 m t h{
a oi
ep ae qe y oO jc [t ig ( ur
) ac
Sm (o i)
oe l gn
Nn = "e al L gn
oe > d fu to i"
}
18. Scala friendly API #3
Aspect Oriented Programming
Defining aspects as (anonymous inner) classes is overkill.
v ll ge Av c =
a o g rd ie
(e hd M to ,a g: A ry Ay e] t re :A y =
mt o : e hd rs r a[ n Rf , a g t n) >
l gd bg "x c tn m to { " m to . eN m)
o . eu (E eu ig e hd } , eh dg ta e
v ls vP it u =( :M t o, c Ca s_ )= m gt ae s at Wt (s v"
a a e on ct
m eh d : ls [] > . e Nm .t rs ih " ae )
What about wiring aspects into beans?
a vc t re (e M Sr i e o s vP it u ui g lg eA vc
d ie a gt n w ye vc ) n ae on ct s n og r di e
19. Integrating with non-Scala
Spring
You can mix your existing Spring Config file into functional
configuration.
c as Pr oC ni u ai ne tn sFn to aC ni u ai n{
l s e sn of gr to xe d uc in l of gr to
ip rC as Nm C ni ua in (
mo t ls [a eo fg r to ])
vl jh = ba ( {
a on
en )
n wP ro (e B a( fr ta e) g te n "a ta e )
e es ng te n" i sN m", eB a( ls N m" )
}
}
Mixing XML...
c as Pr oC ni u ai ne tn sFn to aC ni u ai n{
l s e sn of gr to xe d uc in l of gr to
ip rX l" ls p t: cf cn i .m "
mo t m( ca sa h/ x -o fg xl)
ba ( {
en )
C fx oe [e p ee vc ]
x E ps rP ol Sr i e
}
}
20. 4. Why use Spring Scala
Your snippets were nice, but give me a real reason to use this
library.
21. Rant #1
I can use pure Java Config with Scala!
@ en ii Mt o =" hs S rn h sb e r f co e b a a cd n"
B a( n te hd
ti t ig a e n ea tr d y n ci et,
d sr ye hd = " l v t tp m to n ms so om c "+
e t oM to
I oe o y e e hd a e o o uh
" n dt ci gp ol m a rn ie +
a d ee tn rb es t u tm "
" nt a o i v kn m to si f nt os cl b cs +
i s ed f no ig e hd n u ci n a la k"
" n dt ci gp ol m a cm ia in t m"
a d ee tn rb es t o pl to i e)
B oe Sr ie at v mB oe ( {
r kr e vc c ie qr k r)
vl bo e =n wB oe S ri e
a r kr
e rk re vc
bo e. dC ne t r" c: /o a hs :1 1"
rk r ad on co (t p /l cl ot 66 6)
bo e
rk r
}
..
.
j se pa es n( u u, nw Ms a er ao ( {
m Tm l t. ed qe e e e sg Ce tr )
pb i Ms ae ce tM s ae Ss in ss i n t r w J S xe to {
ul c es g r ae es g( e so e so ) ho s ME cp in
r t r s s i n c e t T x M s a e " e b s t F W ";
e u n e so . ra ee te s g( Vr oi y T!)
}
}
)
Yes, you can...
22. Rant #2
Spring sucks! Cake/Dagger/MacWire FTW!
OK if you start greenfield project
Cake alone won't solve your problems
you can use Cake *and* Spring Scala
how likely is that your existing project uses Spring already?
23. Rant #3
Using Spring in Scala is stupid.
Hi Boss, I think that all the Spring trainings,
developers experience, knowHow, patterns,
practices we follow won't be needed anymore!
Scala is the next big thing, so we want to drop
all of these. BTW Will you send my team to
Scala Days? I'm aware that our budget is tight,
but you know, we need to educate...
24. Rant #4
But this is yet another jar in my classpath!
I don't know how to answer this in 2014...
use Maven/Ivy/Gradle/SBT?
don't deploy over Internet and watch YouTube
delete some porns from your HDD
25. Rant #5
Spring Scala is not 100% type safe. Spring is not type safe.
Indeed, but Spring Scala is safer than raw Spring.
26. Evolve, don't be a rebel #1
make yourself a favor, introduce Scala to your legacy project
pick a slice of your existing Spring application
rewrite that slice in Scala
27. Evolve, don't be a rebel #2
make yourself a favor, introduce Scala to your legacy project
need to implement new enhancement request for the
customer?
write it in Scala and integrate with the existing Spring
codebase
28. Evolve, don't be a rebel #3
make yourself a favor, introduce Scala to your legacy project
promote micro-services approach
write your micro-service module in Scala
wire it however you want
use Spring Scala to expose your service to the shared (nonScala) application context
29. Evolve, don't be a rebel #4
respect human factor
not everybody has to be a hacker
some people don't feel comfortable with Monads ;)
some people will learn and adapt slower than the others
create core logic in Scala, let the others to build on it in
Java/Spring
30. Links
Spring Scala project at GitHub
Spring Scala project forum
Introducing Spring Scala by Arjen Poutsma