BAEL-5789 - reinitializing singleton beans in context (#13096)
* BAEL-5789 - reinitializing singleton beans in context * BAEL-5789 - reinitializing singleton beans in context - changing file path to relative
This commit is contained in:
parent
2ea3448efa
commit
061c30aa08
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reinitializebean;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ReinitializeBeanApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ReinitializeBeanApp.class, args);
|
||||
}
|
||||
}
|
51
spring-core-6/src/main/java/com/baeldung/reinitializebean/cache/ConfigManager.java
vendored
Normal file
51
spring-core-6/src/main/java/com/baeldung/reinitializebean/cache/ConfigManager.java
vendored
Normal file
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.reinitializebean.cache;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@Service("ConfigManager")
|
||||
public class ConfigManager {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(ConfigManager.class);
|
||||
|
||||
private Map<String,Object> config;
|
||||
|
||||
private final String filePath;
|
||||
|
||||
public ConfigManager(@Value("${config.file.path}") String filePath) {
|
||||
this.filePath = filePath;
|
||||
initConfigs();
|
||||
}
|
||||
|
||||
private void initConfigs() {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(Files.newInputStream(Paths.get(filePath)));
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error loading configuration:", e);
|
||||
}
|
||||
config = new HashMap<>();
|
||||
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
|
||||
config.put(String.valueOf(entry.getKey()), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Object getConfig(String key) {
|
||||
return config.get(key);
|
||||
}
|
||||
|
||||
public void reinitializeConfig() {
|
||||
initConfigs();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.reinitializebean.controller;
|
||||
|
||||
import com.baeldung.reinitializebean.cache.ConfigManager;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/config")
|
||||
public class ConfigController {
|
||||
|
||||
@Value("${config.file.path}")
|
||||
private String filePath;
|
||||
private final ApplicationContext applicationContext;
|
||||
private final ConfigManager configManager;
|
||||
|
||||
public ConfigController(ApplicationContext applicationContext, ConfigManager configManager) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.configManager = configManager;
|
||||
}
|
||||
|
||||
@GetMapping("/reinitializeConfig")
|
||||
public void reinitializeConfig() {
|
||||
configManager.reinitializeConfig();
|
||||
}
|
||||
|
||||
@GetMapping("/reinitializeBean")
|
||||
public void reinitializeBean() {
|
||||
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory();
|
||||
registry.destroySingleton("ConfigManager");
|
||||
registry.registerSingleton("ConfigManager", new ConfigManager(filePath));
|
||||
}
|
||||
|
||||
@GetMapping("/destroyBean")
|
||||
public void destroyBean() {
|
||||
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory();
|
||||
registry.destroySingleton("ConfigManager");
|
||||
}
|
||||
|
||||
@GetMapping("/{key}")
|
||||
public Object get(@PathVariable String key) {
|
||||
return configManager.getConfig(key);
|
||||
}
|
||||
|
||||
@GetMapping("/context/{key}")
|
||||
public Object getFromContext(@PathVariable String key) {
|
||||
ConfigManager dynamicConfigManager = applicationContext.getBean(ConfigManager.class);
|
||||
return dynamicConfigManager.getConfig(key);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
environment.name=${OS}
|
||||
java.home.and.environment=${JAVA_HOME}+${OS}
|
||||
not.existing.system.property=${thispropertydoesnotexist}
|
||||
baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}
|
||||
baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}
|
||||
config.file.path=./spring-core-6/src/main/resources/config.properties
|
|
@ -0,0 +1 @@
|
|||
property1=value2
|
Loading…
Reference in New Issue