how can we set the properties of management, other properties which are related to hibernate, jpa, transactions, etc dynamically from rest api. I have tried few things here,
import javax.persistence.EntityManager; import org.hibernate.SessionFactory; import org.hibernate.stat.Statistics;
// Your endpoint that toggles the Hibernate statistics public void toggleHibernateStats(boolean enableStats) { EntityManager entityManager = getEntityManager(); SessionFactory sessionFactory = entityManager.unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics();
// Set the Hibernate statistics property to enable or disable statistics statistics.setStatisticsEnabled(enableStats);
// Return a response indicating whether statistics were successfully turned on or off if (statistics.isStatisticsEnabled() == enableStats) { return "Hibernate statistics " + (enableStats ? "enabled" : "disabled") + " successfully"; } else { return "Failed to " + (enableStats ? "enable" : "disable") + " Hibernate statistics"; } }
@RestController public class ActuatorController {
@Autowired private Environment environment;
@PostMapping("/actuator/properties") public void setActuatorProperty(@RequestParam String key, @RequestParam String value) { ((ConfigurableEnvironment) environment).getPropertySources().addFirst(new MapPropertySource( "runtimeProperties", Collections.singletonMap(key, value))); } }
@RestController public class ActuatorController {
@Autowired private EndpointProperties endpointProperties;
@PostMapping("/actuator/properties") public void setActuatorProperty(@RequestParam String key, @RequestParam String value) { switch (key) { case "enabledByDefault": endpointProperties.setEnabledByDefault(Boolean.parseBoolean(value)); break; case "infoEnabled": endpointProperties.setInfoEnabled(Boolean.parseBoolean(value)); break; default: // handle unknown property break; } } }
And none of these reflected when i was trying to use them.
Comment From: scottfrederick
Spring Boot will generally only read and apply configuration at startup. Spring Cloud has some ability to refresh beans after startup.
Please post any follow-up questions to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.