Monday, January 28, 2013

Brief SpEL overview


"The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL butoffers additional features, most notably method invocation and basic string templating functionality."
from Spring Framework Reference Manual 
Using SpEL we can:
  • reference bean properties, and methods;
  • reference types;
  • do math inside expression;
  • comparing values inside expression;
  • use regular expression inside SpEL;
  • access collection's members;
  • perform selection and projection operation on collections
Here are few examples that demonstrates aforecited SpEL properties:

Reference bean properties, and methods

 <bean id="carl"class="com.springinaction.springidol.Instrumentalist">
   <property name="song"value="#{kenny.song}"/>
 </bean>
Here we have a bean carl which has a property named song and the value of this property will be the same as in bean named kenny(actually at the instantiation time of bean carl kenny's bean must exist)
Also we can invoke methods inside expressions:
 <property name="song"value="#{songSelector.selectSong()}"/>
But there is a trick here: what if we want saying to upperCase the name of the song? We could simply invoke
toUppercase()
method on on resulting string object, but what if it return null? There is a solution for such cases: we can use a null-safe accessor. Here is a sample:
 <property name="song"value="#{songSelector.selectSong()?.toUpperCase()}"/>

Reference types

The key to working with class-scoped methods and constants in SpEL is to use the
T()
operator. Here is a sample:
<property name="multiplier"value="#{T(java.lang.Math).PI}"/>

Comparing values inside expressions

Here is an example:
<property name="largeCircle"
    value="#{shape.kind == 'circle' and shape.perimeter gt 10000}"/>
We can and, or not(or !) operators, as well such logical operators are legal: eq, lt, gt, le, ge. Also it is permitted to use java ternary operator.

Regular expressions

When working with text, it’s sometimes useful to check whether that text matches a cer-
tain pattern. SpEL supports pattern matching in expressions with its
matches
operator.
<property name="validEmail" 
   value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>

Selecting and projecting collections' elements

Let’s say that you want to narrow the list of cities down to only those whose population is greater than 100,000. One way to do this is to wire the entire cities bean into a property and place the burden of sifting out the smaller cities on the receiving bean. But with SpEL, it’s a simple matter of using a selection operator (.?[]) when doing the wiring:
<util:list id="cities">
  <bean class="com.habuma.spel.cities.City"
      p:name="Chicago"p:state="IL"p:population="2853114"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="Atlanta"p:state="GA"p:population="537958"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="Dallas"p:state="TX"p:population="1279910"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="Houston"p:state="TX"p:population="2242193"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="Odessa"p:state="TX"p:population="90943"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="ElPaso"p:state="TX"p:population="613190"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="Jal"p:state="NM"p:population="1996"/>
  <bean class="com.habuma.spel.cities.City"
      p:name="LasCruces"p:state="NM"p:population="91865"/>
</util:list>

<property name="bigCities" value="#{cities.?[population gt 100000]}"/>

SpEL also offers two other selection operators, .^[] and .$[], for selecting the first and last matching items  (respectively) from a collection. For example, to select the first big city from cities:
<property name="aBigCity" value="#{cities.^[population gt 100000]}"/>
No ordering is done on the collection prior to selection, so the City representing Chicago would be wired into the aBigCity property. Likewise, the City object representing El Paso could be selected as follows:
<property name="aBigCity"value="#{cities.$[population gt 100000]}"/>

Collection projection involves collecting a particular property from each of the members of a collection into a new collection. SpEL’s projection operator (.![]) can do exactly that. For example, suppose that instead of a list of City objects, what you want is just a list of String objects containing the names of the cities and states:
<property name="cityNames"value="#{cities.![name+','+state]}"/>

Reference system and environments properties

We can also refer to system properties from spring or to those proprties that were setted with -D flag when running java process. Here are example(now I'll show how to set property of a bean directly in java code using
@Value
annotation):
public class PropertyValueTestBean {
  @Value("#{ systemProperties['user.name'] }")
   private String name;
}
Here we set name property to environment specific property user.name which refers to current user of the system.

PS. All examples are taken from "Spring in Action 3rd edition" and from "Spring framework reference manual"

Saturday, January 19, 2013

Eclipse color themes

When someone spends a lot of time writing code in his/her favourite IDE it's  rather important to correctly configure IDE's color theme to reduce eye strain. I've italicized the word 'correctly' because its meaning is a personal matter.
I like dark themes, but among many dark themes on Eclipse Color Theme I did not find any what would I like, so I decided to create own. There is a good eclipse plugin which provides themes from Eclipse Color Themes. Here is its update site.As a base for my theme I have chosen Inkpot.
So here are some examples of my work:
    java code style

    xml code style
    css code style
    After creating my theme I want it to use in any computer that i work on. Here is a solution how to transfer your color settings:

    1. go to your eclipse workspace directory
    2. then copy directory .metadata/.plugins/org.eclipse.core.runtime/.settings to your cloud drive like dropbox, google drive or whatever you use
    That directory contains color settings for all eclipse editors and also color settings of installed plugins.
    You can use standard eclipse functionality to copy preferences(Import/Export Preferences), but there is some pitfall: it doesn't copy color settings, only code formatting, hot keys, compiler settings etc.

    Here you can download my color theme

    Hello Blogger

    This is my first article in Blogger service. 

    All articles in this blog will be written mostly in English, but it's not my native language, so some mistakes may occur. If you notice a mistake in an article, you may point on it in comment (I will grateful for that)