Spring dependency injection marked unsafe by SpotBugs
09:59 19 Oct 2022

Is there any way to do injections better way?

@Service
public class CountryServiceImpl implements CountryService {

    private final UserSessionService userSessionService;

    public CountryServiceImpl(final UserSessionService userSessionService) {
        this.userSessionService = userSessionService; // SpotBugs warning
    }

}

SpotBugs gives me a warning on marked the line:

This code stores a reference to an externally mutable object into the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.

@Autowired should be the last option beacause it removes ability to inject mock beans during integration test.

java spring dependency-injection service spotbugs