반응형

개발/Spring 3

스프링Bean과 의존성 주입(DI)

의존성 주입이란? 필요한 객체를 함수 내에서 직접 생성해서 사용하면, 다른 객체로 변경하고 싶은 경우에 소스를 수정해야 하는 번거로운 일이 발생한다. 이러한 현상을 방지하기 위해 의존성 주입을 통해, 객체 간의 결합도를 낮추고 수정을 용이하게 해 준다. DI 방식 3가지 방식이 있지만 실행 중 변경될 일이 없기 때문에, 생성자로 1번만 실행하고 변경이 되지 않게 하는 게 좋다. 1) 필드 주입 @Autowired private MemeberService memberService; 2) Setter 방식 @Autowired private MemeberService memberService; public setMemberService(MemeberService memberService){ this.membe..

개발/Spring 2023.01.17

Spring Boot에서 JUnit 사용

JUnit이란? 자바 테스트 tool로 'org.springframework.boot:spring-boot-starter-test'에 의존성에 포함 /test 폴더에서 작업 예제 package hello.hellospring.service; import hello.hellospring.domain.Member; import hello.hellospring.repository.MemoryMemberRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.As..

개발/Spring 2023.01.17

Spring Boot 동작 원리

1. 동적페이지 (resources/templates) // http://localhost:8080/hello @Controller public class HelloController { @GetMapping("hello") public String hello(Model model) { model.addAttribute("data", "hello!!"); return "hello"; } } 1. 사용자가 Request시, 내장 Tomcat이 스프링 컨테이너에서 hello가 Mapping 되어 있는 Controller를 찾는다. -> 있음 2. mapping되어 있는 함수 호출 후, Return 처리 3. Return이 문자열인 경우, ViewResolver를 통해 resources/templates/에서..

개발/Spring 2023.01.13
반응형