#35 springboot: custom auto config init
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package hello.config;
|
||||
|
||||
import memory.MemoryController;
|
||||
import memory.MemoryFinder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MemoryConfig {
|
||||
|
||||
@Bean
|
||||
public MemoryController memoryController() {
|
||||
return new MemoryController(memoryFinder());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MemoryFinder memoryFinder() {
|
||||
return new MemoryFinder();
|
||||
}
|
||||
}
|
||||
14
springboot/autoconfig/src/main/java/memory/Memory.java
Normal file
14
springboot/autoconfig/src/main/java/memory/Memory.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package memory;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
public class Memory {
|
||||
|
||||
private long used;
|
||||
private long max;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package memory;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class MemoryController {
|
||||
|
||||
private final MemoryFinder memoryFinder;
|
||||
|
||||
@GetMapping("/memory")
|
||||
public Memory system() {
|
||||
Memory memory = memoryFinder.get();
|
||||
log.info("memory={}", memory);
|
||||
return memory;
|
||||
}
|
||||
}
|
||||
21
springboot/autoconfig/src/main/java/memory/MemoryFinder.java
Normal file
21
springboot/autoconfig/src/main/java/memory/MemoryFinder.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package memory;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class MemoryFinder {
|
||||
|
||||
public Memory get() {
|
||||
long max = Runtime.getRuntime().maxMemory();
|
||||
long total = Runtime.getRuntime().totalMemory();
|
||||
long free = Runtime.getRuntime().freeMemory();
|
||||
long used = total - free;
|
||||
return new Memory(used, max);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("init memoryFinder");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user