我正在使用Springboot版本2.1.2和MSSQL Server作为数据源。当我只使用一个数据源时,envers没问题。审核工作正常。但是,当一个项目需要两个数据源时,envers无法配合。我只需要审核第一个数据源。
这是配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = { "com.sample.app.repository", "com.sample.app.security.repository" },
repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class
)
public class MainDbConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties appDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource appDataSource() {
return mainDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("appDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.sample.app.model", "com.sample.app.security.model", "com.sample.app.audit")
.persistenceUnit("app")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
和
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "otherEntityManagerFactory",
transactionManagerRef = "otherTransactionManager",
basePackages = {"com.sample.other.repository"}
)
public class OtherDbConfig {
@Bean
@ConfigurationProperties(prefix = "other.datasource")
public DataSourceProperties otherDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("other.datasource.hikari")
public HikariDataSource otherDataSource() {
return otherDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Bean(name = "otherEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("otherDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.sample.other.model")
.persistenceUnit("other")
.build();
}
@Primary
@Bean(name = "otherTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("otherEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
在访问审计之前,一切都很好,可以访问这两个数据源。我创建了一个自定义侦听器:
public class AuditRevisionListener implements RevisionListener {
@Value("${jwt.header:Authorization}")
private String tokenHeader;
@Autowired
private TokenUtil jwtTokenUtil;
@Override
public void newRevision(Object revisionEntity) {
AuditRevisionEntity auditRevisionEntity = (AuditRevisionEntity) revisionEntity;
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String username = jwtTokenUtil.getUsernameFromToken(request.getHeader("X-Auth-Token"));
auditRevisionEntity.setUsername(username);
}
}
tokenHeader不会被填充,TokenUtil不会被初始化。当我使用一个数据源时,一切都很好。
任何帮助将不胜感激。