spring scala - sneaking scala into your corporation

Download Spring scala  - Sneaking Scala into your corporation

Post on 01-Dec-2014

664 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • 1. Spring Scala Sneaking Scala into your corporation Brought to you by Henryk Konsek / @hekonsek
  • 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
  • 4. 1. 2. 3. 4. This talk Some context What is Spring Scala How to use Spring Scala Why use Spring Scala
  • 5. 1. Some context Inversion Of Control (IoC) Dependency Injection (DI) Spring
  • 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 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)
  • 9. 3. How to use Spring Scala
  • 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