File tree Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,9 @@ dependencies {
28
28
implementation ' org.springframework.boot:spring-boot-starter-web'
29
29
testImplementation ' org.springframework.boot:spring-boot-starter-test'
30
30
testImplementation ' junit:junit:4.13.1'
31
+
31
32
implementation ' jakarta.inject:jakarta.inject-api:2.0.1'
33
+ implementation ' org.springframework.boot:spring-boot-starter-web'
32
34
}
33
35
34
36
tasks. named(' test' ) {
Original file line number Diff line number Diff line change
1
+ package hello .core .common ;
2
+ import jakarta .annotation .PostConstruct ;
3
+ import jakarta .annotation .PreDestroy ;
4
+ import org .springframework .context .annotation .Scope ;
5
+ import org .springframework .stereotype .Component ;
6
+
7
+ import java .util .UUID ;
8
+ @ Component
9
+ @ Scope (value = "request" )
10
+ public class MyLogger {
11
+ private String uuid ;
12
+ private String requestURL ;
13
+ public void setRequestURL (String requestURL ) {
14
+ this .requestURL = requestURL ;
15
+ }
16
+ public void log (String message ) {
17
+ System .out .println ("[" + uuid + "]" + "[" + requestURL + "] " + message );
18
+ }
19
+ @ PostConstruct
20
+ public void init () {
21
+ uuid = UUID .randomUUID ().toString ();
22
+ System .out .println ("[" + uuid + "] request scope bean create:" + this );
23
+ }
24
+ @ PreDestroy
25
+ public void close () {
26
+ System .out .println ("[" + uuid + "] request scope bean close:" + this );
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package hello .core .web ;
2
+ import hello .core .common .MyLogger ;
3
+ import jakarta .servlet .http .HttpServletRequest ;
4
+ import lombok .RequiredArgsConstructor ;
5
+ import org .springframework .beans .factory .ObjectProvider ;
6
+ import org .springframework .beans .factory .annotation .Autowired ;
7
+ import org .springframework .stereotype .Controller ;
8
+ import org .springframework .web .bind .annotation .RequestMapping ;
9
+ import org .springframework .web .bind .annotation .ResponseBody ;
10
+
11
+ @ Controller
12
+ @ RequiredArgsConstructor
13
+ public class LogDemoController {
14
+
15
+ private final LogDemoService logDemoService ;
16
+ private final ObjectProvider <MyLogger > myLoggerProvider ;
17
+ //private final MyLogger myLogger;
18
+ @ RequestMapping ("log-demo" )
19
+ @ ResponseBody
20
+ public String logDemo (HttpServletRequest request ) {
21
+ MyLogger myLogger = myLoggerProvider .getObject ();
22
+ String requestURL = request .getRequestURL ().toString ();
23
+
24
+ myLogger .setRequestURL (requestURL );
25
+ myLogger .log ("controller test" );
26
+ logDemoService .logic ("testId" );
27
+ return "OK" ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package hello .core .web ;
2
+
3
+ import hello .core .common .MyLogger ;
4
+ import lombok .RequiredArgsConstructor ;
5
+ import org .springframework .beans .factory .ObjectProvider ;
6
+ import org .springframework .stereotype .Service ;
7
+ @ Service
8
+ @ RequiredArgsConstructor
9
+ public class LogDemoService {
10
+ private final ObjectProvider <MyLogger > myLoggerProvider ;
11
+ public void logic (String id ) {
12
+ MyLogger myLogger = myLoggerProvider .getObject ();
13
+ myLogger .log ("service id = " + id );
14
+ }
15
+
16
+ }
You can’t perform that action at this time.
0 commit comments