0
我想加載彈簧啓動應用程序與預先存在的數據。我想用「系統」用戶填充數據。我有下面的組件定義被攔截並創建一個「系統」用戶。問題是此組件事務在針對H2運行彈簧引導應用程序時被提交到數據庫,但它是當應用程序與PostgreSQL數據源一起運行時,它不會被提交。請幫我弄清楚如何讓交易成功?SpringBoot應用程序 - AuditorAware事務不提交Postgres數據庫
被覆蓋的實施getCurrentAuditor方法是如下
@Transactional
@Component("auditorProvider")
public class UserAuditorAware implements AuditorAware<User> {
@Inject
private UserRepository userRepository;
private User _systemUser;
@Override
public User getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
if (_systemUser != null) {
return _systemUser;
}
Optional<User> systemUser = userRepository.findOneByUsername("system");
if (!systemUser.isPresent()) {
// TODO: decide on which user to designate when entities are created by devices
User user = new User("system");
user = userRepository.save(user);
userRepository.flush();
systemUser = Optional.of(user);
}
_systemUser = systemUser.get();
return _systemUser;
}
CurrentUser userDetails = (CurrentUser) authentication.getPrincipal();
return userDetails.getUser();
}
}
的主應用程序類具有以下注釋。
@ComponentScan
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class, SecurityAutoConfiguration.class })
public class Application {
...