Skip to content

Commit 82737d0

Browse files
committed
code-smell: replaced setter injection with constructor injection for better immutability
1 parent 3f6df57 commit 82737d0

20 files changed

+68
-89
lines changed

src/main/java/com/connect/config/WebSocketConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.connect.service.JwtTokenService;
44
import com.connect.security.StompPrincipal;
5+
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.beans.factory.annotation.Value;
@@ -23,13 +24,13 @@
2324
@Configuration
2425
@EnableWebSocketMessageBroker
2526
@Slf4j
27+
@RequiredArgsConstructor
2628
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
2729

2830
@Value("${frontend.origin}")
2931
private String FRONTEND_URL;
3032

31-
@Autowired
32-
private JwtTokenService service;
33+
private final JwtTokenService service;
3334

3435
@Override
3536
public void configureMessageBroker(MessageBrokerRegistry config) {

src/main/java/com/connect/controller/AuthController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import com.connect.dto.LoginUserDTO;
66
import com.connect.dto.OtpDTO;
7+
import lombok.RequiredArgsConstructor;
78
import lombok.extern.slf4j.Slf4j;
8-
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
1010
import org.springframework.http.ResponseEntity;
1111
import org.springframework.web.bind.annotation.GetMapping;
@@ -21,10 +21,10 @@
2121
@RestController
2222
@RequestMapping("/auth")
2323
@Slf4j
24+
@RequiredArgsConstructor
2425
public class AuthController {
2526

26-
@Autowired
27-
private AuthService authService;
27+
private final AuthService authService;
2828

2929
// Handler method for handling the signup functionality.
3030
@PostMapping("/signup")

src/main/java/com/connect/controller/ChatRoomController.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
import com.connect.service.ChatUserService;
88
import com.connect.service.JwtTokenService;
99
import com.connect.service.RoomService;
10-
import com.connect.utils.JwtUtil;
1110
import jakarta.servlet.http.HttpServletRequest;
12-
import lombok.AllArgsConstructor;
11+
import lombok.RequiredArgsConstructor;
1312
import lombok.extern.slf4j.Slf4j;
14-
import org.springframework.beans.factory.annotation.Autowired;
1513
import org.springframework.http.HttpStatus;
1614
import org.springframework.http.ResponseEntity;
1715
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -26,16 +24,13 @@
2624

2725
@RestController
2826
@Slf4j
29-
@AllArgsConstructor
27+
@RequiredArgsConstructor
3028
public class ChatRoomController {
3129

32-
private ChatUserService chatUserService;
33-
34-
private RoomService roomService;
35-
36-
private JwtTokenService jwtTokenService;
37-
38-
private SimpMessagingTemplate messagingTemplate;
30+
private final ChatUserService chatUserService;
31+
private final RoomService roomService;
32+
private final JwtTokenService jwtTokenService;
33+
private final SimpMessagingTemplate messagingTemplate;
3934

4035
// Route for handling the joining message.
4136
@MessageMapping("/chat.join")

src/main/java/com/connect/controller/ChatUserController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import com.connect.dto.MessageDTO;
44
import com.connect.dto.UserDTO;
5-
import com.connect.model.Message;
6-
import com.connect.model.User;
75
import com.connect.service.ChatUserService;
6+
import lombok.RequiredArgsConstructor;
87
import lombok.extern.slf4j.Slf4j;
98
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -14,7 +13,6 @@
1413
import org.springframework.web.bind.annotation.RestController;
1514

1615
import java.util.List;
17-
import java.util.Optional;
1816
import java.util.stream.Collectors;
1917

2018
// ChatUserController
@@ -23,13 +21,11 @@
2321

2422
@RestController
2523
@Slf4j
24+
@RequiredArgsConstructor
2625
public class ChatUserController {
2726

28-
@Autowired
29-
private ChatUserService chatUserService;
30-
31-
@Autowired
32-
private SimpMessagingTemplate messagingTemplate;
27+
private final ChatUserService chatUserService;
28+
private final SimpMessagingTemplate messagingTemplate;
3329

3430
@MessageMapping("/greet")
3531
public void handleGreeting(SimpMessageHeaderAccessor headerAccessor) {

src/main/java/com/connect/controller/TokenController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package com.connect.controller;
22

33
import com.connect.service.JwtTokenService;
4-
import com.connect.utils.JwtUtil;
4+
import lombok.RequiredArgsConstructor;
55
import lombok.extern.slf4j.Slf4j;
6-
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.http.HttpStatus;
87
import org.springframework.http.ResponseEntity;
98
import org.springframework.web.bind.annotation.*;
109

11-
import java.util.Map;
1210
import java.util.Optional;
1311

1412
// Controller for handling the Verify Token Request
1513

1614
@RestController
1715
@RequestMapping("/api")
1816
@Slf4j
17+
@RequiredArgsConstructor
1918
public class TokenController {
2019

21-
@Autowired
22-
private JwtTokenService tokenService;
20+
private final JwtTokenService tokenService;
2321

2422
@GetMapping("/verify-token")
2523
public ResponseEntity<?> verifyToken(@RequestHeader("Authorization") String authHeader) {

src/main/java/com/connect/dto/RoomDTO.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import lombok.NoArgsConstructor;
77
import lombok.Setter;
88

9-
import java.time.LocalDateTime;
10-
119
@Getter
1210
@Setter
1311
@NoArgsConstructor

src/main/java/com/connect/kafka/KafkaListenerService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import com.connect.buffer.MessageBuffer;
44
import com.connect.model.Message;
5+
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
6-
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.kafka.annotation.KafkaListener;
88
import org.springframework.stereotype.Service;
99

1010
@Service
1111
@Slf4j
12+
@RequiredArgsConstructor
1213
public class KafkaListenerService {
1314

14-
@Autowired
15-
private MessageBuffer messageBuffer;
15+
private final MessageBuffer messageBuffer;
1616

1717
@KafkaListener(topics = "chat", groupId = "chat-group")
1818
public void consumeEvent(Message message) {

src/main/java/com/connect/kafka/KafkaPublisherService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.connect.kafka;
22

33
import com.connect.model.Message;
4+
import lombok.RequiredArgsConstructor;
45
import lombok.extern.slf4j.Slf4j;
5-
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.kafka.core.KafkaTemplate;
77
import org.springframework.kafka.support.SendResult;
88
import org.springframework.stereotype.Service;
@@ -11,10 +11,10 @@
1111

1212
@Service
1313
@Slf4j
14+
@RequiredArgsConstructor
1415
public class KafkaPublisherService {
1516

16-
@Autowired
17-
private KafkaTemplate<String, Object> kafkaTemplate;
17+
private final KafkaTemplate<String, Object> kafkaTemplate;
1818

1919
public void sendEvent(Message message) {
2020
log.info("Message received: {}", message.toString());

src/main/java/com/connect/model/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
@ToString
1616
@Document(collection = "messages")
1717
public class Message {
18+
1819
@Id
1920
private ObjectId id;
20-
2121
private String roomId;
2222
private String sender;
2323
private String message;

src/main/java/com/connect/repository/RoomRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.connect.model.Message;
44
import com.connect.model.Room;
5+
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
6-
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.data.mongodb.core.MongoTemplate;
88
import org.springframework.data.mongodb.core.query.Criteria;
99
import org.springframework.data.mongodb.core.query.Query;
@@ -14,10 +14,10 @@
1414

1515
@Repository
1616
@Slf4j
17+
@RequiredArgsConstructor
1718
public class RoomRepository {
1819

19-
@Autowired
20-
private MongoTemplate mongoTemplate;
20+
private final MongoTemplate mongoTemplate;
2121

2222
public Optional<Room> addRoom(Room room) {
2323
return Optional.of(mongoTemplate.insert(room));

0 commit comments

Comments
 (0)