JaVers needs funding to sustain. You can sponsor JaVers development easily via GitHub Sponsors or Open Collective.

Spring Boot integration

Spring Boot has become a standard in the world of Java enterprise applications.

Our Spring Boot starters simplify integrating JaVers with your application. All required JaVers beans are created and auto-configured with reasonable defaults.

There are two starters compatible with Spring Data and common persistence stacks:

Get JaVers Spring Boot starter

MongoDB starter

Add JaVers MongoDB and Spring Data MongoDB starters to your classpath:

compile 'org.javers:javers-spring-boot-starter-mongo:5.8.6'
compile 'org.springframework.boot:spring-boot-starter-data-mongodb:' + $SPRING_BOOT_VERSION   

SQL starter

Add JaVers SQL and Spring Data JPA starters to your classpath:

compile 'org.javers:javers-spring-boot-starter-sql:5.8.6'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:' + $SPRING_BOOT_VERSION   

Check Maven Central for other build tool snippets.

JaVers configuration

Use Spring Boot configuration and configure JaVers in the same way as your application (typically by YAML property files).

Here is an example application.yml file with the full list of JaVers core properties and their default values. You don’t need to add anything to your configuration, if these defaults are OK for you.

javers:
  mappingStyle: FIELD
  algorithm: SIMPLE
  commitIdGenerator: synchronized_sequence
  prettyPrint: true
  typeSafeValues: false
  newObjectSnapshot: false
  packagesToScan:
  auditableAspectEnabled: true
  springDataAuditableRepositoryAspectEnabled: true
  sqlSchema:
  sqlSchemaManagementEnabled: true
  sqlGlobalIdCacheDisabled: false
  objectAccessHook:
  prettyPrintDateFormats:
    localDateTime: "dd MMM yyyy, HH:mm:ss"
    zonedDateTime: "dd MMM yyyy, HH:mm:ssZ"
    localDate: "dd MMM yyyy"
    localTime: "HH:mm:ss"  

Properties active in SQL starter:

javers:
  sqlSchema:
  sqlSchemaManagementEnabled: true
  sqlGlobalIdCacheDisabled: false
  objectAccessHook: org.javers.hibernate.integration.HibernateUnproxyObjectAccessHook

Properties active in MongoDB starter:

javers:
  documentDbCompatibilityEnabled: false
  objectAccessHook: org.javers.spring.mongodb.DBRefUnproxyObjectAccessHook

Each property in the Spring application.yml file has the corresponding with*() method in JaversBuilder.

Spring AutoConfiguration

Thanks to Spring Boot magic, starters available on the classpath are automatically picked up and launched.

See the complete list of JaVers beans added to your Spring ApplicationContext:

AuthorProvider and CommitPropertiesProvider beans

These two beans are required by Auto-audit aspect. For both, default implementations are created by JaVers starter:

Register your own beans only if you need to override these defaults.

See documentation of AuthorProvider and CommitPropertiesProvider for more details.

JaversRepository configuration

JaVers starters rely on Spring Data starters. Proper JaversRepository implementation is created and configured to reuse an application’s database (managed by Spring Data starters).

Dedicated Mongo database for JaVers

Optionally, you can use dedicated Mongo database for JaVers data, configure Javers as shown below:

javers:
  mongodb:
    host: localhost
    port: 27017
    database: javers-audit
    authentication-database: admin
    username: javers
    password: password

or:

javers:
  mongodb:
    uri: mongodb://javers:[email protected]:27017/javers-audit&authSource=admin

If javers.mongodb property is defined, either host or uri has to set. If so, an application’s data and JaVers data are stored in different databases.

MongoClientOptions

If you need more control over Javers’ dedicated MongoClient, you can configure a MongoClientOptions bean named javersMongoClientOptions. If there is no such bean, default client options are used.

For example, if you want to enable SSL and set socket timeout value, define this bean:

@Bean("javersMongoClientOptions")
public MongoClientOptions clientOptions() {
  return MongoClientOptions.builder()
  .sslEnabled(true)
  .socketTimeout(1500)
  .build();
}

Remember, the javersMongoClientOptions bean is used only when JaVers connects to dedicated Mongo database defined in javers.mongodb property.

Boot it!

Once you’ve added the JaVers starter to the classpath, you can use all JaVers features.

Auto-audit aspect annotations

JaVers auto-audit aspect is based on annotations: @JaversSpringDataAuditable and @JaversAuditable.

Basically, choose Entities you want to be audited by JaVers and add @JaversSpringDataAuditable to corresponding Spring Data CRUD repositories.

For example, if you want to audit the Person Entity, annotate PersonRepository:

import org.javers.spring.annotation.JaversSpringDataAuditable
import org.springframework.data.mongodb.repository.MongoRepository;
import org.javers.organization.structure.domain.Person;

@JaversSpringDataAuditable
public interface PersonRepository extends MongoRepository<Person, String> {
}

and all changes made to Person objects will be committed to JaVersRepository.

If you aren’t using Spring Data repositories, annotate all data-changing methods with @JaversAuditable.

For example:

@Repository
class UserRepository {
    @JaversAuditable
    public void save(User user) {
        ...//
    }

    public User find(String login) {
        ...//
    }
}

Manual commits

If you need more fine-grained control over JaVers commits, use JaVers instance directly (which is available as a Spring bean).

For example, this service commits changes made to a fired Person but only in Friday:

@Service
class PersonService {
    private final Javers javers;
    private final PersonRepository personRepository;

    @Autowired
    public PersonService(Javers javers, PersonRepository personRepository) {
        this.javers = javers;
        this.personRepository = personRepository;
    }
    
    public void fire(Person person) {
        person.fire();
        personRepository.save(person);

        if (LocalDate.now().getDayOfWeek() == DayOfWeek.FRIDAY){
            javers.commit("author", person);
        }
    }
}

Querying JaversRepository

When your objects are persisted in JaversRepository use JQL to query for snapshots and changes.

REST controller example:

@RestController
@RequestMapping(value = "/audit")
public class AuditController {

    private final Javers javers;

    @Autowired
    public AuditController(Javers javers) {
        this.javers = javers;
    }

    @RequestMapping("/person")
    public String getPersonChanges() {
        QueryBuilder jqlQuery = QueryBuilder.byClass(Person.class);

        List<Change> changes = javers.findChanges(jqlQuery.build());

        return javers.getJsonConverter().toJson(changes);
    }
}

Demo application

We created a demo application based on Spring Boot. It manages the Organization Structure domain. The User can change salaries and positions of Employees and move them in the Structure. All changes are audited and easy to browse.

Check out how easy it is to add the JaVers audit to the Spring Boot application.

Clone it from github:

git clone https://github.com/javers/organization-structure.git

Run it

cd organization-structure
./gradlew organization-structure-sql:bootRun

and check it out on localhost:8080/view/person.