Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Spring AOP for logging     - Installed FFmpeg 2.6.3 on Ubuntu / Debian / Fedora system (Linux)

- Detailed iptables (Linux)

- Difference between TCP and UDP protocols (Linux)

- Linux System Administrator common interview questions and answers 30 (Linux)

- Oracle 11g RAC root.sh execution error second node failure example (Database)

- Elementary OS Freya global menu (Linux)

- Zombie process under Linux (Linux)

- Java rewrite the hashcode method (Programming)

- Oracle ORA-01089 failure analysis (Database)

- Compile and install Ubuntu Linux 4.0.5 kernel, network and fix vmware kernel module compilation error (Linux)

- How to clean up your Ubuntu 14.10 / 14.04 / 13.10 system (Linux)

- Python control multi-process and multi-threaded concurrency (Programming)

- Oracle database import and export (Database)

- Applications Linux leap second problem caused (Linux)

- JavaScript function definition mode (Programming)

- Depth understanding of the use of Spring Redis (Programming)

- To setup CentOS LAMP environment (Server)

- Distributed Firewall Design on Linux platform (Linux)

- Tmux create the perfect terminal management tool under CentOS (Linux)

- The practical application of Oracle synonyms + dblink (Database)

 
         
  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:      
 
- Java executable file to read information from a database copy (Programming)
- Linux formatted partition error Could not stat / dev / sda No such file or directory Solution (Linux)
- Linux uses a screen recording under RecordMyDesktop (Linux)
- Mumble installation source VoIP application on Ubuntu (Linux)
- Oracle 11g contraction table space error ORA-03297: file contains used data beyondrequested RESIZE value (Database)
- How Ubuntu Snappy Core 2 running on Raspberry Pi (Linux)
- How to compile and install wxWidgets in Ubuntu / Debian / Linux Mint (Linux)
- MySQL master recovery from failure using binlog (Database)
- Use the dd command to the hard disk I / O performance test (Linux)
- Android 4.2 compilation notes (Programming)
- Windows7 / 8 / 8.1 hard drive to install Ubuntu 14.04 dual system (Linux)
- Ceph tuning --Journal and tcmalloc (Server)
- Installation and use Docker under Ubuntu 14.04 (Linux)
- About Hibernate cache, you want the latest data have trouble even session.clear (Database)
- Ubuntu Linux Change the PATH (Linux)
- Eclipse distributed management using GitHub project development (Linux)
- Debian 8.2.0 (Jessie) fast clean installation tutorial (Linux)
- git checkout generated in the use of temporary br (Linux)
- Android Action Compendium (Programming)
- Linux firewall settings -DNS server articles (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.