Spring Boot调试时@Autowired注入的SessionScope对象字段为Null求助
Hey there! Let's work through why your SessionComponent fields are showing up as null when injected into AnotherClass. I've run into similar snags before, so here are the most common fixes to check:
1. Ensure you're operating within an HTTP request context
The @SessionScope bean is tied directly to an HTTP session, which only exists when handling a web request. If you're calling methods in AnotherClass from:
- A startup bean (like using
@PostConstructin a singleton bean) - A scheduled task (
@Scheduled) - A background thread outside of an active request
Spring can't resolve the active session, so it injects a proxy that doesn't have access to real session data—resulting in null fields.
Fix: Trigger your AnotherClass logic during an HTTP request. For example, call it from a controller endpoint:
@RestController public class UserController { @Autowired private AnotherClass anotherClass; @GetMapping("/user-details") public User getUserDetails() { return anotherClass.getCurrentUser(); // Runs in an active request context } }
2. Verify you're actually populating the session fields
A @SessionScope bean starts with all fields null—you need to explicitly set sessionId and user when the session initializes (like after a user logs in). If you haven't added code to populate these values, they'll stay null.
Example of setting fields on login:
@Controller public class LoginController { @Autowired private SessionComponent sessionComponent; @Autowired private UserService userService; @PostMapping("/login") public String handleLogin(@RequestParam String username, @RequestParam String password, HttpServletRequest request) { // Validate user credentials User authenticatedUser = userService.authenticate(username, password); // Populate the session component with user data sessionComponent.setUser(authenticatedUser); sessionComponent.setSessionId(request.getSession().getId()); return "redirect:/dashboard"; } }
3. Avoid proxy object confusion in debugging
When injecting a scoped bean like @SessionScope, Spring creates a proxy class to manage the session instance. In IntelliJ's debugger, you might be looking at this proxy instead of the real SessionComponent object.
How to view real values:
- Instead of inspecting fields directly, call the getter methods (e.g.,
session.getUser()) in the debugger—this routes to the actual session-bound instance. - If you want to see fields directly, expand the proxy object in the debugger and look for nested properties like
targetorCGLIB$CALLBACK_0(depending on proxy type) to access the real bean.
4. Confirm correct @SessionScope configuration
Double-check your SessionComponent annotation setup—while the default proxy mode works, being explicit avoids confusion:
@Component @SessionScope(proxyMode = ScopedProxyMode.TARGET_CLASS) // Default, but explicit is safer public class SessionComponent { private String sessionId; private User user; // Getters and setters here }
This proxy mode ensures Spring can inject the scoped bean into singleton beans like AnotherClass without scope conflicts.
内容的提问来源于stack exchange,提问作者jack

