JavaMelody monitoring spring, struts

Foreword

  Recall that the basic configuration of Javamelody , how to use Javamelody to monitor JDBC and SQ L.

  Continue here explain how to monitor struts and spring.

  Easy to hand code, please indicate: xingoo

  Since the spring of theory is not solid, but also rely on sring monitoring spring of AOP Aspect Oriented technology to do, so when configured though the official reference document, but still can not obtain monitoring data. Here to talk about the simple monitoring of struts.

  Struts monitoring

  Struts monitoring to be relatively more simple, just follow the steps below, it is certainly not a problem.

  The first step introduced the necessary jar package, the package needs jar front already been mentioned.

  Is a javamelody.jar, the other is jrobin-x.jar

  The second step, it is necessary to add a corresponding filter to monitor in web.xml

 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-mapping>
14 
15 <listener>
16   <listener-class> net.bull.javamelody.SessionListener</listener-class>
17  </listener>

  Of course, do not forget your own filters struts

 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   <filter-mapping>
11    <filter-name>struts</filter-name>
12    <url-pattern>/*</url-pattern>
13   </filter-mapping>

  The third step is to add the default package in struts.xml, this package provides a default interceptor

 1 <package name= "default" extends ="struts-default,json-default" >
 2   <!-- 向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册 -->
 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   <!--设置整个包范围内所有Action所要应用的默认拦截器信息 -->
11   <default-interceptor-ref name ="myStack" />
12     </package>

  Other package such as sttuts package, should inherit the default package.

 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>

  The above three steps, even if the configuration is finished.

  If you do not trigger monitor events, such as clicking something to respond to jump, use the struts, it is not monitoring data. Although there are displays corresponding images, but the data on the pictures are 0, Nan or below the table is empty, these are not listening event triggers causes.

 

  Spring monitor

 

  JavaMelody for the spring is to monitor the level of the method, we can monitor to a method of a class, it is necessary to use AOP pointcut inside listening.

  Here's a look at the main monitor configuration:

  The first step is still necessary to import the jar package, the above said two, not repeat it.

 

  The second step, load monitoring-spring.xml as well as our own applicationContext.xml profile.

  If you want to read the spring configuration file web.xml at load time, the need to implement a listener

1 <listener>
2         <listener-class>
3             org.springframework.web.context.ContextLoaderListener
4         </listener-class>
5     </listener>

  Then in the web.xml, add the spring file path. Context parameters set by only

1 <context-param>
2   <param-name> contextConfigLocation</param-name>
3              <param-value>
4                   classpath:net/bull/javamelody/monitoring-spring.xml
5                   /WEB-INF/classes/bean.xml
6              </param-value>
7  </context-param>

  The first line above, the definition of the spring profile monitoring applications, the following is our own spring configuration file.

  The third step, regular expressions, a positioning method

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>

  There is mainly used JdkRegexpMethodPointcut , which is being positioned business method expressions. The following parameters may be the pattern or patterns, the parameters

    com.test. *. * means all methods in all classes corresponding to the package under com.test

    com.test. *. doGet mean com.test packet corresponding to the method name of all classes doGet

    . * Test. * Means that all methods of all classes ending with Test

  Specific configuration details need to go to learn about AOP pointcut used on the entry point. If not, then look at the relevant knowledge of it.

  For bean is then want to monitor, add the interceptor:

 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>

  When using this ProxyFactoryBean, it will automatically call interceptor interceptorNames, navigate to the method facadeMonitoringAdvisor, and starting around method net.bull.javamelody.MonitoringSpringAdvisor , monitoring information.

 

  Corresponding programming code section here also directly attached, may lower interest test run, the main idea is to understand this, you can monitor their own interest to the business.

 1 people.java
 2 public class People{
 3     // 讲话
 4      public void speak() {
 5          System.out.println("Hello,我是People!");
 6      }
 7      // 跑步
 8      public void Running() {
 9          System.out.println("我在跑……跑…………逃……");
10      }
11      // 恋爱
12      public void Loving() {
13         System.out.println("我在和MM恋爱……别来打搅我!");
14      }
15     // 死亡
16      public void died() {
17          System.out.println("完了,我死了");
18      }
19  }
20 
21 advice类
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() + "正在" +
27  method.getName()+ "!");
28   System.out.println("before!________________");
29  
30  }
31 } 
32 
33 TestMain
34 public class TestMain {
35    
36     public static void main(String[] args) {
37         ApplicationContext ac = new ClassPathXmlApplicationContext(
38                 "bean1.xml");
39  
40         //通过ProxyFactoryBean获取IComputer接口实现类的实例
41         People c = (People) ac.getBean("ProxyFactoryBean");
42         c.speak();
43         c.Running();
44         c.Loving();
45         c.died();
46     }
47 }  
48 
49 
50 spring配置文件
51 <bean id="Computer" class="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="org.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>
View Code

  

  I fiddle with a day and a half, has been monitoring data can not, because although the default configuration of the interceptor, but there has been no interceptors to trigger a response. Therefore, there has been no call to monitor this category, of course, there is no monitoring information emerged.

  To sum up, the principle of spring AOP or because there is no understanding, the future will spring up on the relevant learning.

Reproduced in: https: //my.oschina.net/u/204616/blog/544984

Guess you like

Origin blog.csdn.net/weixin_34132768/article/details/91990135