org.javers.spring.boot.sql.JaversSqlAutoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-spring-boot-starter-sql Show documentation
Show all versions of javers-spring-boot-starter-sql Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.spring.boot.sql;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.javers.core.Javers;
import org.javers.hibernate.integration.HibernateUnproxyObjectAccessHook;
import org.javers.repository.sql.ConnectionProvider;
import org.javers.repository.sql.DialectName;
import org.javers.repository.sql.JaversSqlRepository;
import org.javers.repository.sql.SqlRepositoryBuilder;
import org.javers.spring.auditable.*;
import org.javers.spring.auditable.aspect.JaversAuditableAspect;
import org.javers.spring.auditable.aspect.springdatajpa.JaversSpringDataJpaAuditableRepositoryAspect;
import org.javers.spring.jpa.JpaHibernateConnectionProvider;
import org.javers.spring.jpa.TransactionalJaversBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManagerFactory;
/**
* @author pawelszymczyk
*/
@Configuration
@EnableAspectJAutoProxy
@EnableConfigurationProperties(value = {JaversSqlProperties.class, JpaProperties.class})
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
public class JaversSqlAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(JaversSqlAutoConfiguration.class);
private final DialectMapper dialectMapper = new DialectMapper();
@Autowired
private JaversSqlProperties javersSqlProperties;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public DialectName javersSqlDialectName() {
SessionFactoryImplementor sessionFactory =
(SessionFactoryImplementor) entityManagerFactory.unwrap(SessionFactory.class);
Dialect hibernateDialect = sessionFactory.getDialect();
logger.info("detected Hibernate dialect: " + hibernateDialect.getClass().getSimpleName());
return dialectMapper.map(hibernateDialect);
}
@Bean(name = "JaversSqlRepositoryFromStarter")
@ConditionalOnMissingBean
public JaversSqlRepository javersSqlRepository(ConnectionProvider connectionProvider) {
return SqlRepositoryBuilder
.sqlRepository()
.withSchema(javersSqlProperties.getSqlSchema())
.withConnectionProvider(connectionProvider)
.withDialect(javersSqlDialectName())
.withSchemaManagementEnabled(javersSqlProperties.isSqlSchemaManagementEnabled())
.build();
}
@Bean(name = "JaversFromStarter")
@ConditionalOnMissingBean
public Javers javers(JaversSqlRepository sqlRepository, PlatformTransactionManager transactionManager) {
return TransactionalJaversBuilder
.javers()
.withTxManager(transactionManager)
.registerJaversRepository(sqlRepository)
.withObjectAccessHook(new HibernateUnproxyObjectAccessHook())
.withProperties(javersSqlProperties)
.build();
}
@Bean(name = "SpringSecurityAuthorProvider")
@ConditionalOnMissingBean
@ConditionalOnClass(name = {"org.springframework.security.core.context.SecurityContextHolder"})
public AuthorProvider springSecurityAuthorProvider() {
return new SpringSecurityAuthorProvider();
}
@Bean(name = "MockAuthorProvider")
@ConditionalOnMissingBean
@ConditionalOnMissingClass({"org.springframework.security.core.context.SecurityContextHolder"})
public AuthorProvider unknownAuthorProvider() {
return new MockAuthorProvider();
}
@Bean(name = "EmptyPropertiesProvider")
@ConditionalOnMissingBean
public CommitPropertiesProvider commitPropertiesProvider() {
return new EmptyPropertiesProvider();
}
@Bean(name = "JpaHibernateConnectionProvider")
@ConditionalOnMissingBean
public ConnectionProvider jpaConnectionProvider() {
return new JpaHibernateConnectionProvider();
}
@Bean
@ConditionalOnProperty(name = "javers.auditableAspectEnabled", havingValue = "true", matchIfMissing = true)
public JaversAuditableAspect javersAuditableAspect(Javers javers, AuthorProvider authorProvider, CommitPropertiesProvider commitPropertiesProvider) {
return new JaversAuditableAspect(javers, authorProvider, commitPropertiesProvider);
}
@Bean
@ConditionalOnProperty(name = "javers.springDataAuditableRepositoryAspectEnabled", havingValue = "true", matchIfMissing = true)
public JaversSpringDataJpaAuditableRepositoryAspect javersSpringDataAuditableAspect(Javers javers, AuthorProvider authorProvider, CommitPropertiesProvider commitPropertiesProvider) {
return new JaversSpringDataJpaAuditableRepositoryAspect(javers, authorProvider, commitPropertiesProvider);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy