Tuesday, August 12, 2008

Spring by Example Content Update

I've updated all of the examples on Spring by Example by making them static content instead of being served in the TWiki. I think the formatting is improved and I think it will make maintaining things easier. The Spring by Example TWiki is still running and available for user contributions.

This is the first draft converting the content. If you find any errors or problems with any examples, please let me know.


Monday, August 4, 2008

Spring Web Services Example

I ended up having time to finish up a really basic Spring Web Services example using JAXB for marshalling. It was very simple. Really once you have a project setup you will just have to focus on your XSD and business logic. It's nice having the marshalling/unmarshalling handled for you, but if the XML you're receiving becomes larger and you just need a specific part of it you can create a method that uses @XPathParam annotation in front of method variables to get a few values.

In the current example once everything is setup the client just has to make a call on the WebServiceTemplate passing in the request and receiving the response. Both are JAXB generated beans.

     PersonResponse response =
         (PersonResponse) wsTemplate.marshalSendAndReceive(request);


Saturday, August 2, 2008

Simple GWT Spring Webapp Example

I finally had time to finish up the GWT example I've been working on. I may add a bit more to it in the future (like an autocomplete search widget), but there is a lot there and should be helpful getting people started integrating GWT with Spring.
My general impression of using GWT is positive. It's definitely nice that you can work in Java and all the JavaScript is generated for you. Especially that it should perform well and work in different browers. It's a little tedious keeping everything in sync between the static servlet I use when running GWT in debug mode (avoids integrating Spring and injection into GWT debug) and the Spring controller. Also to have GWT use the main message resource for internationalization, a JavaScript object has to be maintained on the static HTML and JSP page (GWT debug vs. Spring webapp). It also wasn't obvious that if I made a bean to use with the main GWT entry point class that it had to be in the same package or a subpackage.

Also, as a side note, I have the person form working using Spring JS. It isn't too much work once I realized that it was working, but Spring Web Flow was refreshing the page afterword until I added at the end of the 'save' transition a render element specifying to only update the 'content' fragment. Otherwise only the 'onclick' attribute of the save button has to be modified.


<input type="submit" id="save"
       name="_eventId_save" value="<fmt:message key="button.save">"
       onclick="Spring.remoting.submitForm('save', 'person', {fragments:'content'}); return false;"/>


You currently can't specify a target div for returned fragments. I filed a JIRA (Enhance AjaxEventDecoration in Spring JS to specify a Target Div) to allow a targetId. Ideally it will be a list like fragments so you can specify what fragment goes with what div. I wanted to be able to have menu links refresh the content div without refreshing the entire page. Current funtionally assumes that you would just use AJAX for updates within functional areas. Like, I'm on the person page and want to just update the messages and personForm divs with fragments. So the fragment is expected to be wrapped in the div that is already is on the page and they are automatically matched up and updated. I think it will be a useful feature and hopefully will get added in an upcoming release.

Sunday, July 27, 2008

Spring by Example JDBC, Spring Dynamic Tiles Module, and Google Web Toolkit

I almost have everything ready to post a simple Google Web Toolkit (GWT) example with Spring. I have everything checked in and I also had to update Spring by Example JDBC and Spring by Example Dynamic Tiles Module.

Spring by Example JDBC version 1.0.3 was upgraded to match changes in Spring 2.5.5 and HsqldbInitializingDriverManagerDataSource & InitializingDriverManagerDataSource were changed to use SimpleDriverDataSource (which was added in Spring 2.5.5).

Spring by Example Dynamic Tiles Module 1.1 has a number of changes. The project was updated to use Spring 2.5.5, AJAX support like Spring JS and Spring Web Flow is provided, and a few other items (see release notes in the projects README.txt). Also, Spring by Example Dynamic Tiles Module 1.0 was compiled with Java 6, but should have been compiled with Java 5. The projects are basically all ready, but just need a little more testing. Although everything is checked into Subversion.

I'll try to get everything finalized and checked in over the next couple of days. I think the AJAX support that works with Spring JS' Spring.remoting.submitForm is great. It's really nice to have simple and easy control over updating specific div sections that match parts of a Tiles template.

Thursday, July 10, 2008

Spring JPA Example with Hibernate

This took longer than I expected since I was able to reuse the Entity beans from the Hibernate annotation example. I had a lot of trouble getting the right jars to work together. Hopefully the Maven config in the project will help someone else.

The key things besides the classpath to get setup in the example were just the Spring Config and putting the JPA configuration in META-INF/persistence.xml. Also the LocalContainerEntityManagerFactoryBean actually returns a javax.persistence.EntityManagerFactory. I assumed it returned an EntityManger based on the name of the class.

One other point is that JpaDaoSupport has setEntityManger and setEntityManagerFactory as final. So you can't override them, use @Autowired, then super the value to the parent method. I ended up passing in the EntityManagerFactory in the constructor using @Autowired. Which is probably better since it's required, but I'm used to typically using setters in Spring.

Note: This example has the DAO class extend JpaDaoSupport and uses the JpaTemplate, but Spring recommends the native JPA style of coding.

"
JpaTemplate mainly exists as a sibling of JdoTemplate and HibernateTemplate, offering the same style for people used to it. or newly started projects, consider adopting the native JPA style of coding data access objects instead, based on a "shared EntityManager" reference obtained through the JPA @PersistenceContext annotation." (from Spring 2.5.x JPA documentation)

Spring Config with Zero XML

So, if you really don't want to use any XML to configure Spring and you also don't want to use the Spring JavaConfig project (create beans using Java code), you can actually use annotation-based config. I found this looking at the context:component-scan unit tests. You just create a GenericApplicationContext and a ClassPathBeanDefinitionScanner. Then call refresh on the context load the bans and register a shutdown hook so you get destruction callbacks on your beans when the JVM is shutdown. The ClassPathBeanDefinitionScanner registers the @Autowired and @Required bean post processors. It also registers the JSR-250 Commons Annotations and the PersistenceAnnotationBeanPostProcessor for JPA if their jars are in the classpath. It's pretty straightforward.

Also it could be a good way to unit test how many beans are being pick up by the context:component-scan in case you're worried you configured the packages it's scanning incorrectly.

A downside to this is that if you are using the Spring IDE in Eclipse, you won't be able to register an XML file with it. The latest version handles XML configured beans and ones that are scanned so you can get consolidated bean graphs and all the other help it provides. I personally wouldn't do this and it's worth having a very, very small XML file just to define context:component-scan directly.


String basePackage = this.getClass().getPackage().getName();

GenericApplicationContext ctx = new GenericApplicationContext();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
int beanCount = scanner.scan(basePackage);

ctx.refresh();
ctx.registerShutdownHook();

Monday, July 7, 2008

Spring In-depth, In Context

I've been working for a while on a book for Spring 2.5. I've decided to post it for free online and hopefully it will be useful to people. It currently has a very good introduction to Spring, basic bean creation using XML and Annotations. Although the Annotations chapter isn't 100% complete. There are also partially complete chapters on resources & ApplicationContexts and AOP.

As updates are available, I'll continue to post them. I decided since there was a lot of information and examples done, it was better to release it now and continue to work on it when I have free time. I hope it helps people. It was a lot of work getting even this much done. There are so many nuances to Spring's configuration options between XML and Annotation-based configuration.

Also, a very special thanks to Susan Kerschbaumer who has co-authored the book with me and has personally done a really excellent job with what she has done so far. Currently Susan has only had time to work on the Preface, Chapter 1, and Chapter 2. But as she has time she will focus on other chapters.


Spring In-depth, In Context