在Spring-Boot项目中使用JavaMelody监视spring bean

gam*_*ore 7 spring java-melody spring-boot

我正在尝试基于Spring教程构建RESTful Web服务来监视REST应用程序,但在Java Melody文档页面中,配置依赖于web.xml文件,但spring项目没有这样的文件.我尝试使用java旋律注释并在WebInitializer中设置contextConfigLocation,但是当我进入Java Melody页面时,我看不到Spring部分.

我有这样的WebInitializar:

public class WebInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class).properties();
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("contextConfigLocation", "classpath:net/bull/javamelody/monitoring-spring.xml");
    super.onStartup(servletContext);
}
}
Run Code Online (Sandbox Code Playgroud)

我已经将contextConfigLocation设置为Java Melody文档所说的.

而我的控制器:

@RestController
@MonitoredWithSpring
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();


@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}
}
Run Code Online (Sandbox Code Playgroud)

有什么建议让它起作用吗?

eve*_*nat 6

现在有一个用javamelody监控Spring-boot应用程序的文档,包括Spring bean:https: //github.com/javamelody/javamelody/wiki/SpringBootStarter


小智 5

您只需要Web应用程序中的javamelody依赖jar,并在spring应用程序上下文中注册两个bean:

@Bean
public HttpSessionListener javaMelodyListener(){
    return new net.bull.javamelody.SessionListener();
}

@Bean
public Filter javaMelodyFilter(){
    return new net.bull.javamelody.MonitoringFilter();
}
Run Code Online (Sandbox Code Playgroud)