1.源码解析:
提供一些状态管理机制:
public interface SpringApplicationAdminMXBean {
/**
* Specify if the application has fully started and is now ready.
* @return {@code true} if the application is ready
* @see org.springframework.boot.context.event.ApplicationReadyEvent
*/
boolean isReady();
/**
* Specify if the application runs in an embedded web container. Return {@code false}
* on a web application that hasn't fully started yet, so it is preferable to wait for
* the application to be {@link #isReady() ready}.
* @return {@code true} if the application runs in an embedded web container
* @see #isReady()
*/
boolean isEmbeddedWebApplication();
/**
* Return the value of the specified key from the application
* {@link org.springframework.core.env.Environment Environment}.
* @param key the property key
* @return the property value or {@code null} if it does not exist
*/
String getProperty(String key);
/**
* Shutdown the application.
* @see org.springframework.context.ConfigurableApplicationContext#close()
*/
void shutdown();
通过实现Spring的ApplicationContextAware, GenericApplicationListener接口,其他没有了。
实例方法如下:
public SpringApplicationAdminMXBeanRegistrar(String name) throws MalformedObjectNameException {
this.objectName = new ObjectName(name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Assert.state(applicationContext instanceof ConfigurableApplicationContext,
"ApplicationContext does not implement ConfigurableApplicationContext");
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
void onWebServerInitializedEvent(WebServerInitializedEvent event) {
if (this.applicationContext.equals(event.getApplicationContext())) {
this.embeddedWebApplication = true;
}
}
@Override
public void afterPropertiesSet() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
server.registerMBean(new SpringApplicationAdmin(), this.objectName);
if (logger.isDebugEnabled()) {
logger.debug("Application Admin MBean registered with name '" + this.objectName + "'");
}
}
@Override
public void destroy() throws Exception {
ManagementFactory.getPlatformMBeanServer().unregisterMBean(this.objectName);
}
二。使用:
MBean,managed bean,被管理的bean,也就是一个被管理的Java对象。
它暴露了一个管理接口,可以包含以下内容:
一系列可读或可写的属性
一系列可调用的操作
自我描述
SpringApplicationAdminMXBean
Springboot里默认有暴露出SpringApplicationAdminMXBean,通过源码可以看出,是在afterPropertiesSet方法注册了mbean,destroy方法取消注册
该bean本身暴露了两个属性(Ready, EmbeddedWebApplication),两个方法(getProperty, shutdown)
通过jvisualvm,可以看到具体mbean的信息