Using Spring's JdbcTemplate with Micronaut

If you haven't heard, there's a hot new kid on the JVM Framework block called Micronaut. Micronaut touts fast startup times and small memory footprints. So far as my limited exposure to it goes, this seems to be true. Out of the box, Micronaut provides a robust set of data integrations like Hibernate/JPA, Mongo, Neo4J and even Postgresql using the reactive postgres client. I have a love hate relationship with Hibernate/JPA so I wanted to know what it would take to use Spring's JdbcTemplate API with Micronaut.

JdbcTemplate is pretty easy to use. It just needs to be instantiated with a DataSource. The first step was getting a DataSource bean wired up in Micronaut. The docs tell us exactly how to do that. BTW, the Micronaut docs are outstanding, especially considering how young the framework is. You can read the docs for yourself to see what you need. In addition, you'll want a JDBC driver for your RDMS of choice, the org.springframework.jdbc library, and for Transaction support micronaut-spring. The following were added to my build.gradle file's dependencies:

dependencies {  
  ...
  compile "io.micronaut.configuration:micronaut-jdbc-tomcat"
  compile 'org.postgresql:postgresql:42.2.4'
  compile 'org.springframework:org.springframework.jdbc:3.2.2.RELEASE'
  compile "io.micronaut:micronaut-spring"
...
}

micronaut-jdbc-tomcat allows you to define your default DataSource in application.yml. Again, the docs are pretty clear on this but I'll include mine for the sake of completion:

---
micronaut:  
  application:
    name: my-app

---
datasources:  
  default:
    url: jdbc:postgresql://0.0.0.0:5432/somedb
    username: someuser
    password: somepassword
    driverClassName: org.postgresql.Driver

What micronaut-jdbc-tomcat does for you is wire up a DataSource bean that can be injected. What we need is to be able to inject a JdbcTemplte bean. To do this, I created a Factory class:

@Factory
public class JdbcTemplateFactory {

    @Inject
    DataSource dataSource;

    @Bean
    @Singleton
    JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }
}

I inject the DataSource bean we get for free and then create my JdbcTemplate bean using our default DataSource. And that's it! Now we can do something like this:

@Singleton
@Requires(beans = JdbcTemplate.class)
public class UserService {

    private final JdbcTemplate jdbcTemplate;

    public UserService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Transactional
    public void printUsernames() {
        jdbcTemplate.query("select * from sec_user", (rs) -> {
            System.out.println(rs.getString("username"));
        });
    }
}

Yep, lamest example I could come up with but it gets the point across. Note that most JDBC drivers are still blocking so while you can use the cool reactive features of Micronaut, you'll want to take care using JDBC unless your vendor has a Reactive driver.

I'm really excited about Micronaut and its future. I'm still diving in but I've found a couple of smaller projects at work that will allow me to take for a enterprise level spin.