#94 Review of a Spring User Creation Service

Companies:
OTP Bank
Themes:
SpringTransactionsCode Review

Perform a code review of the service:

@Component
public class UserService2 {

    public int counter = 0;

    @Autowired
    private UserFactory userFactory;

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionsServiceFeignClient remoteService;

    public void createUser() {
        remoteService.init();
        User user = userFactory.createUser();
        // do something else
        saveUser(user);
    }

    @Transactional
    private void saveUser(User user) {
        userService.save(user);
        Permissions permissions = remoteService.resolveUserPermissions(user);
        userService.savePermissions(user, permissions);
        counter++;
        System.out.println(counter);  // 1, 2, ...
    }
}

// test/java/UserServiceTest.java
@Component
public class UserServiceTest {
    @Autowired
    private UserService2 bean;

    public void aaa() {
        bean.createUser();
        System.out.println(bean.counter);  // 0
        bean.createUser();
        System.out.println(bean.counter);  // 0
        bean.createUser();
        System.out.println(bean.counter);  // 0
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in