분류 전체보기 63

@SpringBootApplication 역할 나누기

@SpringBootApplication 역할 나누기□ @SpringBootApplication 역할 클래스를 두개 이상으로 쪼갤 수 있다(단, main 메서드는 쪼개서 사용할 수 없음)main 메서드 외 역할 = @SpringBootApplication그러므로 메인기능을 하는 클래스와 ComponentScan 역할만 할 수 있음 § 컴포넌트 스캔 기능과 메인메서드 기능을 나눈 모습   ※ 참고로 DI대상 객체의 생명주기 (디자인)을 @SpringBootApplication 클래스에서 설정할 수 있다.@SpringBootApplication@ComponentScan(basePackages = "com.example.ex1")public class Ex1Application2 { A target1; @B..

SPRING 2024.12.10

[Rest API]퍼머링크 형식와 일반적인 URL 형식

일반적인 URL 형식Permanent Link 형태( 퍼머링크형태 ) URL 모양 http://localhost/countryList?currentPage=10 http://localhost/countryList/1 (요청값이 1개일때)http://localhost/countryList/1/2/3 (요청값이 3개일때)파라미터값 요청방식 @RequestParam @PathVariable매핑@GetMapping("/countryList1")@GetMapping("/countryList2/{currentPage}")특징요청값 부분과 URL이 명확히 분리되고, 파라미터를 옵션으로 관리가 가능하다.요청주소가 변수값이 들어오는 ?부분 전 까지이다.클라이언트에게 호출 인터페이스를 편하게 보여주기 위함요청주소가 변수..

SPRING 2024.12.10

@Scheduled / @Validation / View Mapping 방법

@Scheduled 스케쥴링 코드  ex - 회원탈퇴 후 6개월동안 개인정보를 가지고있다가 기간이 지나면 자동삭제특정 시점에 자동으로 메서드가 실행되는 기능이 필요할 때 @Scheduled와 @inableScheduling 애노테이션을 사용하면 된다. (예제는 보통 쿼리 실행을 할 때 사용하므로 @Service 선언을 하였음) package com.example.ex1;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class MySchedule { @Scheduled(cron = "0 0 7 1 * *")// 매달 1일날 오전 7시에 해당..

SPRING 2024.12.10

[Rest API] JSON과 XML로 반환 방법

Rest API에서는 표준화된 VO 타입 필요하다.자바에는 익명 객체가 없으므로 MAP을 사용하는데, 이는 클라이언트 등 호출받는 쪽에서 JSON / XML 문자열로 반환된다.  JSON과 XML로 반환 과정  @RestControllerpublic class UserRest { @GetMapping( path = "/userJson", produces = MediaType.APPLICATION_JSON_VALUE) public List> userJson() { List> list = new ArrayList(); Map u1 = new HashMap(); Map u2 = new HashMap(); Map u3 = new HashMap(); u1...

SPRING 2024.12.10

[백준 2439-별찍기] String 배열을 초기화 안하면 생기는 문제

import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); String[] star = new String[a]; for (int i = 0; i 5 null11111 null11111 null11111 null11111 null11111  star[] 배열을 초기화하지 않으면 String 디폴트 값인 null이 배열에 들어간다 star[i] += "1"; 하는 순간 null += "1"; 을 한셈String 배열을 반복문에서 사용해야 하는 경우 초기화..

알고리즘 2024.12.09

[백준 8393] n이 주어졌을 때, 1부터 n까지 합 반복문없이

합의 공식 n×(n+1)​ / 2 이용 ※ 반복문 있이 풀었을 때import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = 0; for (int i = 1; i  ※ 반복문 없이 풀었을 때import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); in..

알고리즘 2024.12.09

DI / Bean(@ComponentScan)

스프링 빈을 등록하는 방법은 두가지   ① (@Component)로 자바 클래스들을 설정하고 component scan 으로 자동 스프링빈에 등록되게 끔한다.        ※ component scan 범위는 @SpringBootApplication 패키지 하위 범주 내 이고, 디폴트로 싱글톤 패턴으로 빈에 등록 ② 자바 코드로 직접 스프링 빈에 등록하기DI 방법은 세가지 ① 필드 주입 : Java Reflection API를 사용하면, 접근 제한자를 무시하고 필드에 직접 접근할 수 있다. Spring Framework는 이를 이용하여 private으로 선언된 필드에서도 의존성을 주입할 수 있는 것" 필드 주입은 의존성을 주입받는 클래스의 필드에 직접 의존성을 주입하는 방법입니다. 이 방법은 코드가 간결..

SPRING 2024.12.09

Filter / Interceptor / AOP

Filter / Interceptor / AOP 필터 서블릿을 가로챔서블릿 API : 메서드가 실행되기 전에 사용되는 방식 (웹서버)인터셉터 컨트롤러를 가로챔 Spring API (스프링)AOP메서드를 가로챔 외부 라이브러리 사용방법@Filter1. springBootApplication 에서 서블릿 API를 사용하겠다 선언 ( ServletComponentScan )    ※ @ServletComponentScan // 서블릿 API를 사용한다고 선언 - 서블릿 관련 애노테이션을 스캔한다.    ※ @WebServlet @Filter @WebListener 주로 모델2 작업시 사용한 애노테이션을 사용하려면        @ServletComponentScan을 선언해야지만 사용할 수 있다. @Spring..

SPRING 2024.12.09

POST-Redirect-GET (PRG) 패턴

RedirectAttributes 개념리다이렉션 수행시, 한 컨트롤러 메서드에서 다른 컨트롤러 메서드로 Attributes를 전달하는데 이용한다. 데이터 저장시에는 redirectAttributes.addAttribute() 또는 redirectAttributes.addFlashAttributes()를 사용한다.주문이 완료된 후 주문 결과 상세 페이지로 Redirect하고, 그 결과를 보여주고 싶을때, 주문 처리가 끝났을 때 생성된 주문번호를 리 다이렉트 페이지로 넘길 수 있다.  -> 위와 같이 노출해도 상관없는 정보를 넘길때는 redirectAttributes.addAttribute()로 전달한다. 리다이렉트 시, 노출하기 싫은 정보를 숨길때 addFlashAttributes로 Session을 사용하..

SPRING 2024.11.30