-
TIL-2024.05.09 - Spring - 000. Spring> Backend/Spring 2024. 5. 10. 07:02
질문
1. Spring 기초에 대해 알려줘
2. Spring 핵심 개념
Spring
1. Spring 프레임 워크란?
- Spring은 엔터프라이즈 애플리케이션 개발을 위한 종합적인 프레임워크
- Spring의 주요 목표는 개발자가 보다 쉽게 자바 애플리케이션을 개발하고, 유지보수하며, 배포할 수 있도록 돕는 것
2. 핵심 개념
- IOC (Inversion of Control - 제어의 역전)
- 객체의 생성과 관리를 개발자가 아닌 프레임워크가 담당하는 것을 의미합니다.
- 이는 애플리케이션의 유연성과 확장성을 높이는 데 도움을 줘, Spring에서 IoC는 주로 Dependency Injection (DI)를 통해 구현
- 의존성 주입(Dependency Injection)**은 객체가 자신이 의존하는 객체를 직접 생성하는 대신, 외부에서 주입받는 방식.
예제
public class Service { private Repository repository; // Constructor injection public Service(Repository repository) { this.repository = repository; } // Setter injection public void setRepository(Repository repository) { this.repository = repository; } }
- AOP (Aspect-Oriented Programming)
- AOP는 관점 지향 프로그래밍을 의미.
- 이는 프로그램의 핵심 비즈니스 로직과 공통 관심사를 분리하여 코드의 재사용성과 유지보수성을 높이는 기법.
- Spring에서는 AOP를 통해 로깅, 트랜잭션 관리, 보안 등의 기능을 애플리케이션에 투명하게 적용
3. 주요 모듈
- Spring Core
- ㅇSpring Core는 IoC와 DI를 제공하는 기본 모듈입니다. 애플리케이션 컨텍스트(Application Context)를 통해 객체를 생성하고, 의존성을 주입하며, 애플리케이션 전반에 걸쳐 관리
- Spring AOP
- Spring Core는 IoC와 DI를 제공하는 기본 모듈입니다. 애플리케이션 컨텍스트(Application Context)를 통해 객체를 생성하고, 의존성을 주입하며, 애플리케이션 전반에 걸쳐 관리
- Spring MVC
- Spring MVC는 웹 애플리케이션 개발을 위한 모듈로 모델-뷰-컨트롤러패턴을 기반으로 하여 웹 요청을 처리
예제
@Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Spring MVC!"); return "hello"; } }
- Spring DATA
- Spring Data는 데이터 액세스 계층을 단순화하는 모듈로 다양한 데이터 저장소(JPA, MongoDB, Cassandra 등)와 통합할 수 있는 공통된 접근 방식을 제공합니다.
public interface UserRepository extends JpaRepository<User, Long> { List<User> findByLastName(String lastName); }
4. Spring 부트란?
- Spring 부트(Spring Boot)는 Spring 프레임워크를 기반으로 한 서브 프레임워크로, 설정을 최소화하고 신속한 애플리케이션 개발을 가능하게 함.
- 스프링 부트를 사용하면 복잡한 XML 설정 없이 간단한 애플리케이션을 빠르게 시작 가능예제
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }