Java, Spring, 그리고 Spring Boot 개발 시 권장되는 코딩 컨벤션(Coding Conventions)을 정리한 것입니다. 각 조직이나 팀마다 세부 사항이 다를 수 있지만, 다음 가이드라인을 참고하면 표준적이고 일관성 있는 코드를 작성할 수 있습니다.
패키지명
com.example.myapp
클래스/인터페이스명
public class UserService
, public interface Runnable
메서드명
getUserById()
, calculateTotalPrice()
변수명
int userCount
, String firstName
상수명
_
) 사용. public static final int MAX_SIZE = 100;
public void example() {
// ...
}
int sum = a + b;
if
, for
등의 키워드 뒤에 공백: if (condition) { ... }
/**
* 사용자 정보를 반환합니다.
*
* @param id 사용자 ID
* @return 사용자 객체
*/
public User getUserById(Long id) { ... }
// 사용자 정보를 DB에서 조회
User user = userRepository.findById(id);
try-catch
시 구체적인 메시지 로그.일반적으로 다음과 같은 계층형 아키텍처를 사용:
com.example.myapp
├─ controller (또는 web)
├─ service
├─ repository
├─ domain (또는 model, entity)
└─ config
Controller 클래스
@RestController
또는 @Controller
사용.@RequestMapping
(클래스 레벨) 또는 @GetMapping
, @PostMapping
(메서드 레벨)로 선언.Service 클래스
@Service
사용.@Transactional
부여.Repository 클래스
@Repository
사용 또는 JPA에서는 CrudRepository
, JpaRepository
등을 상속.Entity 클래스
@Entity
, @Table
사용.@Id
, @Column
으로 매핑.생성자 주입 권장:
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
// ...
}
필드 주입은 테스트나 유지보수 관점에서 비추천.
Setter 주입은 선택적인 의존성에만 사용.
Spring Boot는 Spring을 보다 간편하게 사용할 수 있도록 도와주는 프레임워크이며, 다음 사항을 지키면 좋습니다.
com.example.demo
├─ DemoApplication.java (main class)
├─ controller/
├─ service/
├─ repository/
├─ domain/
└─ config/
DemoApplication
)는 루트 패키지에 위치해 컴포넌트 스캔이 패키지 하위까지 적용되도록 함.application.properties
또는 application.yml
파일에 환경별 설정(profiles) 구분 (application-dev.properties
, application-prod.properties
등).@ConfigurationProperties
를 사용해 명시적으로 관리.@Configuration
클래스로 추가 설정.application.properties
에서 로깅 레벨 설정:logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
@Controller
, @Service
, @Repository
)을 사용합니다.이러한 컨벤션을 따르면 프로젝트의 가독성, 유지보수성, 그리고 협업 효율성이 높아지며, 일관된 코드를 작성할 수 있습니다.
추가 학습 자료