spring core : web scope
This commit is contained in:
@@ -14,6 +14,7 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter'
|
implementation 'org.springframework.boot:spring-boot-starter'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'javax.inject:javax.inject:1'
|
implementation 'javax.inject:javax.inject:1'
|
||||||
|
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.example.basic.common;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.context.annotation.ScopedProxyMode;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||||
|
public class MyLogger {
|
||||||
|
|
||||||
|
private String uuid;
|
||||||
|
private String requestURL;
|
||||||
|
|
||||||
|
public void setRequestURL(String requestURL) {
|
||||||
|
this.requestURL = requestURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log(String message) {
|
||||||
|
System.out.println("[" + uuid + "] " + "[" + requestURL + "] " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
uuid = UUID.randomUUID().toString();
|
||||||
|
System.out.println("[" + uuid + "] request scope bean create: " + this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void close() {
|
||||||
|
System.out.println("[" + uuid + "] request scope bean close: " + this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.basic.web;
|
||||||
|
|
||||||
|
import com.example.basic.common.MyLogger;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
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 ObjectProvider<MyLogger> myLoggerProvider;
|
||||||
|
private final MyLogger myLogger;
|
||||||
|
|
||||||
|
@RequestMapping("log-demo")
|
||||||
|
@ResponseBody
|
||||||
|
public String logDemo(HttpServletRequest request) {
|
||||||
|
String requestURL = request.getRequestURL().toString();
|
||||||
|
// MyLogger myLogger = myLoggerProvider.getObject();
|
||||||
|
myLogger.setRequestURL(requestURL);
|
||||||
|
|
||||||
|
myLogger.log("controller test");
|
||||||
|
logDemoService.logic("testId");
|
||||||
|
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.basic.web;
|
||||||
|
|
||||||
|
import com.example.basic.common.MyLogger;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class LogDemoService {
|
||||||
|
|
||||||
|
// private final ObjectProvider<MyLogger> myLoggerProvider;
|
||||||
|
private final MyLogger myLogger;
|
||||||
|
|
||||||
|
public void logic(String id) {
|
||||||
|
// MyLogger myLogger = myLoggerProvider.getObject();
|
||||||
|
myLogger.log("service id = " + id);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user