Spring + AOP + Log4j uses annotations to record logs of a specific method.

Source: Internet
Author: User

Spring + AOP + Log4j uses annotations to record logs of a specific method.

I. Description of spring aop execution expressions

When using the spring framework to configure AOP, you must define pointcut "entry point" in both XML configuration files and annotations"

For example, define the pointcut expression execution (* com. sample. service. impl ..*.*(..))

Execution () is the most commonly used tangent function. Its syntax is as follows:

The entire expression can be divided into five parts:

1. execution (): expression subject.

2. The first "*" indicates the return type, and "*" indicates all types.

3. Package name: indicates the name of the package to be intercepted. The following two periods indicate the current package and all the sub-packages of the current package, com. sample. service. the impl package and the methods of all classes in the Child package.

4. The second "*" indicates the class name, and "*" indicates all classes.

5. * (...): The asterisk (*) indicates the method name. The asterisk (*) indicates all methods. The square brackets (*) indicate the parameters of the method, and the two periods (*) indicate any parameters.

Ii. Procedures

1. A method for logging: updatePromote

/*** Modify the item activity ** @ param vo * @ param promoteId to the query ID of AOP monitoring * @ return * @ throws Exception */public ResultVO updatePromote (PromoteVO, long promoteId) throws Exception;

2. Add a crosstab concern and print the log. The Java class is

Package com. fortis. drugstore. web. userdbThrift. aop; import java. util. date; import org. apache. log4j. logger; import org. aspectj. lang. joinPoint; import org. aspectj. lang. proceedingJoinPoint; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. pointcut; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. component; import com. fortis. drugstore. base. action. baseAction; import com. fortis. drugstore. system. utils. sessionInfo; import com. fortis. drugstore. web. userdbThrift. activity. service. promoteService; import com. fortis. thrift. userdb. promoteVO; // declare that this is a Component @ Component // declare that this is a validation Bean @ Aspectpublic class ServiceAspect extends BaseAction <Object> {private static final long serialVersionUID = 7690224540336%592l; private final static Logger log = Logger. getLogger (ServiceAspect. class); @ Autowired private PromoteService; // configure the entry point. This method has no method body, it is mainly used to facilitate other methods of the same type to use the entry point configured here @ Pointcut ("execution (* com. fortis. drugstore. web. userdbThrift. activity. service. impl .. promoteServiceImpl. updatePromote (..)) ") public void aspect () {}/** configure the pre-notification. Use the entry point registered on method aspect () * and accept the JoinPoint entry point object, the parameter */@ Before ("aspect ()") public void before (JoinPoint joinPoint) {PromoteVO obBefore = new PromoteVO (); Object [] param = joinPoint. getArgs (); if (log. isInfoEnabled () {Object promoteId = param [1]; // There are two parameters in the cut point, and the second is the table's primary key, used to query SessionInfo sessionInfo = (SessionInfo) getSession (). getAttribute ("sessionInfo"); try {obBefore = service. queryPromoteDetail (Long. valueOf (promoteId. toString ();} catch (Exception e) {e. printStackTrace ();} log.info ("[modify activity data]:" + new Date () + "[account]:" + sessionInfo. getUserAcct () + "[name]:" + sessionInfo. getUserName (); log.info ("before modification:" + obBefore. toString () ;}}// configure the post-notification. Use the entry point registered on method aspect () @ After ("aspect ()") public void after (JoinPoint joinPoint) {PromoteVO obAfter = new PromoteVO (); Object [] param = joinPoint. getArgs (); if (log. isInfoEnabled () {Object promoteId = param [1]; try {obAfter = service. queryPromoteDetail (Long. valueOf (promoteId. toString ();} catch (Exception e) {e. printStackTrace ();} log.info ("after modification:" + obAfter. toString ());}}}

3. spring aop configuration file spring-aop.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: c = "http://www.springframework.org/schema/c" xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: context = "http://www.springframework.org/schema/context" xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: je E = "http://www.springframework.org/schema/jee" xmlns: lang = "http://www.springframework.org/schema/lang" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: p = "http://www.springframework.org/schema/p" xmlns: task = "http://www.springframework.org/schema/task" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: util = "http://www.springframework.org/schema/util" xsi: schemaLocation = "http: // www .Springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context Ntext. xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spr Ing-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <! -- Activate the automatic proxy function --> <aop: aspectj-autoproxy proxy-target-class = "true"/> </beans>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.