Skip to content

Commit

Permalink
고급편/섹션4: 예제 프로젝트 만들기 v1 (#5)
Browse files Browse the repository at this point in the history
섹션4: 프록시 패턴과 데코레이터 패턴
  • Loading branch information
toychip committed Mar 5, 2024
1 parent 8ad4885 commit 0fae170
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package hello.proxy;

import hello.proxy.config.AppV1Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication(scanBasePackages = "hello.proxy.app") //주의
@Import(AppV1Config.class)
@SpringBootApplication(scanBasePackages = "hello.proxy.app")
// 주의 config 하위 클래스 AppV1Config 등을 컴포넌트 스캔 대상에 두지 않기 위해 작업
public class ProxyApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hello.proxy.app.v1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping // Spring은 @Controller 또는 @RequestMapping이 있어야 스프링 컨트롤러로 인식
@ResponseBody
public interface OrderControllerV1 {

@GetMapping("/v1/request")
String request(@RequestParam("itemId") String itemId);

@GetMapping("/v1/no-log")
String noLog();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hello.proxy.app.v1;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class OrderControllerV1Impl implements OrderControllerV1 {

private final OrderServiceV1 orderService;

@Override
public String request(final String itemId) {
orderService.orderItem(itemId);
return "ok";
}

@Override
public String noLog() {
return "noLog";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello.proxy.app.v1;

public interface OrderRepositoryV1 {
void save(String itemId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hello.proxy.app.v1;

public class OrderRepositoryV1Impl implements OrderRepositoryV1 {
@Override
public void save(final String itemId) {
// 저장 로직
if (itemId.equals("ex")) {
throw new IllegalStateException("예외 발생");
}
sleep(1000);
}

private void sleep(final int mills) {
try {
Thread.sleep(mills);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello.proxy.app.v1;

public interface OrderServiceV1 {
void orderItem(String itemId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hello.proxy.app.v1;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class OrderServiceV1Impl implements OrderServiceV1 {

private final OrderRepositoryV1 orderRepository;

@Override
public void orderItem(final String itemId) {
orderRepository.save(itemId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hello.proxy.config;

import hello.proxy.app.v1.OrderControllerV1;
import hello.proxy.app.v1.OrderControllerV1Impl;
import hello.proxy.app.v1.OrderRepositoryV1;
import hello.proxy.app.v1.OrderRepositoryV1Impl;
import hello.proxy.app.v1.OrderServiceV1;
import hello.proxy.app.v1.OrderServiceV1Impl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppV1Config {

/* Bean 수동 등록 */

@Bean
public OrderControllerV1 orderControllerV1() {
return new OrderControllerV1Impl(orderServiceV1());
}

@Bean
public OrderServiceV1 orderServiceV1() {
return new OrderServiceV1Impl(orderRepositoryV1());
}

@Bean
public OrderRepositoryV1 orderRepositoryV1() {
return new OrderRepositoryV1Impl();
}
}

0 comments on commit 0fae170

Please sign in to comment.