SlideShare a Scribd company logo
That old Spring magic
has me in its SpEL
Craig Walls
About me




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
About me

• Java, Spring, and OSGi fanatic




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
About me

• Java, Spring, and OSGi fanatic


• Principal consultant with
  Improving Enterprises




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
About me

• Java, Spring, and OSGi fanatic


• Principal consultant with
  Improving Enterprises

• Author




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
About me

• Java, Spring, and OSGi fanatic


• Principal consultant with
  Improving Enterprises

• Author




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda




                                                                                          3
         SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda

• Introducing SpEL




                                                                                               3
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda

• Introducing SpEL


• Using SpEL




                                                                                                3
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda

• Introducing SpEL


• Using SpEL


• SpEL Essentials




                                                                                                3
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda

• Introducing SpEL


• Using SpEL


• SpEL Essentials


• A few SpEL incantations




                                                                                                3
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Agenda

• Introducing SpEL


• Using SpEL


• SpEL Essentials


• A few SpEL incantations


• Q&A


                                                                                                3
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Download the examples




          Approximately the same code...

http://spring.habuma.com/examples/SpEL-examples.zip




                                                                                               4
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Introducing SpEL




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL




                                                                                           6
          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0




                                                                                               6
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
  – Originally conceived in Spring.NET




                                                                                                6
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
  – Originally conceived in Spring.NET

• The Spring Expression Language




                                                                                                6
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
  – Originally conceived in Spring.NET

• The Spring Expression Language
• Much like Unified EL, OGNL, JBoss EL, and others




                                                                                                6
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
   – Originally conceived in Spring.NET

• The Spring Expression Language
• Much like Unified EL, OGNL, JBoss EL, and others

• Will be used across entire Spring portfolio




                                                                                                 6
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
   – Originally conceived in Spring.NET

• The Spring Expression Language
• Much like Unified EL, OGNL, JBoss EL, and others

• Will be used across entire Spring portfolio

• Succinctly express complex concepts




                                                                                                 6
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
   – Originally conceived in Spring.NET

• The Spring Expression Language
• Much like Unified EL, OGNL, JBoss EL, and others

• Will be used across entire Spring portfolio

• Succinctly express complex concepts

• Can be used to wire bean properties



                                                                                                 6
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is SpEL

• New in Spring 3.0
   – Originally conceived in Spring.NET

• The Spring Expression Language
• Much like Unified EL, OGNL, JBoss EL, and others

• Will be used across entire Spring portfolio

• Succinctly express complex concepts

• Can be used to wire bean properties

• Can be used outside of Spring

                                                                                                 6
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Using SpEL




       SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Configuring beans with SpEL (XML)




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Configuring beans with SpEL (XML)




<bean id="appConfigurer"
  class="com.habuma.spel.tests.Witch">
 <property name="name"
   value="#{systemProperties['WITCH_NAME']}" />
</bean>




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Configuring beans with SpEL (@Value)




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Configuring beans with SpEL (@Value)



@Component
public class Wizard {
  @Value("#{systemEnvironment['WIZARD_NAME']}")
  private String name;

    // ...
}




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Programming with SpEL




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Programming with SpEL


ExpressionParser parser = new SpelAntlrExpressionParser();

StandardEvaluationContext context =
  new StandardEvaluationContext(rootObject);

Expression ex =
  parser.parseExpression("witches.^[isWicked()]");

Witch wickedWitch = (Witch) ex.getValue(context);




                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Essential SpEL




         SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                         #{'abracadabra'}




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                          #{'abracadabra'}



        #{42}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                          #{'abracadabra'}



        #{42}                                                 #{3.1415926}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                          #{'abracadabra'}



        #{42}                                                 #{3.1415926}

                                        #{1e4}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                           #{'abracadabra'}



        #{42}                                                  #{3.1415926}

                                         #{1e4}

       #{true}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                           #{'abracadabra'}



        #{42}                                                  #{3.1415926}

                                         #{1e4}

       #{true}                                                                       #{false}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Literal expressions


                           #{'abracadabra'}



        #{42}                                                  #{3.1415926}

                                         #{1e4}

       #{true}                                                                       #{false}


                                         #{null}

             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

    #{systemProperties['WIZARD_NAME']}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

    #{systemProperties['WIZARD_NAME']}
          #{systemProperties.WIZARD_NAME}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

    #{systemProperties['WIZARD_NAME']}
          #{systemProperties.WIZARD_NAME}


   #{systemEnvironment['WIZARD_NAME']}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

    #{systemProperties['WIZARD_NAME']}
          #{systemProperties.WIZARD_NAME}


   #{systemEnvironment['WIZARD_NAME']}
          #{systemEnvironment.WIZARD_NAME}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

    #{systemProperties['WIZARD_NAME']}
          #{systemProperties.WIZARD_NAME}


   #{systemEnvironment['WIZARD_NAME']}
          #{systemEnvironment.WIZARD_NAME}


                    #{wizardBean.name}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables

     #{systemProperties['WIZARD_NAME']}
             #{systemProperties.WIZARD_NAME}


    #{systemEnvironment['WIZARD_NAME']}
            #{systemEnvironment.WIZARD_NAME}


                      #{wizardBean.name}

 Only available when using SpEL in Spring configuration

               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables



        #{request.getParameter('wizardId')}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables



        #{request.getParameter('wizardId')}



         #{session.getAttribute('wizard')}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ready-to-use variables



           #{request.getParameter('wizardId')}



             #{session.getAttribute('wizard')}



Can only be used to configure appropriately-scoped beans




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Constructors




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Constructors



   #{new com.habuma.spel.tests.Wizard('Gandalf')}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Constructors



   #{new com.habuma.spel.tests.Wizard('Gandalf')}




            #{new String('hokus pokus')}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing object members




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing object members


                               #{elf.name}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing object members


                               #{elf.name}


                               #{elf.Name}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing object members


                               #{elf.name}


                               #{elf.Name}


                        #{elf.getName()}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing object members


                               #{elf.name}


                               #{elf.Name}


                        #{elf.getName()}


                 #{elf.name?.length()}



          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Mixing expressions and text




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Mixing expressions and text



                Embeds the wizard’s
                 name in some text
       The wizard’s name is #{wizard.name}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

               #{'Harry' + ' ' + 'Potter'}




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}


            #{77 - 25}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}


            #{77 - 25}                                  #{52 - -25}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}


            #{77 - 25}                                  #{52 - -25}


         #{6 * 7}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}


            #{77 - 25}                                  #{52 - -25}


         #{6 * 7}                                                     #{42 / 6}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Arithmetic operators

                #{'Harry' + ' ' + 'Potter'}


        #{25 + 52}                                          #{2.19 + 2.02}


            #{77 - 25}                                  #{52 - -25}


         #{6 * 7}                                                     #{42 / 6}

                                 #{44 % 7}


            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


     #{42 > 7}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


     #{42 > 7}                                                                           #{7 > 42}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


     #{42 > 7}                                                                           #{7 > 42}


                          #{10000 == 1e4}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


     #{42 > 7}                                                                           #{7 > 42}


                          #{10000 == 1e4}



                     #{‘Apple’ == ‘Apple’}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


      #{42 > 7}                                                                            #{7 > 42}


                            #{10000 == 1e4}



                       #{‘Apple’ == ‘Apple’}


   #{‘Apple’ < ‘Orange’}



               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Relational operators


      #{42 > 7}                                                                            #{7 > 42}


                            #{10000 == 1e4}



                       #{‘Apple’ == ‘Apple’}


   #{‘Apple’ < ‘Orange’}                                           #{‘Orange’ > ‘Apple’}



               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Logical operators




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Logical operators


    #{witch.isWicked() and witch.name == 'Elphaba'}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Logical operators


    #{witch.isWicked() and witch.name == 'Elphaba'}


    #{witch.isWicked() or witch.name == 'Tattypoo'}




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Logical operators


    #{witch.isWicked() and witch.name == 'Elphaba'}


    #{witch.isWicked() or witch.name == 'Tattypoo'}


                        #{!witch.isWicked()}




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ternary and Elvis operator




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ternary and Elvis operator



      #{wizard.isGood() ? 'Gandalf' : 'Saruman'}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ternary and Elvis operator



      #{wizard.isGood() ? 'Gandalf' : 'Saruman'}




            #{wizard.name ?: 'unknown'}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The type operator : T()




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The type operator : T()


     #{T(String)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The type operator : T()


     #{T(String)}


                    #{T(Math)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The type operator : T()


     #{T(String)}


                    #{T(Math)}


                                                     #{T(int)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The type operator : T()


     #{T(String)}


                    #{T(Math)}


                                                     #{T(int)}


                                                                            #{T(java.util.Date)}



              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing class members




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing class members



              #{T(Math).floor(42.56)}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing class members



              #{T(Math).floor(42.56)}



                            #{T(Math).PI}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof

         #{‘Sabrina’ instanceof T(String)}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof

         #{‘Sabrina’ instanceof T(String)}

             #{123 instanceof T(Integer)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof

         #{‘Sabrina’ instanceof T(String)}

             #{123 instanceof T(Integer)}

             #{123L instanceof T(Long)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof

         #{‘Sabrina’ instanceof T(String)}

             #{123 instanceof T(Integer)}

             #{123L instanceof T(Long)}

             #{1.23 instanceof T(Double)}




              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
instanceof

         #{‘Sabrina’ instanceof T(String)}

             #{123 instanceof T(Integer)}

              #{123L instanceof T(Long)}

             #{1.23 instanceof T(Double)}

             #{true instanceof T(Boolean)}



              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Regular expressions




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Regular expressions


    #{phoneNumber matches 'd{3}-d{3}-d{4}'}




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Regular expressions


     #{phoneNumber matches 'd{3}-d{3}-d{4}'}



   #{websiteUrl matches
           'http://www.[a-zA-Z0-9]*.(com|edu|net)'}




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Regular expressions


      #{phoneNumber matches 'd{3}-d{3}-d{4}'}



    #{websiteUrl matches
            'http://www.[a-zA-Z0-9]*.(com|edu|net)'}



 #{customerEmail matches
        '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}'}



                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Setting variables




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Setting variables




Assuming that the root object has a name property...




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Setting variables




Assuming that the root object has a name property...

                       name = 'Broomhilda'




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing collection members




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing collection members


                                       Arrays
                             #{wizards[0]}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Accessing collection members


                                       Arrays
                             #{wizards[0]}


                 Maps and Properties
         #{magicWords['abracadabra']}




          SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection selection




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection selection


      Select all witches that aren’t wicked
                 #{witches.?[!isWicked()]}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection selection


      Select all witches that aren’t wicked
                 #{witches.?[!isWicked()]}

          Select the first wicked witch
                  #{witches.^[isWicked()]}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection selection


      Select all witches that aren’t wicked
                 #{witches.?[!isWicked()]}

          Select the first wicked witch
                  #{witches.^[isWicked()]}

          Select the last wicked witch
                  #{witches.$[isWicked()]}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection projection




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Collection projection




         Get the names of all wizards

                      #{wizards.![name]}




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The #this variable




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The #this variable



      Get the names of all wizards whose
       name is lexically ordered after ‘G’
         #{wizards.![name].?[#this > 'G']}




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Adding a custom function

      Only available with programmatic SpEL




                                                                                             31
            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Adding a custom function

         Only available with programmatic SpEL
context = new StandardEvaluationContext();
parser = new SpelAntlrExpressionParser();
context.registerFunction("inEnglish",
  EnglishNumberInator.class.getDeclaredMethod(
     "translate", new Class[] {int.class}));




                                                                                                31
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Adding a custom function

          Only available with programmatic SpEL
context = new StandardEvaluationContext();
parser = new SpelAntlrExpressionParser();
context.registerFunction("inEnglish",
  EnglishNumberInator.class.getDeclaredMethod(
     "translate", new Class[] {int.class}));


Expression ex = parser.parseExpression("#inEnglish(123)");
String englishNumber = ex.getValue(context);




                                                                                                 31
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Demo




   SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
A few SpEL incantations




    SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring in a system property
(with a default)




               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring in a system property
(with a default)




<bean class="Wizard">
 <property name="name"
           value=
     "#{systemProperties['WIZARD_NAME'] ?: 'Gandalf'}" />
</bean>




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring bean references




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring bean references



@Value("#{pricingService}")
private PricingService pricingService;




                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring bean references



@Value("#{pricingService}")
private PricingService pricingService;
                                                 ...or...

<property name="pricingService"
  value="#{pricingService}" />




                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring bean references



@Value("#{pricingService}")
private PricingService pricingService;
                                                 ...or...

<property name="pricingService"
  value="#{pricingService}" />

                          Don’t do this!


                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Wiring bean references



@Value("#{pricingService}")
private PricingService pricingService;
                                                 ...or...

<property name="pricingService"
  value="#{pricingService}" />

                          Don’t do this!
                           However...

                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective bean wiring




           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective bean wiring




@Value("#{systemProperties['PRICING'] == 'aggressive'"
       + "? aggressivePricing : regularPricing}")
private PricingService pricingService;




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective collection wiring




            SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective collection wiring

public interface TaxRule {
  boolean appliesToState(String state);
  double calculateTax(double base);
}




                    SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective collection wiring

                                                                            <util:list id="taxRules">
public interface TaxRule {                                                   <ref id="revenueTax" />
  boolean appliesToState(String state);                                      <ref id="carpetTax" />
  double calculateTax(double base);                                          <ref id="existenceTax" />
}                                                                            <ref id="justBecauseTax" />
                                                                            </util:list>




                    SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Selective collection wiring

                                                                             <util:list id="taxRules">
public interface TaxRule {                                                    <ref id="revenueTax" />
  boolean appliesToState(String state);                                       <ref id="carpetTax" />
  double calculateTax(double base);                                           <ref id="existenceTax" />
}                                                                             <ref id="justBecauseTax" />
                                                                             </util:list>


<bean id="taxProcessor" class="TaxProcessor"
  scope="session">
 <aop:scoped-proxy />
 <property name="taxRules" value=
    "#{taxRules.?[appliesToState(session.getAttribute('user').state)]}" />
</bean>



                     SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts




                                                                                            38
           SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic




                                                                                               38
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic
• SpEL expressions are just Strings...no compile-
  time help to ensure type-safety, syntax, etc.




                                                                                                38
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic
• SpEL expressions are just Strings...no compile-
  time help to ensure type-safety, syntax, etc.
• Don’t abuse SpEL




                                                                                                38
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic
• SpEL expressions are just Strings...no compile-
  time help to ensure type-safety, syntax, etc.
• Don’t abuse SpEL
  – Remember Goethe’s Sorcerer’s Apprentice




                                                                                                38
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic
• SpEL expressions are just Strings...no compile-
  time help to ensure type-safety, syntax, etc.
• Don’t abuse SpEL
  – Remember Goethe’s Sorcerer’s Apprentice
  – A little bit of magic in the wrong hands can be
    dangerous




                                                                                                38
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Parting thoughts

• SpEL expressions are great for doing Spring
  configuration magic
• SpEL expressions are just Strings...no compile-
  time help to ensure type-safety, syntax, etc.
• Don’t abuse SpEL
   – Remember Goethe’s Sorcerer’s Apprentice
   – A little bit of magic in the wrong hands can be
     dangerous

• Write tests!!!


                                                                                                 38
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Q&A
Thank you!
Don’t forget to turn in your evaluations!!!




             SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.

More Related Content

Viewers also liked

Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
IndicThreads
 
Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1
Sam Brannen
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
Naphachara Rattanawilai
 
Spring Mvc Rest
Spring Mvc RestSpring Mvc Rest
Spring Mvc Rest
Craig Walls
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
zeeshanhanif
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
Sam Brannen
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring Boot
Joshua Long
 
RESTful Web Services with Spring MVC
RESTful Web Services with Spring MVCRESTful Web Services with Spring MVC
RESTful Web Services with Spring MVC
digitalsonic
 
Presentation Spring
Presentation SpringPresentation Spring
Presentation Spring
Nathaniel Richand
 

Viewers also liked (10)

Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Spring Mvc Rest
Spring Mvc RestSpring Mvc Rest
Spring Mvc Rest
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring Boot
 
RESTful Web Services with Spring MVC
RESTful Web Services with Spring MVCRESTful Web Services with Spring MVC
RESTful Web Services with Spring MVC
 
Presentation Spring
Presentation SpringPresentation Spring
Presentation Spring
 

Recently uploaded

Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 

Recently uploaded (20)

Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 

That old Spring magic has me in its SpEL

  • 1. That old Spring magic has me in its SpEL Craig Walls
  • 2. About me SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 3. About me • Java, Spring, and OSGi fanatic SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 4. About me • Java, Spring, and OSGi fanatic • Principal consultant with Improving Enterprises SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 5. About me • Java, Spring, and OSGi fanatic • Principal consultant with Improving Enterprises • Author SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 6. About me • Java, Spring, and OSGi fanatic • Principal consultant with Improving Enterprises • Author SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 7. Agenda 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 8. Agenda • Introducing SpEL 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 9. Agenda • Introducing SpEL • Using SpEL 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 10. Agenda • Introducing SpEL • Using SpEL • SpEL Essentials 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 11. Agenda • Introducing SpEL • Using SpEL • SpEL Essentials • A few SpEL incantations 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 12. Agenda • Introducing SpEL • Using SpEL • SpEL Essentials • A few SpEL incantations • Q&A 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 13. Download the examples Approximately the same code... http://spring.habuma.com/examples/SpEL-examples.zip 4 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 14. Introducing SpEL SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 15. What is SpEL 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 16. What is SpEL • New in Spring 3.0 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 17. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 18. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 19. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language • Much like Unified EL, OGNL, JBoss EL, and others 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 20. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language • Much like Unified EL, OGNL, JBoss EL, and others • Will be used across entire Spring portfolio 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 21. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language • Much like Unified EL, OGNL, JBoss EL, and others • Will be used across entire Spring portfolio • Succinctly express complex concepts 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 22. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language • Much like Unified EL, OGNL, JBoss EL, and others • Will be used across entire Spring portfolio • Succinctly express complex concepts • Can be used to wire bean properties 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 23. What is SpEL • New in Spring 3.0 – Originally conceived in Spring.NET • The Spring Expression Language • Much like Unified EL, OGNL, JBoss EL, and others • Will be used across entire Spring portfolio • Succinctly express complex concepts • Can be used to wire bean properties • Can be used outside of Spring 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 24. Using SpEL SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 25. Configuring beans with SpEL (XML) SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 26. Configuring beans with SpEL (XML) <bean id="appConfigurer" class="com.habuma.spel.tests.Witch"> <property name="name" value="#{systemProperties['WITCH_NAME']}" /> </bean> SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 27. Configuring beans with SpEL (@Value) SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 28. Configuring beans with SpEL (@Value) @Component public class Wizard { @Value("#{systemEnvironment['WIZARD_NAME']}") private String name; // ... } SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 29. Programming with SpEL SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 30. Programming with SpEL ExpressionParser parser = new SpelAntlrExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(rootObject); Expression ex = parser.parseExpression("witches.^[isWicked()]"); Witch wickedWitch = (Witch) ex.getValue(context); SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 31. Essential SpEL SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 32. Literal expressions SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 33. Literal expressions #{'abracadabra'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 34. Literal expressions #{'abracadabra'} #{42} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 35. Literal expressions #{'abracadabra'} #{42} #{3.1415926} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 36. Literal expressions #{'abracadabra'} #{42} #{3.1415926} #{1e4} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 37. Literal expressions #{'abracadabra'} #{42} #{3.1415926} #{1e4} #{true} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 38. Literal expressions #{'abracadabra'} #{42} #{3.1415926} #{1e4} #{true} #{false} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 39. Literal expressions #{'abracadabra'} #{42} #{3.1415926} #{1e4} #{true} #{false} #{null} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 40. Ready-to-use variables SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 41. Ready-to-use variables #{systemProperties['WIZARD_NAME']} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 42. Ready-to-use variables #{systemProperties['WIZARD_NAME']} #{systemProperties.WIZARD_NAME} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 43. Ready-to-use variables #{systemProperties['WIZARD_NAME']} #{systemProperties.WIZARD_NAME} #{systemEnvironment['WIZARD_NAME']} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 44. Ready-to-use variables #{systemProperties['WIZARD_NAME']} #{systemProperties.WIZARD_NAME} #{systemEnvironment['WIZARD_NAME']} #{systemEnvironment.WIZARD_NAME} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 45. Ready-to-use variables #{systemProperties['WIZARD_NAME']} #{systemProperties.WIZARD_NAME} #{systemEnvironment['WIZARD_NAME']} #{systemEnvironment.WIZARD_NAME} #{wizardBean.name} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 46. Ready-to-use variables #{systemProperties['WIZARD_NAME']} #{systemProperties.WIZARD_NAME} #{systemEnvironment['WIZARD_NAME']} #{systemEnvironment.WIZARD_NAME} #{wizardBean.name} Only available when using SpEL in Spring configuration SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 47. Ready-to-use variables SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 48. Ready-to-use variables #{request.getParameter('wizardId')} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 49. Ready-to-use variables #{request.getParameter('wizardId')} #{session.getAttribute('wizard')} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 50. Ready-to-use variables #{request.getParameter('wizardId')} #{session.getAttribute('wizard')} Can only be used to configure appropriately-scoped beans SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 51. Constructors SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 52. Constructors #{new com.habuma.spel.tests.Wizard('Gandalf')} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 53. Constructors #{new com.habuma.spel.tests.Wizard('Gandalf')} #{new String('hokus pokus')} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 54. Accessing object members SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 55. Accessing object members #{elf.name} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 56. Accessing object members #{elf.name} #{elf.Name} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 57. Accessing object members #{elf.name} #{elf.Name} #{elf.getName()} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 58. Accessing object members #{elf.name} #{elf.Name} #{elf.getName()} #{elf.name?.length()} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 59. Mixing expressions and text SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 60. Mixing expressions and text Embeds the wizard’s name in some text The wizard’s name is #{wizard.name} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 61. Arithmetic operators SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 62. Arithmetic operators #{'Harry' + ' ' + 'Potter'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 63. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 64. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 65. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} #{77 - 25} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 66. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} #{77 - 25} #{52 - -25} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 67. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} #{77 - 25} #{52 - -25} #{6 * 7} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 68. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} #{77 - 25} #{52 - -25} #{6 * 7} #{42 / 6} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 69. Arithmetic operators #{'Harry' + ' ' + 'Potter'} #{25 + 52} #{2.19 + 2.02} #{77 - 25} #{52 - -25} #{6 * 7} #{42 / 6} #{44 % 7} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 70. Relational operators SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 71. Relational operators #{42 > 7} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 72. Relational operators #{42 > 7} #{7 > 42} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 73. Relational operators #{42 > 7} #{7 > 42} #{10000 == 1e4} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 74. Relational operators #{42 > 7} #{7 > 42} #{10000 == 1e4} #{‘Apple’ == ‘Apple’} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 75. Relational operators #{42 > 7} #{7 > 42} #{10000 == 1e4} #{‘Apple’ == ‘Apple’} #{‘Apple’ < ‘Orange’} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 76. Relational operators #{42 > 7} #{7 > 42} #{10000 == 1e4} #{‘Apple’ == ‘Apple’} #{‘Apple’ < ‘Orange’} #{‘Orange’ > ‘Apple’} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 77. Logical operators SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 78. Logical operators #{witch.isWicked() and witch.name == 'Elphaba'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 79. Logical operators #{witch.isWicked() and witch.name == 'Elphaba'} #{witch.isWicked() or witch.name == 'Tattypoo'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 80. Logical operators #{witch.isWicked() and witch.name == 'Elphaba'} #{witch.isWicked() or witch.name == 'Tattypoo'} #{!witch.isWicked()} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 81. Ternary and Elvis operator SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 82. Ternary and Elvis operator #{wizard.isGood() ? 'Gandalf' : 'Saruman'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 83. Ternary and Elvis operator #{wizard.isGood() ? 'Gandalf' : 'Saruman'} #{wizard.name ?: 'unknown'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 84. The type operator : T() SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 85. The type operator : T() #{T(String)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 86. The type operator : T() #{T(String)} #{T(Math)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 87. The type operator : T() #{T(String)} #{T(Math)} #{T(int)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 88. The type operator : T() #{T(String)} #{T(Math)} #{T(int)} #{T(java.util.Date)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 89. Accessing class members SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 90. Accessing class members #{T(Math).floor(42.56)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 91. Accessing class members #{T(Math).floor(42.56)} #{T(Math).PI} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 92. instanceof SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 93. instanceof #{‘Sabrina’ instanceof T(String)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 94. instanceof #{‘Sabrina’ instanceof T(String)} #{123 instanceof T(Integer)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 95. instanceof #{‘Sabrina’ instanceof T(String)} #{123 instanceof T(Integer)} #{123L instanceof T(Long)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 96. instanceof #{‘Sabrina’ instanceof T(String)} #{123 instanceof T(Integer)} #{123L instanceof T(Long)} #{1.23 instanceof T(Double)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 97. instanceof #{‘Sabrina’ instanceof T(String)} #{123 instanceof T(Integer)} #{123L instanceof T(Long)} #{1.23 instanceof T(Double)} #{true instanceof T(Boolean)} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 98. Regular expressions SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 99. Regular expressions #{phoneNumber matches 'd{3}-d{3}-d{4}'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 100. Regular expressions #{phoneNumber matches 'd{3}-d{3}-d{4}'} #{websiteUrl matches 'http://www.[a-zA-Z0-9]*.(com|edu|net)'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 101. Regular expressions #{phoneNumber matches 'd{3}-d{3}-d{4}'} #{websiteUrl matches 'http://www.[a-zA-Z0-9]*.(com|edu|net)'} #{customerEmail matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}'} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 102. Setting variables SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 103. Setting variables Assuming that the root object has a name property... SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 104. Setting variables Assuming that the root object has a name property... name = 'Broomhilda' SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 105. Accessing collection members SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 106. Accessing collection members Arrays #{wizards[0]} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 107. Accessing collection members Arrays #{wizards[0]} Maps and Properties #{magicWords['abracadabra']} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 108. Collection selection SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 109. Collection selection Select all witches that aren’t wicked #{witches.?[!isWicked()]} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 110. Collection selection Select all witches that aren’t wicked #{witches.?[!isWicked()]} Select the first wicked witch #{witches.^[isWicked()]} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 111. Collection selection Select all witches that aren’t wicked #{witches.?[!isWicked()]} Select the first wicked witch #{witches.^[isWicked()]} Select the last wicked witch #{witches.$[isWicked()]} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 112. Collection projection SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 113. Collection projection Get the names of all wizards #{wizards.![name]} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 114. The #this variable SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 115. The #this variable Get the names of all wizards whose name is lexically ordered after ‘G’ #{wizards.![name].?[#this > 'G']} SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 116. Adding a custom function Only available with programmatic SpEL 31 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 117. Adding a custom function Only available with programmatic SpEL context = new StandardEvaluationContext(); parser = new SpelAntlrExpressionParser(); context.registerFunction("inEnglish", EnglishNumberInator.class.getDeclaredMethod( "translate", new Class[] {int.class})); 31 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 118. Adding a custom function Only available with programmatic SpEL context = new StandardEvaluationContext(); parser = new SpelAntlrExpressionParser(); context.registerFunction("inEnglish", EnglishNumberInator.class.getDeclaredMethod( "translate", new Class[] {int.class})); Expression ex = parser.parseExpression("#inEnglish(123)"); String englishNumber = ex.getValue(context); 31 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 119. Demo SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 120. A few SpEL incantations SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 121. Wiring in a system property (with a default) SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 122. Wiring in a system property (with a default) <bean class="Wizard"> <property name="name" value= "#{systemProperties['WIZARD_NAME'] ?: 'Gandalf'}" /> </bean> SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 123. Wiring bean references SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 124. Wiring bean references @Value("#{pricingService}") private PricingService pricingService; SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 125. Wiring bean references @Value("#{pricingService}") private PricingService pricingService; ...or... <property name="pricingService" value="#{pricingService}" /> SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 126. Wiring bean references @Value("#{pricingService}") private PricingService pricingService; ...or... <property name="pricingService" value="#{pricingService}" /> Don’t do this! SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 127. Wiring bean references @Value("#{pricingService}") private PricingService pricingService; ...or... <property name="pricingService" value="#{pricingService}" /> Don’t do this! However... SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 128. Selective bean wiring SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 129. Selective bean wiring @Value("#{systemProperties['PRICING'] == 'aggressive'" + "? aggressivePricing : regularPricing}") private PricingService pricingService; SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 130. Selective collection wiring SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 131. Selective collection wiring public interface TaxRule { boolean appliesToState(String state); double calculateTax(double base); } SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 132. Selective collection wiring <util:list id="taxRules"> public interface TaxRule { <ref id="revenueTax" /> boolean appliesToState(String state); <ref id="carpetTax" /> double calculateTax(double base); <ref id="existenceTax" /> } <ref id="justBecauseTax" /> </util:list> SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 133. Selective collection wiring <util:list id="taxRules"> public interface TaxRule { <ref id="revenueTax" /> boolean appliesToState(String state); <ref id="carpetTax" /> double calculateTax(double base); <ref id="existenceTax" /> } <ref id="justBecauseTax" /> </util:list> <bean id="taxProcessor" class="TaxProcessor" scope="session"> <aop:scoped-proxy /> <property name="taxRules" value= "#{taxRules.?[appliesToState(session.getAttribute('user').state)]}" /> </bean> SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 134. Parting thoughts 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 135. Parting thoughts • SpEL expressions are great for doing Spring configuration magic 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 136. Parting thoughts • SpEL expressions are great for doing Spring configuration magic • SpEL expressions are just Strings...no compile- time help to ensure type-safety, syntax, etc. 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 137. Parting thoughts • SpEL expressions are great for doing Spring configuration magic • SpEL expressions are just Strings...no compile- time help to ensure type-safety, syntax, etc. • Don’t abuse SpEL 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 138. Parting thoughts • SpEL expressions are great for doing Spring configuration magic • SpEL expressions are just Strings...no compile- time help to ensure type-safety, syntax, etc. • Don’t abuse SpEL – Remember Goethe’s Sorcerer’s Apprentice 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 139. Parting thoughts • SpEL expressions are great for doing Spring configuration magic • SpEL expressions are just Strings...no compile- time help to ensure type-safety, syntax, etc. • Don’t abuse SpEL – Remember Goethe’s Sorcerer’s Apprentice – A little bit of magic in the wrong hands can be dangerous 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 140. Parting thoughts • SpEL expressions are great for doing Spring configuration magic • SpEL expressions are just Strings...no compile- time help to ensure type-safety, syntax, etc. • Don’t abuse SpEL – Remember Goethe’s Sorcerer’s Apprentice – A little bit of magic in the wrong hands can be dangerous • Write tests!!! 38 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 141. Q&A Thank you! Don’t forget to turn in your evaluations!!! SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.