Spring Framework

Hayoung Kim·2020년 7월 8일
0

SpringFramework

목록 보기
1/9

Spring

Framework를 사용하는 이유

  1. 생산성

  2. 유지보수성

  3. 재사용성

  4. 신뢰성

  5. 표준화

  6. 확장성

    하지만, 학습난이도 증가, 자유도 감소, 패키지 용량 증가, 프레임워크 의존 위험 증가 될 수 있다.

Reactive Stack(Spring 5이상) vs. Servlet Stack(Spring 4이하)

  • Reactive stack
    • non-blocking
    • async
    • 속도가 빠르기 때문에 대용량 처리에 유리하다.
    • Spring WebFlux : 소수의 thread로 동시성을 처리하고 하드웨어 자원을 적게 사용해서 스케일링 하는게 필요함
  • Servlet stack
    • blocking
    • sync
    • MVC

Spring 3대 요소

  1. IoC(Inversion of Control) : 프로그래머가 작성한 프로그램이 라이브러리에 의해 호출된다.
    • DL(Dependency Lookup)
    • DI(Dependency Injection)
      • Constructor Injection
      • Field Injection
      • Setter Injection
      • Interface Injection(Method Injection)
  2. PSA(Portable Service Abstraction) : Annotaion을 사용해서 추상화를 시켜서 사용하면 관련된 어떤 객체를 사용하던 모두 사용할 수 있게된다.
    활경의 변화와 관계없이 일관된 방식으로 기술에 접근할 수 있는 추상화 계층 구조이다.
  1. AOP(Aspect Oriented Programming)
    Filter --> Interceptor --> Aspect1, Aspect2, ...
    필요한 부분에 AOP(Aspect)만 추가해서 사용하면 된다.

    *filter : 암복호화, 인코딩 변환, XSS 방어
    *Interceptopr : HTTP 프로토콜 레벨 로그인 체크, 권한 체크
    *Aspect : 비즈니스 로직 관련, 그룹 로깅, 트랜잭션 등 비즈니스 단의 method 보다 세밀한 조정이 필요한 경우 사용

@GetMappgin(path=uri 주소)
path에 uri 주소를 입력하면 해당 주소로 API가 매핑된다.

@RestController
public class StudyController {

	@GetMapping(path = "/sample") //path is uri, post 로 request를 보낸다.
	public String sample() {
		String ret ="Hello world!"; //화면에 text로 보여짐
		
		return ret;
	}
}
<!DOCTYPE html>
<html>
  <head>
      <meta charset="UTF-8">
      <title>html file</title>
  </head>
  <body>
      HELLO WORLD
  </body>
</html>


@Controller
public class StudyController {
	
	@GetMapping(path = "/sample") //path is uri
	public String sample() {
		String ret ="Hello world!";
		
		ret = "helloworld"; //resources template에 생성한 html파일 이름과 같게 생성해야된다.
		return ret;
	}
}

CRUD

	@GetMapping(path = "/sample")
	public String selectSample() {
		String ret ="";

		ret = "helloworld"; 
		return ret;
	}
	@PostMapping(path = "/sample")
	public String insertSample() {
		String ret ="";

		ret = "helloworld"; 
		return ret;
	}
	@PutMapping(path = "/sample") 
	public String updateSample() {
		String ret ="";

		ret = "helloworld"; 
		return ret;
	}
	@DeleteMapping(path = "/sample")
	public String deleteSample() {
		String ret ="";

		ret = "helloworld"; //화면에 text로 보여짐
		return ret;
	}

@SpringBootApplication에서 필요한 자원을 스캔한뒤에 bean으로 생성한다.
DI injection :: @Authwired를 통해 bean을 할당함.

Component :: service | controller | repositry

lifeCycle

생성자가 호출된 후에 @PostConstruct가 바로 호출된다.
종료되기 직전에는 @PreDestroy

따라서 @PostContructor로 선언된 메소드에서 @Authwired된 객체를 print해보면 객체가 메모리에 할당된 것을 확인할 수 있다.

	StudyService studyServicce;
	
	@Autowired
	public StudyController(StudyService service) {
		this.studyServicce = service;
	}

생성자의 전달인수로 할당 받게 하는게 권장된다.

0개의 댓글