Spring MVC is designed to be composable, so it integrates cleanly with data-access technologies like JPA and messaging technologies like WebSocket rather than trying to handle those concerns itself. Controllers stay focused on HTTP while Spring Data JPA handles persistence and Spring's WebSocket support handles bidirectional, real-time communication.
Key Points: • Spring Data JPA lets controllers call repository interfaces for CRUD operations without hand-written DAO code. • @Transactional on service methods keeps persistence logic consistent and decoupled from the web layer. • @EnableWebSocket plus a WebSocketConfigurer registers WebSocket handlers alongside normal MVC endpoints. • STOMP over WebSocket (via @EnableWebSocketMessageBroker) adds a messaging abstraction for pub/sub style real-time features like chat or live dashboards. • Because all of this runs in the same Spring container, the same dependency injection and configuration model applies everywhere.
Example: A stock ticker application could use a @RestController backed by Spring Data JPA to serve historical price data, while a separate WebSocket endpoint pushes live price updates to connected browsers without the client needing to poll.
Code Example:
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new PriceUpdateHandler(), "/ws/prices")
.setAllowedOrigins("*");
}
}Interview Tip: A concise interview answer is:
"Spring MVC integrates with JPA through Spring Data repositories for persistence, and with WebSocket through @EnableWebSocket and a WebSocketConfigurer, or STOMP messaging for pub/sub features. Since everything lives in the same Spring container, dependency injection and configuration stay consistent across the web, data, and messaging layers."