Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Spring AOP for logging     - CentOS Set the Mono environment variable (Server)

- MariaDB database storage path modify configuration issues (Database)

- Linux memory Cache Analysis (Linux)

- Linux Powerful command Awk Introduction (Linux)

- CentOS7 yum install third-party source EPEL (Linux)

- Security enhancements in Ubuntu ssh service (Linux)

- Nginx server security configuration (Server)

- Linux Log Clear (Linux)

- Difference in MySQL VARCHAR and CHAR data format (Database)

- SolrCloud-5.2.1 cluster deployment and testing (Server)

- You can not ignore the seven Git tips (Linux)

- Java factory pattern (Factory mode) (Programming)

- Household use Linux Security (Linux)

- Oracle Database Performance Optimization of memory disk (Database)

- Precautions against hackers Linux environment (Linux)

- Fedora10 use Git version Configuration Management (Linux)

- Java precision four operations (Programming)

- How to install Kernel 4.0.2 on CentOS 7 (Linux)

- Analysis of potential problems through custom Orabbix monitoring Oracle (Database)

- C ++ 11 feature: decltype keywords (Programming)

 
         
  Spring AOP for logging
     
  Add Date : 2016-11-17      
         
       
         
  Java development in the management of the log there are many. I usually use a filter, or the Spring interceptors for processing log. If the filter is relatively simple, as long as all .do submitted to intercept and get the action of the submission path you can get calls to each method. Then logging. Using filters own benefits can be selective for certain methods to filter logging. However, to achieve a bit of trouble.

Another is the use of Spring AOP. Achieved in this way is very simple, just click the configuration profile on it. But this approach will intercept all of the action of each operation. So that efficiency is relatively low. However, this method still want a detailed log very good. Here I will introduce the use of Spring AOP for logging mode.

The first. Spring AOP interception for ordinary class

First, we want to write a general category, such as the logging classes. such as
package chen.hui.log

public classs MyLog {
      // Writing method in which class, method name poem can be arbitrary. I have used before and after the standard is represented here
      public void before () {
                System.out.println ( "call this function before calling this method intercept method, the output of this statement");
      }
      public void after () {
                  System.out.println ( "to be called after the call intercept method this method, the output of this statement");
      }
}

 

Secondly, we are writing a class as interceptor class (Spring's AOP is to intercept this class inside the method)
package chen.hui.log

public class Test {// this class can write any number of methods. I just write a
          public void test () {
                Sytem.out.println ( "test method to test the class is called");
          }
}


Finally, write the configuration file. We need a few words arranged in Spring configuration file
< Bean id = "testLog" class = "chen.hui.log.MyLog"> < / bean> < -! Log class is injected into the bean. ->
< Aop: config>
 < Aop: aspect id = "b" ref = "testLog"> < - call logging class -!>
  < Aop: pointcut id = "log" expression = "execution (* chen.hui.log * * (..)..)" /> < - Log package configurations in all classes before the call will be intercepted! ->
  < Aop: before pointcut-ref = "log" method = "before" /> < -! In the log package all of the following are all methods of the class before calling MyLog the method is called before ->
  < Aop: after pointcut-ref = "log" method = "after" / >> < - log all packets in all of the following methods are called before the class is called in after MyLog method ->!
 < / Aop: aspect>
< / Aop: config>

 

Here the whole procedure is finished, add the code to log logic MyLog inside before and after the class method to complete log management. These are the management of the normal class, If you want to intercept a certain category. As long as the penultimate * change the class name on it.

  Second: the use of Spring AOP for action to make the log management

If you want to intercept the action to do action log management, basic and almost above, but be careful. Following

First is to write a general category, but the methods in this class need to pass parameters. such as
package chen.hui.log

import org.aspectj.lang.JoinPoint;

public classs MyLog {

      // Writing method in which class, method name poem can be arbitrary. I have used before and after the standard is represented here

        JoinPoint class // here can get, action all the configuration information and request and other built-in objects.

      public void before (JoinPoint joinpoint) {

                joinpoint.getArgs (); // This method returns an array, the array includes request and other types of objects ActionCofig

                System.out.println ( "call this function before calling this method intercept method, the output of this statement");
      }
      public void after (JoinPoint joinpoint) {

                  System.out.println ( "to be called after the call intercept method this method, the output of this statement");
      }
}

Secondly, we write as a class action was blocked class (Spring's AOP is to intercept this class inside the method)
package chen.hui.log

public class LoginAction {// this class can write any number of methods. I just write a
 public void test () {
  Sytem.out.println ( "test method to test the class is called");
 }
}


Finally, write the configuration file. We need a few words arranged in Spring configuration file
< Bean id = "testLog" class = "chen.hui.log.MyLog"> < / bean> < -! Log class is injected into the bean. ->
      < Aop: config>
            < Aop: aspect id = "b" ref = "testLog"> < - call logging class -!>
            < Aop: pointcut id = "log" expression = "execution (* chen.hui.log * * (..)..)" /> <- Log package configurations in all classes before the call will be intercepted! ->
            < Aop: before pointcut-ref = "log" method = "before" /> < -! In the log package all of the following are all methods of the class before calling MyLog the method is called before ->
            < Aop: after pointcut-ref = "log" method = "after" /> < - log all packets in all of the following methods are called before the class is called in after MyLog method -!>
      < / Aop: aspect>
 < / Aop: config>

In addition to the basic parameters and other places outside the general category similar.

Note: the general category can monitor a single class, and action in the configuration file to the package name only and not to the action of the class name. Or will be error. That is, if you want to log must record all the action but can not record one, which I tried for a long time the results obtained.
     
         
       
         
  More:      
 
- MySQL common functions summary (Database)
- Java multithreading easy to confuse the concept (Programming)
- SQL in the specific internal Oracle process (Database)
- 30 minutes with your Quick Start MySQL Tutorial (Database)
- Eclipse-ee Tomcat browser can not start Tomcat, and Web project service deployment (Server)
- Linux (RHEL6 CENTOS6 OLE6) VNC-SERVER Installation and Configuration (Server)
- CentOS 5.x install Lua 5.2.3 error (LIBS = -lncurses) (Linux)
- GNU / Linux system, how to clean up memory space (Linux)
- Ubuntu install Lighttpd + Mono support .net (Linux)
- The practical application of Oracle synonyms + dblink (Database)
- Use chattr and lsattr command to manage linux files and directory attributes (Linux)
- C ++ Supplements - malloc free and new delete the same and different (Programming)
- Linux system security configuration (Linux)
- Java object initialization (Programming)
- AngularJS achieve picture upload feature (Programming)
- Java study notes: String (Programming)
- Linux platform to prevent hackers to share practical skills (Linux)
- Varnish configuration language VCL and its built-in variables (Server)
- Linux Network Security: nmap port scanning software (Linux)
- Use regular expressions to check whether the input box to enter a URL (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.