<![CDATA[Round hill of the apples - Groovy, Grails, Griffon and a hint of Java - Blog]]>Sat, 18 May 2013 14:37:25 -0800Weebly<![CDATA[Identified Flying Objects - no worries, I got it sorted]]>Wed, 04 Apr 2012 17:49:43 GMThttp://roundhilloftheapples.weebly.com/1/post/2012/04/identified-flying-objects-no-worries-i-got-it-sorted.htmlTired of implementing Comparable? sick of creating Comparators?

In my case I had a Map full of complex objects. Each of my complex objects held an arraylist of photo albums. As I was going to be iterating over the map later on I wanted to be able to get the elements out of the map ordered by the number of photoalbums in each element value - in descending order:

Groovy to the rescue with the spaceship operator (<=>)  and a simple one liner:

friendActivityBucketMap = friendActivityBucketMap.sort {entry1,entry2 -> entry2.value.photoAlbums.size() <=> entry1.value.photoAlbums.size()}


]]>
<![CDATA[Adding Groovy to a Spring MVC Maven project]]>Wed, 04 Apr 2012 16:24:40 GMThttp://roundhilloftheapples.weebly.com/1/post/2012/04/adding-groovy-to-a-spring-mvc-maven-project.htmlI've been working on a Spring MVC project for the past year or so. Our stack basically goes as follows: Spring MVC, Hibernate, MySql. We also use JQuery, JMS and Solr.

Whilst working with Spring is great, Java was just becoming too verbose, so I decided to make an effort to integrate Groovy.

Turns out its pretty easy..

First add the following dependency to your Maven pom.xml:


<!-- Groovy -->
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>1.8.6</version>
            </dependency>



Next modify the maven compiler plugin:


                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerId>groovy-eclipse-compiler</compilerId>
                        <verbose>false</verbose>
                    </configuration>
                    <dependencies>
                      <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.6.0-01</version>
                      </dependency>
                      <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>1.8.6-01</version>
                      </dependency>
                    </dependencies>
                </plugin>

At that's basically it. If you are working on a Spring MVC project like I am, you can now create .groovy files in src/main/java or src/main/groovy. In addition you can also annotate them with @Service etc and @Autowire them into other Java or Groovy classes.

One problem I did have was autowiring a groovy bean that was annotated with @Transactional into another. It turns out the probject was because the class did not have an interface and this causes proxy problems for Spring. The following change to the bean solved the problem:

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class UserNotificationsScheduled {
..
}



]]>