Spring

Introduction

We provide an integration for the Spring framework. The integration is an interceptor that will record properties about your MVC application per HTTP request, and then later those properties will be picked up by the actual ORMs and augment your SQL statements. It is best used with the following ORM integrations:

Requirements

Dependency management

We can add the integration to our applications in the following ways:

Manually

Please read installing sqlcommenter-java from source

Package management

Please include this in your dependency management system as follows

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>sqlcommenter-java</artifactId>
        <version>0.0.1</version>
    </dependency>
// https://mvnrepository.com/artifact/com.google.cloud/sqlcommenter-java
compile group: 'com.google.cloud', name: 'sqlcommenter-java', version: '0.0.1'

Expected fields

When coupled say with sqlcommenter for Hibernate, the following fields will be added to your SQL statement as comments

Field Description
action The name of the command that execute the logical behavior e.g. '/fees'
controller The name of your controller e.g. 'fees_controller'
web_framework The name of the framework, it will always be 'spring'

Using it

There are 2 different flavors of Spring – Spring 5 and later vs before Spring 5. Please read along to see how to enable it for the different versions:

Spring 5

If using Spring 5, please import the SpringSQLCommenterInterceptor class by:

import com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public SpringSQLCommenterInterceptor sqlInterceptor() {
         return new SpringSQLCommenterInterceptor();
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sqlInterceptor());
    }
}

Before Spring 5

If using a version before Spring 5, your WebConfig class needs to extend the WebMVCConfigureAdapter class instead like this:

import com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigureAdapter {

    @Bean
    public SpringSQLCommenterInterceptor sqlInterceptor() {
         return new SpringSQLCommenterInterceptor();
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sqlInterceptor());
    }
}

XML based configuration

You can add the interceptor as a bean in your XML configuration

<mvc:interceptors>
    <bean class="com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor"></bean>
</mvc:interceptors>
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/flights"></mvc:mapping>
        <bean class="com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>

Hibernate

If Spring is using Hibernate, in addtion to the step XML based configuration, since you might not be using a persistence.xml file, we can setup in Java code the hibernate.session_factory.statement_inspector configuration property in your additionalProperties method as per

import com.google.cloud.sqlcommenter.schhibernate.SCHibernate;

@Configuration
@EnableTransactionManagement
public class JPAConfig {
 
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em 
        = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "you.application.domain.model" });

        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(additionalProperties());

        return em;
    }
    
    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.session_factory.statement_inspector", SCHibernate.class.getName());

        return properties;
    }
}

References

Resource URL
Spring framework homepage https://spring.io/
sqlcommenter-java on Github https://github.com/google/sqlcommenter/tree/master/java/sqlcommenter-java
Spring Interceptor https://docs.spring.io/spring/docs/5.0.4.BUILD-SNAPSHOT/javadoc-api/org/aopalliance/intercept/Interceptor.html
Hibernate SQLCommenter integration /java/hibernate