Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Spring AOP for logging     - Ubuntu 14.04 installation and configuration environment variable JDK1.8.0_25 (Linux)

- Redis performance test (Database)

- C language keywords Comments (Programming)

- using the ssh command to check the socket / Network Connections (Linux)

- JVM garbage collector and memory allocation strategy (Programming)

- C ++ multithreading and critical resource instance (Programming)

- redis configuration in detail (English) (Database)

- Category prevent DoS attacks against Linux (Linux)

- Vagrant failed to start, stuck in Waiting for VM to boot solution (Linux)

- The official release method to upgrade to Ubuntu 15.04 (Linux)

- NET Developers need to know some Linux commands (Linux)

- Variables Python variables (Programming)

- CentOS system Amoeba + MySQL Master-slave configuration (Database)

- Linux script to copy the folder to all folders with the same name (Linux)

- printf PHP string operations () built-in function usage (Programming)

- On the PC goes heavy security watch your startup items (Linux)

- expdp / impdp use version parameter data migration across versions (Database)

- Oracle view object space usage show_space (Database)

- Getting Started with Linux system to learn: how to configure a static IP address for CentOS7 (Linux)

- How do you temporarily emptied Bash environment variable before running the command on Linux (Linux)

 
         
  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:      
 
- The text formatting tools awk Linux system (Linux)
- YUM install desktop environment in CentOS (Linux)
- The Sublime Text 3 configuration file (Linux)
- Linux host dual LAN transceiver package ARP problem (Linux)
- Packages with Snort intrusion monitoring light (Linux)
- Linux System Tutorial: Fix ImportError: No module named wxversion error (Linux)
- Linux under DB2SQL1024N A database connection does not exist. SQLS (Database)
- Shell Scripting Interview Questions (Programming)
- Install Ubuntu open source drawing program MyPaint 1.2.0 (Linux)
- Practical top command (Linux)
- Ubuntu / openSUSE users to install Veusz 1.21.1 (Linux)
- Linux foreground to background process switch (Linux)
- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)
- Zabbix monitoring tool deployment under Ubuntu server (Server)
- Create your own YUM repository (Linux)
- Samhain: Powerful intrusion detection system under Linux (Linux)
- The AWK use Cygwin tools mysql binlog log viewer (Database)
- Ubuntu under Spark development environment to build (Server)
- Python extension module Ganglia 3.1.x (Linux)
- Let Markdown code syntax highlighting and support Django1.6 (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.