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