본문 바로가기

2022동계모각코

2022 동계모각코 6회차 개인 목표 및 결과

일시

2022-02-015 14:00~18:00

목표

이때까지 모각코를 통하여 공부한 것을 한번 정리하는 시간을 가진다

 

결과

하는동안 잘 숙지 되지 않았던 싱글톤 패턴과 같은 것들을 다시 한번 보고 앞으로의 공부 방향성을 잡을 수 있었다

싱글톤 패턴 적용

package hello.core.singleton;
public class SingletonService {

private static final SingletonService instance = new SingletonService();

허용한다.
public static SingletonService getInstance() {
return instance;
}

private SingletonService() {
}
public void logic() {
System.out.println("싱글톤 객체 로직 호출");
}
}
@Test
@DisplayName("싱글톤 패턴을 적용한 객체 사용")
public void singletonServiceTest() {

//new SingletonService();

SingletonService singletonService1 = SingletonService.getInstance();

SingletonService singletonService2 = SingletonService.getInstance();

System.out.println("singletonService1 = " + singletonService1);
System.out.println("singletonService2 = " + singletonService2);
// singletonService1 == singletonService2
assertThat(singletonService1).isSameAs(singletonService2);
singletonService1.logic();
}

프록시 방식

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyLogger {
}
package hello.core.web;
import hello.core.common.MyLogger;
import hello.core.logdemo.LogDemoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequiredArgsConstructor
public class LogDemoController {
private final LogDemoService logDemoService;
private final MyLogger myLogger;
@RequestMapping("log-demo")
@ResponseBody
public String logDemo(HttpServletRequest request) {
String requestURL = request.getRequestURL().toString();
myLogger.setRequestURL(requestURL);
myLogger.log("controller test");
logDemoService.logic("testId");
return "OK";
}
}

package hello.core.logdemo;
import hello.core.common.MyLogger;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class LogDemoService {
private final MyLogger myLogger;
public void logic(String id) {
myLogger.log("service id = " + id);
}
}