package hello;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import net.bull.javamelody.MonitoringFilter;
import net.bull.javamelody.Parameter;
import net.bull.javamelody.SessionListener;

/**
 * @author speralta, evernat
 */
@Configuration
@ImportResource("classpath:net/bull/javamelody/monitoring-spring.xml")
@SuppressWarnings("javadoc")
public class JavaMelodyConfiguration implements ServletContextInitializer {
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		servletContext.addListener(new SessionListener());
	}

	@Bean
	public FilterRegistrationBean javaMelody() {
		final FilterRegistrationBean javaMelody = new FilterRegistrationBean();
		javaMelody.setFilter(new MonitoringFilter());
		javaMelody.setAsyncSupported(true);
		javaMelody.setName("javamelody");
		javaMelody.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);

		// see the list of parameters:
		// https://github.com/javamelody/javamelody/wiki/UserGuide#6-optional-parameters
		javaMelody.addInitParameter(Parameter.LOG.getCode(), Boolean.toString(true));
		// to exclude images, css, fonts and js urls from the monitoring:
		// javaMelody.addInitParameter(Parameter.URL_EXCLUDE_PATTERN.getCode(), "(/webjars/.*|/css/.*|/images/.*|/fonts/.*|/js/.*)");
		// to add basic auth:
		// javaMelody.addInitParameter(Parameter.AUTHORIZED_USERS.getCode(), "admin:pwd");
		// to change the default storage directory:
		// javaMelody.addInitParameter(Parameter.STORAGE_DIRECTORY.getCode(), "/tmp/javamelody");

		javaMelody.addUrlPatterns("/*");
		return javaMelody;
	}
}

@@ -123,16 +123,94 @@
 ption;%0A%0A
+import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;%0A
 import o
@@ -529,51 +529,263 @@
 ce;%0A
-%0Aimport net.bull.javamelody.MonitoringFilte
+import org.springframework.stereotype.Controller;%0Aimport org.springframework.stereotype.Service;%0Aimport org.springframework.web.bind.annotation.RestController;%0A%0Aimport net.bull.javamelody.MonitoringFilter;%0Aimport net.bull.javamelody.MonitoringSpringAdviso
 r;%0Ai
@@ -2269,11 +2269,784 @@
 lody;%0A%09%7D
+%0A%0A%09@Bean%0A%09public MonitoringSpringAdvisor springServiceMonitoringAdvisor() %7B%0A%09%09final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();%0A%09%09interceptor.setPointcut(new AnnotationMatchingPointcut(Service.class));%0A%09%09return interceptor;%0A%09%7D%0A%0A%09@Bean%0A%09public MonitoringSpringAdvisor springControllerMonitoringAdvisor() %7B%0A%09%09final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();%0A%09%09interceptor.setPointcut(new AnnotationMatchingPointcut(Controller.class));%0A%09%09return interceptor;%0A%09%7D%0A%0A%09@Bean%0A%09public MonitoringSpringAdvisor springRestControllerMonitoringAdvisor() %7B%0A%09%09final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();%0A%09%09interceptor.setPointcut(new AnnotationMatchingPointcut(RestController.class));%0A%09%09return interceptor;%0A%09%7D
 %0A%7D%0A

add example for automatic monitoring of controllers and services in Spring Boot