Round hill of the apples - Groovy, Grails, Griffon and a hint of Java

  • Blog
  • About
  • Contact us
 

Identified Flying Objects - no worries, I got it sorted

04/04/2012

1 Comment

 
Tired 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()}


1 Comment
 

Adding Groovy to a Spring MVC Maven project

04/04/2012

10 Comments

 
I'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 {
..
}



10 Comments
 

    Author

    Groovy & Java developer at FunnelScope.com

    Archives

    April 2012

    Categories

    All
    Groovy Collections
    Spring Groovy Dependency Injection

    RSS Feed


Create a free website with Weebly