5
我試圖在我的服務的方法中使用@Transactional
註釋來延遲加載字段。然而,在我的Implementation類中使用@Transactional
將使所有自動裝配字段null
。Spring引導2 @Transactional註釋使得Autowired字段爲空
這是我實現:
@Service
public class UserServiceImpl implements UserService {
/**
* DefaultMapper.
*/
@Autowired
private DefaultMapper defaultMapper;
/**
* Resource service injection.
*/
@Autowired
private ResourceService resourceService;
/**
* UserRepository.
*/
@Autowired
private UserRepository userRepository;
/**
* Jwt Factory.
*/
@Autowired
private JwtService jwtService;
@Override
@Transactional
public final UserDto findByLogin(final String login) throws ResourceNotFoundException {
// user repository is null here when using @Transactional
User user = this.userRepository.findByLogin(login)
.orElseThrow(() -> new ResourceNotFoundException(
resourceService.getMessage(MessageBundle.EXCEPTION, "resource.notfound.user.login")
));
UserDto userDto = defaultMapper.asUserDtoWithRoles(user);
return userDto;
}
預先感謝您。
從你的方法中刪除'final'或者在'application.properties'中設置'spring.aop.proxy-target-class = false'。 **注意:**這需要Spring Boot 1.5.3或完全正常工作! –
在你的spring應用程序中啓用了事務上下文嗎? – drgPP
在閱讀行爲中,大部分時間進行交易是無用的,你確定你需要閱讀交易嗎? – Zorglube