2025/05/12 6

@Component의 진화: @Controller, @Service, @Repository

1. @Component가장 기본적인 스프링 빈 등록 애노테이션@Componentpublic class MyComponent { }컴포넌트 스캔 대상이 되며, 스프링 컨테이너가 관리하는 객체로 등록된다.특별한 의미는 없지만 범용적으로 사용 가능하다. 2. @Controller@Component의 특별한 버전으로 웹 요청을 처리하는 역할@Controllerpublic class ProductController { } MVC 구조에서 컨트롤러 역할클라이언트의 요청을 받고, 응답을 반환주로 @RequestMapping, @GetMapping 등과 함께 사용 3. @Service비즈니스 로직을 처리하는 클래스에 사용@Service@Servicepublic class ProductService { } @Compo..

카테고리 없음 2025.05.12

Spring에서 요청 데이터를 전달하는 3가지 방법

{ "name": "handcream", "price": 15000, "description": "촉촉해요"}1. 쿼리 스트링 (Query String)주소에 데이터를 key=value 형태로 붙여 전달하는 방식.http://localhost:8080/products?name=_____ value의 이름으로 쿼리스트링이 날라온다. @RequestMapping(value = "/product", method = RequestMethod.POST) public void saveProduct(@RequestParam(value="name") String productName){ productService.saveProduct(productName); } 특징@RequestParam..

카테고리 없음 2025.05.12

Controller - Service (객체 생성, 호출)

@RestController//@Controller//@ResponseBodypublic class ProductController { // 상품 조회, 상품 등록 ProductController(){ System.out.println("스프링이 객체 생성하는지 확인"); } @RequestMapping(value = "", method = RequestMethod.GET) public String getProduct(){ ProductService productService = new ProductService(); return productService.getProduct(); }}⚠️ 문제 발견 !getProduct 메소드가 호출..

카테고리 없음 2025.05.12