我正在尝试在我的服务方法中使用@Transactional
注解来懒加载一个字段。然而,将@Transactional
应用于我的实现类会使所有自动装配的字段变为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 或更高版本才能完全生效! - M. Deinum