본문 바로가기
JAVA/Spring

1. spring core 모듈

by sihyeong 2025. 7. 2.

Spring Framework는 여러가지 모듈을 가진다.

 

그 중 spring core 모듈은 Spring Framework의 가장 낮은 수준의 기본 기능을 제공하는 모듈로

Spring 전체 기반을 구성하는 핵심 유틸리티 및 인프라 코드를 가진 핵심 기반 모듈이다.

 

이 모듈은 Spring의 나머지 모든 모듈이 의존하는 공통 기능을 담고 있다.

주로 프레임워크 내부 동작, 유틸리티 제공, 환경 추상화, 타입 변환, 리소스 추상화 등의 기능을 제공한다.

주요 기능

1. 리소스(Resource) 추상화

- 클래스패스, 파일 시스템, URL 등의 리소스를 일관된 방식으로 다룰 수 있게 하는 추상화 기능 제공

- Spring에서는 모든 리소스를 org.springframework.core.io.Resource 인터페이스로 추상화하여 처리한다.
즉, 파일이 classpath:에 있든, file: 시스템에 있든, http:로 접근하든 동일한 방식으로 사용할 수 있다.

 

* org.springframework.core.io.Resource 인터페이스의 주요 구현 클래스

구현 클래스 설명
ClassPathResource 클래스패스 상의 리소스를 읽을 때 사용 (resources/)
FileSystemResource 로컬 파일 시스템의 파일을 읽을 때 사용
UrlResource HTTP / FTP 등 URL 리소스를 읽을 때 사용
InputStreamResource InputStream을 직접 감싸는 용도 (스트림 기반 리소스 처리용)

 

import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.UrlResource;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ResourceExample {

    public static void main(String[] args) throws Exception {
        // 1. Classpath 리소스 읽기
        Resource classpathResource = new ClassPathResource("data/config.txt");
        printResourceContent("Classpath Resource", classpathResource);

        // 2. File 시스템 리소스 읽기 (절대 경로)
        Resource fileSystemResource = new FileSystemResource("C:/temp/config.txt");
        printResourceContent("File System Resource", fileSystemResource);

        // 3. URL 리소스 읽기
        Resource urlResource = new UrlResource("https://www.example.com/sample.txt");
        printResourceContent("URL Resource", urlResource);
    }

    private static void printResourceContent(String label, Resource resource) throws Exception {
        System.out.println("==== " + label + " ====");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
            reader.lines().forEach(System.out::println);
        }
    }
}

 

* ApplicationContext를 통한 리소스 로딩

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Resource resource = context.getResource("classpath:data/config.txt");

2. Environment 및 Property 추상화

- 외부 설정 값(프로퍼티 파일, 시스템 환경 변수 등)을 처리하는 기능

- 인터페이스 : Environment, PropertyResolver

Environment env = applicationContext.getEnvironment();
String dbUrl = env.getProperty("db.url");

- Profiles 지원으로 개발 / 운영 환경별 구성을 분리 가능

@Profile("dev")
@Bean
public DataSource devDataSource() { ... }

3. 타입 변환 시스템 (Type Conversion System)

- 문자열 <--> 객체 간의 변환 기능

- @RequestParam, @ModelAttribute, @PathVariable을 통해 타입 자동 변환 후 바인딩

Converter<String, LocalDate> converter = source -> LocalDate.parse(source);

4. 코어 유틸리티 클래스 (Core Utilities)

- 컬렉션, 문자열, 날짜 등 다양한 유틸리티 클래스 포함

- 대표적인 유틸리티 클래스 :

  • StringUtils
  • ObjectUtils
  • CollectionUtils
  • Assert
  • ClassUtils, ReflectUtils, ResourceUtils

5. 인터페이스 기반 프로그래밍 지원

- Ordered, PriorityOrdered 인터페이스:

  • 빈 처리 우선순위 지정에 사용

- Aware 계열 인터페이스

BeanNameAware, ApplicationContextAware 등

6. 스프링 내부 인프라 클래스

- 프레임워크 내부에서 쓰이는 다양한 지원 클래스 제공

 

 

 

요약

기능 영역 설명
리소스 추상화 파일, URL, 클래스패스 등 다양한 리소스 처리
환경 및 프로퍼티 처리 Environment, @Profile, 외부 설정 주입
타입 변환 Converter, ConversionService, PropertyEditor
유틸리티 클래스 문자열, 컬렉션, 리플렉션 등 다양한 지원 도구
인프라 클래스 Spring 내부에서 사용되는 공통 구조 클래스
순서 지정 Ordered, PriorityOrdered 인터페이스

 

'JAVA > Spring' 카테고리의 다른 글

2. spring beans 모듈  (0) 2025.07.03
메시지, 국제화 LocaleResolver 구현  (2) 2023.12.05
메시지, 국제화  (0) 2023.12.04