Tuesday, December 9, 2008

Maven generating JAXB with the Fluent API

I thought this was interesting and there wasn't a lot of information on this online. It's using the Maven JAXB plugin to generate JAXB classes with the Fluent API. All the regular methods for getting and setting values are available, but there is also an API to chain together setting and creating classes. Thanks to Steve Berman for the Maven config.

Standard API

PersonResponse personList = new PersonResponse();

Person person = new Person();
person.setId(ID);
person.setFirstName(FIRST_NAME);
person.setLastName(LAST_NAME);

Person person2 = new Person();
person2.setId(SECOND_ID);
person2.setFirstName(SECOND_FIRST_NAME);
person2.setLastName(SECOND_LAST_NAME);

personList.getPerson().add(person);
personList.getPerson().add(person2);


Fluent API

PersonResponse personList = new PersonResponse().withPerson(
    new Person().withId(ID).withFirstName(FIRST_NAME).withLastName(LAST_NAME),
    new Person().withId(SECOND_ID).withFirstName(SECOND_FIRST_NAME).withLastName(SECOND_LAST_NAME));


Inside the build element's plugins section, this can be added to generate JAXB beans with the Fluent API.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xfluent-api</arg>
        </args>
        <schemaDirectory>src/main/resources</schemaDirectory>
        <plugins>
            <plugin>
                <groupId>net.java.dev.jaxb2-commons</groupId>
                <artifactId>jaxb-fluent-api</artifactId>
                <version>2.1.8</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<repositories>
    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net Maven 2 Repository</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
</repositories>

Friday, December 5, 2008

Spring by Example Wins

I was going to really try to get back into working on the book, but I think I realistically don't have the time. I did make a small update to Spring In-depth, In Context before SpringOne started, but I was thinking about how Spring 3.0 is coming out soon so I would need to go through everything again. Posting examples on Spring by Example is less effort since each example is more or less standalone and I'm primarily writing up something specifically on the example. Instead of trying to explain the subject completely, which involves a lot more research and effort. Maybe one day I'll try to get back into working on it, but I actually think hands on examples are probably more valuable. At least that what I always like to see. I typically just want to get going on a project and I'll learn all the nuances of the technology as I go. Possibly the preface and intro from the book can become part of the Spring by Example documentation at some point.

Based on this decision I moved the work I had done for the book to Spring by Example.

Tuesday, December 2, 2008

SpringOne Americas 2008 Presentation

SpringOne has been really interesting so far. Rossen Stoyanchev gave a really excellent presentation on all the nuances and best practices for using Spring MVC Annotations. I'll need to update all my web examples, because there's at least one thing if not more that could be done more elegantly. Like using the @ModelAttribute on a method to populate the model for a form instead of using a dummy method just for a create (so the form has an instance to bind to).

My presentation on GWT & Dojo Cometd with Spring Bayeux is tomorrow morning. I've put a lot of work into the presentation, writing up what I could for Spring In Depth, In Context, and getting the examples cleaned up as much as possible and checked into Subversion. Hopefully the presentation will go well and everyone will find it interesting.

Spring by Example & 'Spring In-depth, In Context'

I've made updates to both sites. Spring by Example doesn't have anything too major, but the last site release updated the Spring by Example Web Module to version 1.1.1. For Spring In-depth, In Context, I finally had time to get the Chat and Trade Monitor examples I made for my SpringOne presentation posted and written up for Jetty. The examples are checked in except for the Trade Monitor example running an embedded Jetty in Tomcat. This is the same as the Trade Monitor that runs under Jetty, was just to show that you could still use Jetty's Bayeux implementation even if you were on Tomcat.

Also I finally had time to post a simple Spring dm Server example showing side-by-side versioning. It has a version 1.0 and 1.1 of a message service. The version 1.0 uses Commons Lang 2.1.0 and version 1.1 uses Commons Lang 2.4.0. There is a web module that just displays the data and you can switch back and forth between the two different message services at runtime. I'll work on this section more to add screen shots and a more detailed explanation of things, but I wanted to get what was ready posted.