JavaMelody monitors spring and struts
Because spring's theory is not solid, monitoring spring relies on the Aspect-Oriented AOP technology of sring. Therefore, although the configuration is based on the official documentation, it still cannot obtain monitoring data. Here we will talk about the simple struts monitoring. Struts monitoring is much simpler. You only need to follow the steps below, and it is certainly no problem. Step 1: import necessary jar packages. The required jar packages have been mentioned earlier. One is javamelody. jar, the other is the second step of the jrobin-x.jar, which needs to be in the web. add the corresponding monitoring filter in xml to copy code 1 <filter> 2 <filter-name> monitoring </filter-name> 3 <filter-class> net. bull. javamelody. monitoringFilter </filter-class> 4 5 <init-param> 6 <param-name> log </param-name> 7 <param-value> true </param-value> 8 </init-param> 9 </filter> 10 <filter-mapping> 11 <filter-name> monitoring </filter-name> 12 <url-pattern>/* </url-pattern> 13 </filter-map Ping> 14 15 <listener> 16 <listener-class> net. bull. javamelody. sessionListener </listener-class> 17 </listener> copy the code. Of course, do not forget struts's own filter. Copy the Code 1 <filter> 2 <filter-name> struts </filter-name> 3 <filter-class> org. apache. struts2.dispatcher. filterDispatcher </filter-class> 4 5 <init-param> 6 <param-name> struts. action. extension </param-name> 7 <param-value> action </param-value> 8 </init-param> 9 </filter> 10 <filt Er-mapping> 11 <filter-name> struts </filter-name> 12 <url-pattern>/* </url-pattern> 13 </filter-mapping> copy the code step 3, in struts. add the default package in xml. This package provides the default Interceptor to copy code 1 <package name = "default" extends = "struts-default, json-default"> 2 <! -- Register the interceptor or interceptor stack with the Struts2 framework, generally, it is used for registration of custom interceptor or interceptor Stack --> 3 <interceptors> 4 <interceptor name = "monitoring" class = "net. bull. javamelody. strutsInterceptor "/> 5 <interceptor-stack name =" myStack "> 6 <interceptor-ref name =" monitoring "/> 7 <interceptor-ref name =" defaultStack "/> 8 </interceptor-stack> 9 </interceptors> 10 <! -- Set the default interceptor information to be applied for all actions in the entire package --> 11 <default-interceptor-ref name = "myStack"/> 12 </package> copy other packages in the code for example, the sttuts package, to inherit the default package. Copy code 1 <package name = "test" extends = "default"> 2 <action name = "login" class = "com. test. loginAction "> 3 <result name =" error ">/error. jsp </result> 4 <result name = "success">/success. jsp </result> 5 </action> 6 7 <action name = "search" class = "com. test. searchAction "> 8 <result name =" error ">/error. jsp </result> 9 <result name = "success">/searchSuccess. jsp </result> 10 </action> 11 12 <action name = "hibernatetest" Class = "com. test. testHibernate "> 13 <result name =" error ">/error. jsp </result> 14 <result name = "success">/hibernateSuccess. jsp </result> 15 </action> 16 17 </package>: copy the preceding three steps of the Code, even if the configuration is complete. If a monitoring event is not triggered, such as clicking something to respond to the jump and using struts, data cannot be monitored. Although the corresponding image is displayed, the data on the image is 0, and the Nan or table below is empty. These are the reasons why the listening event is not triggered. Spring monitoring JavaMelody is at the method level for spring monitoring. We can monitor a method of a class, so we need to use pointcut In AOP for listening. Next, let's take a look at the main Listener Configuration: the first step is to import the necessary jar package. The two mentioned above are no longer repeated. Step 2: load the monitoring-spring.xml and our own applicationContext. xml configuration file. If you want to load the web. when reading the spring configuration file in xml, You need to implement a listener 1 <listener> 2 <listener-class> 3 org. springframework. web. context. contextLoaderListener4 </listener-class> 5 </listener>. add the spring file path in xml. Use this context parameter to set the copy code 1 <context-param> 2 <param-name> contextConfigLocation </param-name> 3 <param-value> 4 classpath: net/bull/javamelody/monitoring-spring.xml5/WEB-INF/classes/bean. xml6 </param-value> 7 </context-param> copies the first line of the code above and defines the spring configuration file for the monitoring application. below is our own spring configuration file. Step 3: copy the Code 1 <bean id = "facadeMonitoringAdvisor" class = "net. bull. javamelody. monitoringSpringAdvisor "> 2 <property name =" pointcut "> 3 <bean class =" org. springframework. aop. support. jdkRegexpMethodPointcut "> 4 <property name =" pattern "value =" com. test. *. * "/> 5 </bean> 6 </property> 7 </bean> copy the code. Here, the JdkRegexpMethodPointcut is used, that is, the regular expression is used to locate the business method. The following parameters may be pattern or patterns, and the com. test. *. * indicates that it corresponds to com. com. test. *. doGet indicates that it corresponds to com. the doGet method for all classes under the test package. * Test. * Indicates the configuration details of all methods of classes ending with Test. You also need to learn about the use of pointcut In AOP. If not, read the related knowledge. Then add the interceptor for the bean you want to monitor: copy the Code 1 <bean id = "ProxyFactoryBean" class = "org. springframework. aop. framework. proxyFactoryBean "> 2 <property name =" target "> 3 <ref bean =" Computer "/> 4 </property> 5 <property name =" interceptorNames "> 6 <list> 7 <value> facadeMonitoringAdvisor </value> 8 </list> 9 </property> 10 </bean> copy the code so that when ProxyFactoryBean is used, the interceptor interceptorNames is automatically called, and the method in facadeMonitoringAdvisor is located. Start net. bull. javamelody. MonitoringSpringAdvisor for information monitoring. The corresponding programming code is directly attached here. If you are interested, you can run the experiment to understand this idea and monitor the business you are interested in. 1 people. java 2 public class People {3 // speech 4 public void speak () {5 System. out. println ("Hello, I am People! "); 6} 7 // Running 8 public void Running () {9 System. out. println (" I'm Running ...... Run ............ Escape ...... "); 10} 11 // love 12 public void Loving () {13 System. out. println (" I am in love with MM ...... Don't bother me! "); 14} 15 // death 16 public void died () {17 System. out. println ("finished, I am dead"); 18} 19} 20 21 advice class 22 public class LogerPeople implements MethodBeforeAdvice {23 24 public void before (Method method, Object [] args, object target) 25 throws Throwable {26 System. out. println (target. getClass (). getSimpleName () + "in progress" + 27 method. getName () + "! "); 28 System. out. println (" before! ____________ "); 29 30} 31} 32 33 TestMain34 public class TestMain {35 36 public static void main (String [] args) {37 ApplicationContext ac = new ClassPathXmlApplicationContext (38 "bean1.xml"); 39 40 // get the IComputer interface implementation class instance 41 People c = (People) ac through ProxyFactoryBean. getBean ("ProxyFactoryBean"); 42 c. speak (); 43 c. running (); 44 c. loving (); 45 c. died (); 46} 47} 48 49 50 spring configuration file 51 <bean id = "Computer" cl Ass = "com. test. people "> </bean> 52 <bean id =" LogerComputer "class =" com. test. logerPeople "/> 53 54 <bean id =" ProxyFactoryBean "class =" org. springframework. aop. framework. proxyFactoryBean "> 55 <property name =" target "> 56 <ref bean =" Computer "/> 57 </property> 58 <property name =" interceptorNames "> 59 <list> 60 <value> DefaultAdvisor </value> 61 </list> 62 </property> 63 </bean> 64 65 <bean id = "DefaultAdvisor" class = "o Rg. springframework. aop. support. defaultPointcutAdvisor "> 66 <property name =" pointcut "ref =" JdkRegexpPointcut "/> 67 <property name =" advice "ref =" LogerComputer "/> 68 </bean> 69 <bean id = "JdkRegexpPointcut" class = "org. springframework. aop. support. jdkRegexpMethodPointcut "> 70 <property name =" patterns "> 71 <list> 72 <value>. * spea. * </value> 73 <value>. * ing </value> 74 <value>. * di. * </value> 75 </list> 76 </property> 77 <property name = "excludedPattern" value = ". * Run. * "/> 78 </bean> I tried to copy the code for a day and a half and never monitored the data because although the default interceptor is configured, however, no response is triggered to the interceptor. Therefore, this monitoring class has never been called, and of course there is no monitoring information. To sum up, we still do not understand the principles of spring AOP. We will add spring-related learning in the future.