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 compilegroup:'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

FieldDescription
actionThe name of the command that execute the logical behavior e.g. '/fees'
controllerThe name of your controller e.g. 'fees_controller'
web_frameworkThe 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:

importcom.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;@EnableWebMvc@ConfigurationpublicclassWebConfigimplementsWebMvcConfigurer{@BeanpublicSpringSQLCommenterInterceptorsqlInterceptor(){returnnewSpringSQLCommenterInterceptor();}@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){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:

importcom.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;@EnableWebMvc@ConfigurationpublicclassWebConfigextendsWebMvcConfigureAdapter{@BeanpublicSpringSQLCommenterInterceptorsqlInterceptor(){returnnewSpringSQLCommenterInterceptor();}@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(sqlInterceptor());}}

XML based configuration

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

<mvc:interceptors><beanclass="com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor"></bean></mvc:interceptors>
<mvc:interceptors><mvc:interceptor><mvc:mappingpath="/flights"></mvc:mapping><beanclass="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

importcom.google.cloud.sqlcommenter.schhibernate.SCHibernate;@Configuration@EnableTransactionManagementpublicclassJPAConfig{@BeanpublicLocalContainerEntityManagerFactoryBeanentityManagerFactory(){LocalContainerEntityManagerFactoryBeanem=newLocalContainerEntityManagerFactoryBean();em.setDataSource(dataSource());em.setPackagesToScan(newString[]{"you.application.domain.model"});em.setJpaVendorAdapter(newHibernateJpaVendorAdapter());em.setJpaProperties(additionalProperties());returnem;}privatePropertiesadditionalProperties(){Propertiesproperties=newProperties();properties.setProperty("hibernate.session_factory.statement_inspector",SCHibernate.class.getName());returnproperties;}}

References

ResourceURL
Spring framework homepagehttps://spring.io/
sqlcommenter-java on Githubhttps://github.com/google/sqlcommenter/tree/master/java/sqlcommenter-java
Spring Interceptorhttps://docs.spring.io/spring/docs/5.0.4.BUILD-SNAPSHOT/javadoc-api/org/aopalliance/intercept/Interceptor.html
Hibernate SQLCommenter integration/java/hibernate
close