Spring (43) 썸네일형 리스트형 스프링 빈 라이프 사이클(Spring Bean Life Cycle) 스프링 빈은 스프링컨테이너에 의해서 관리된다. 프로그램이 실행되면서 스프링 컨테이너가 시작되고, 컨테이너의 요청에 따라 빈의 인스턴스를 생성하고 의존성을 주입한다. 스프링 컨테이너가 닫히면 빈이 파괴된다. init(), destroy() 메소드 대신 사용자 정의 메소드 이름을 선택할 수 있다. 빈이 인스턴스화될 때 init메소드를 실행하고 컨테이너를 닫을 때 destroy()메소드를 실행한다. 빈 라이프 사이클 빈 객체 생성 ⬇️ 빈 프로퍼티 설정 ⬇️ BeanNameAware.setBeanName() ⬇️ ApplicationContextAware.setApplicationContect() ⬇️ BeanPostProcessor 초기화 전처리 postProcessBeforeInitialization ⬇️.. Spring ResourceHttpRequestHandler ☝ ResourceHttpRequestHandler 정적 리소스를 처리하는 작업을 담당한다. 유연한 캐시설정을 허용해서 브라우저 성증에 최적화된 정적 리소스를 제공한다. 요청에 대한 리소스가 있는지 확인한 후 처음으로 발견한 리소스를 Expires와 헤더와 함께 리턴한다. Cache-Controll, Last-Modified를 적절하게 평가해서 이미 캐시한 리소스에 대해 불필요한 오버헤드를 방지한다. /static, /public, /resources, /META-INF/resources 디렉터리에 있는 정적컨텐츠를 사용한다. ☝ Custom Path Patterns 기본적으로 스프링은 모든 정적 컨텐츠를 루트 밑에 제공하고 있다. 기본 구성이 좋은 것 같지만 설정을 변경할 수 있다. spring.mvc.. Spring MVC 동작 방식 (DispatcherServlet) ❓ MVC모듈 서블릿 기반 웹 어플리케이션에 사용할 수 있는 프레임워크로 객체 사이의 관심사를 분리할 수 있는 프레임워크이다. 컨트롤러 : 요청을 처리 검증기(Validator) : 검증을 수행 모델 : 데이터를 저장 🔘기본적인 동작방식 client에서 요청이 드들어오면 맵핑되는 Controller를 호출한다. 컨트롤러에서 ModelAndView객체를 리턴하면 뷰이름에 해당하는 페이지를 리턴한다. 스프링 웹 MVC는 SimpleUrlHandlerMapping과 InternalResourceViewResolver 빈을 자동으로 감지해서 요청을 처리하는 컨트롤러와 뷰를 찾는다. 🔘 DispatcherServlet 스프링 웹 MVC기반 어플리케이션에서는 요청을 DispatcherSetvlet이 가로챈다. (요.. Spring boot thymeleaf 로 Error페이지 처리 🔍 모든 에러를 다 잡아낼 수 없기 때문에 Spring에서 ErrorController를 구현해서 에러페이지를 처리할 수 있다. package org.springframework.boot.web.servlet.error; import org.springframework.stereotype.Controller; /** * Marker interface used to identify a {@link Controller @Controller} that should be used * to render errors. * * @author Phillip Webb * @author Scott Frederick * @since 2.0.0 */ public interface ErrorController { } @Cont.. Spring ExceptionHandler & ControllerAdvice 💡 SpringBoot가 제공하는 ExceptionResolver 1. ExceptionHandlerExeptionResolver @ExceptionHandler 처리 > API 예외처리 2. ResponseStatusExceptionResolver @ResponseStatus(value=HttpStatus.NOT_FOUND) Http 상태코드를 지정해준다. 3. DefaultHandlerExceptionResolver 스프링 내부 기본 예외를 처리한다. ☝ ExceptionHandler @Target ( 값 = METHOD ) @Retention ( 값 = RUNTIME ) @Documented public @interface ExceptionHandler 특정 핸들러 클래스 또는 핸들러 메서드에서 예.. Spring Boot QR코드 이미지 생성 해서 thymeleaf로 보여주기 QR코드 생성을 위한 라이브러리 추가 pom.xml com.google.zxing javase 3.1.0 Controller.java @RequestMapping("/getQrcode") public String getQrcode(Model model) throws Exception { String img = getQRCodeImage(result.getData().getQrCode(), 200, 200); model.addAttribute("img", img); return "qrcode"; } // QR코드 이미지 생성 public static String getQRCodeImage(String text, int width, int height) throws WriterException, IOExce.. resilience4j circuit breaker test (with spring boot) Resilience4j 란? hystrix로 부터 영감을 받은 경량 fault tolerance library. 다른 외부 라이브러리 종속성이 없기 때문에 가볍게 사용하기 좋다. Circuit Breaker / Rate Limiter / Bulk Head / Retry / Cache / Time Limiter 구현체가 존재. hystrix는 더이상 개발되지 않고 maintenance만 하겠다고 해서 많이 사용하는 추세이다. resilience4j.readme.io/ resilience4j resilience4j.readme.io build.gradle implementation 'io.github.resilience4j:resilience4j-spring-cloud2:1.6.1' implementatio.. SpringBoot 외부 프로퍼티 파일로 실행하기 (application.properties) SpringBoot는 application.properties를 외부 프로퍼티를 이용해서 실행할 수 있다. jar 실행방법 java -jar spring-boot.jar 프로퍼티의 이름을 바꿔서 실행할 경우(spring.config.name) java -jar spring-boot.jar --spring.config.name=./bootStrap.properties 프로퍼티의 location을 지정해줄 경우 ava -jar board-0.0.1-SNAPSHOT.jar --spring.config.location=./config/ config 폴더 아래에있는 프로퍼티 파일을 찾아서 실행한다. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___.. 이전 1 2 3 4 5 6 다음