Spring in Practice

Willie Wheeler's Spring blog

Spring in Practice Now Available

| Comments

SiP at Barnes & Noble

Spring in Practice is now available!

It’s also the Deal of the Day today, which means half off if you buy it today (May 8, 2013) from Manning. Use code dotd0508au at www.manning.com/wheeler.

Josh and I hope that everybody enjoys the book. It was a lot of work, but I’m very happy with the end result.

Renaming Node Classes When Using Spring Data Neo4j

| Comments

This is one of those posts where I’m just jotting down some notes for my own future use, but someone else may find this useful.

I’m going to show how to rename a node entity class when using Spring Data Neo4j. I’m talking about the fully-qualified classname here, so it applies when we want to rename a package too.

Pageable Custom Queries With Spring Data JPA

| Comments

In previous posts I explained how you can use Spring Data JPA to create repositories that support custom queries, as well as to support paging in your app. You might wonder whether you can use these together.

The answer is yes. It works just like you would expect:

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface IncidentRepo extends JpaRepository<Incident, Long> {

    Page<Incident> findByProblemId(Long problemId, Pageable pageable);
}

How to Run JavaScript From Java

| Comments

Java 6 comes with the Rhino JavaScript engine, which makes it easy to run JavaScript from inside your Java app. There are different situations in which you might want to do this. Chapter 9 of Spring in Practice affords a good example. There we’re implementing a rich-text comment engine based on the WMD editor that Stack Overflow uses. We have a showdown.js script that maps Markdown to HTML, and we want to run it in two places:

  • on the client to present a preview pane
  • on the server to store the Markdown as HTML for easy presentation (as opposed to running the script against every comment dynamically)

Here’s how we can run it on the server:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

...

public final class RichTextFilter implements TextFilter {
    @Inject private File showdownJsFile;

    private String markdownToHtml(String markdown) {
        try {
            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine engine = mgr.getEngineByName("JavaScript");
            engine.eval(showdownJs);
            engine.eval("var markdown = '" + markdown + "';");
            engine.eval("var converter = new Showdown.converter();");
            engine.eval("var html = converter.makeHtml(markdown);");
            return (String) engine.get("html");
        } catch (ScriptException e) {
            // Shouldn't happen unless somebody breaks the script
            throw new RuntimeException(e);
        }
    }

    ... other stuff ...
}

To see how to inject the File into the class in Spring, see my post Injecting a file from the classpath into a bean.

Injecting a File From the Classpath Into a Bean

| Comments

Here’s a quick tip for you.

Sometimes you need to inject a java.io.File from your classpath into a bean, but you don’t want to have to spell out the absolute path (even in a configuration file). Never fear. It’s easy:

<bean id="tagProviderResource" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="/htmlcleaner.xml" />
</bean>

<util:property-path id="tagProviderFile" path="tagProviderResource.file" />

<bean id="tagProvider" class="org.htmlcleaner.ConfigFileTagProvider">
    <constructor-arg ref="tagProviderFile" />
</bean>

In the configuration above, I used ClassPathResource to find the htmlcleaner.xml resource on the classpath. Then I used the handy <util:property-path> tag to assign the resource’s file property its own ID. Finally, I inject the File using constructor injection.

Spring’s Constructor Namespace Is a Bad Idea

| Comments

The other day I wrote up a post explaining how to use Spring’s constructor namespace, which is new with Spring 3.1. So the following might be a little surprising:

Spring’s constructor namespace is a bad idea.

I wanted to like it—honest I did. While I’ve always been a little iffy on the whole idea of introspecting on method parameter names, Juergen Hoeller answered my concerns with respect to @PathVariable and the like. My worries around least surprise were admittedly academic since I always compile binaries in debug mode.

So I tried the constructor namespace out, and it bit me in exactly the way I expected.

Mavenizing an Empty GitHub Project in Eclipse

| Comments

This post is for myself more than anything else, just because I keep forgetting the steps involved.

I’m using SpringSource Tool Suite 2.9.1.RELEASE, which is based on Eclipse 3.7.2 (Indigo). I have the egit and m2e Eclipse plugins installed.

The scenario

You have a brand new, pretty-much-empty GitHub project (other than the README, say—but no Maven stuff yet), and you want to import it into Eclipse as a Maven project.

The steps

  1. Add the remote GitHub repo to your list of Git repos in Eclipse.
  2. In Eclipse, go to File → Import → Git → Projects from Git. (I’m on a Mac; the menu may be a little different for other platforms.)
  3. On the “Select Repository Source”, choose “URI”.
  4. On the “Source Git Repository” pane, enter the URI info. It might be something like ssh://git@github.com/williewheeler/sip11.git, for example.
  5. On the “Branch Selection” pane, choose the master branch.
  6. On the “Local Destination” pane, decide where you want the local copy to live.
  7. Where it asks you to select an import wizard, choose “Use the New Project wizard” and click “Finish”.
  8. Now you have to choose a New Project wizard. Choose Maven → Maven Project.
  9. From here just create the project like you would any other new Maven project. Once you’re done, it will show up in your list of projects in the Package Explorer view, and sharing should be activated.

Combined My Two Blogs

| Comments

To better focus my Java/Spring blogging, I’ve merged my former Wheeler Software blog into this one. There are a few articles that didn’t make the cut, but the majority of them did.