私のサービスのメソッドで@Transactional
注釈を使用してフィールドを遅延ロードしようとしています。しかし、私の実装クラスの@Transactional
を使用すると、すべての自動フィールドが作成されますnull
。 春のブート2 @トランザクションの注釈によってAutowiredフィールドがnullになる
@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