Spring in Practice

Willie Wheeler's Spring blog

Migrating non-Wordpress Blog Comments Into Wordpress

| Comments

This isn’t a Spring post, but I’m doing something that I think others might find useful, so I’m going to share it.

I’m in the process of migrating content over from my old Wheeler Software blog to this one, which is a Wordpress blog. Besides the posts themselves, I want to move the comments over.

The slight wrinkle in the plan is that the software and database for the old blog are custom. So getting the comments over involves some SQL scripting. Both databases are MySQL databases, so that helps a bit.

Here’s what I’m doing.

The Spring Constructor Namespace

| Comments

You know the p namespace? It’s the one that cleans up your Spring configuration files by providing a shorthand for injecting beans and values:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=
       "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:packagesToScan="com.springinpractice.ch10.model" />

    ... other beans ...

</beans>

Did you know that Spring 3.1 introduces an analogous namespace for constructor injection? Yup—it’s the c namespace:

Dynamic DAOs and Queries Using Spring Data JPA

| Comments

For a long time, creating a DAO layer in Spring has been a largely manual process:

  1. Create a base generic DAO interface.
  2. Create a generic abstract DAO implementation with general-purpose CRUD methods and common queries (e.g., findAll()).
  3. For each DAO we want, extend the base DAO interface with an entity-specific interface (e.g., CustomerDao).
  4. For each DAO we want, extend the abstract DAO class with an entity-specific concrete class (e.g., HibernateCustomerDao).

Items 1 and 2 amount to creating a homegrown DAO framework, and items 3 and 4 amount to using it to implement DAOs.

Now there’s a better way to do things.

Sending Cookies With RestTemplate

| Comments

Sometimes it is necessary to send cookies along with requests to a RESTful API. One such example is the JIRA 4.3 API, which requires sending the JSESSIONID to JIRA for session management and authentication purposes. REST purists point out that such usages are not properly RESTful (see http://blog.mikepearce.net/2010/08/24/cookies-and-the-restful-api/ for a good discussion), and indeed the RestTemplate doesn’t directly support sending cookies.

But in the real world, we need to make things work, and so in this quick post I’ll show how to send cookies with RestTemplate.

Zkybase Now Supports Authorized Access to GitHub via Spring Social GitHub

| Comments

This one’s another quick Zkybase screenshot post. Using Spring Social GitHub, it’s easy to access public user and repo information via the GitHubTemplate. But if we want to access private information, or write capabilities, we need to use the Spring Social connection framework. This involves adding a Spring Social web controller to the app, giving users a way to log into Zkybase itself (via Spring Security, and then finally giving them a way to connect their Zkybase account to their GitHub account via OAuth 2.